Skip to content

Commit 80df034

Browse files
authored
Merge pull request #1 from rebeccawshaw/sd-python-doc-samples
Service Directory python doc samples
2 parents d5afc7c + 9995dc5 commit 80df034

File tree

5 files changed

+298
-0
lines changed

5 files changed

+298
-0
lines changed

servicedirectory/README.rst.in

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Service Directory
5+
short_name: Service Directory
6+
url: https://cloud.google.com/service-directory/docs/
7+
description: >
8+
`Google Cloud Service Directory`_ is a platform
9+
for discovering, publishing, and connecting services. It offers customers a
10+
single place to register and discover their services in a consistent and
11+
reliable way, regardless of their environment. These sample Java applications
12+
demonstrate how to access the Service Directory API using the Google Java API
13+
Client Libraries.
14+
15+
16+
setup:
17+
- auth
18+
- install_deps
19+
20+
samples:
21+
- name: Snippets
22+
file: snippets.py
23+
24+
25+
folder: servicedirectory
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==5.3.2

servicedirectory/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-service-directory==0.1.0

servicedirectory/snippets.py

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from google.cloud import servicedirectory_v1beta1
18+
19+
20+
def create_namespace(project_id, location_id, namespace_id):
21+
"""Creates a namespace in the given location."""
22+
23+
client = servicedirectory_v1beta1.RegistrationServiceClient()
24+
25+
namespace = servicedirectory_v1beta1.Namespace(
26+
name='projects/{0}/locations/{1}/namespaces/{2}'.format(
27+
project_id, location_id, namespace_id))
28+
29+
response = client.create_namespace(
30+
parent='projects/{0}/locations/{1}'.format(project_id, location_id),
31+
namespace=namespace,
32+
namespace_id=namespace_id,
33+
metadata=[[
34+
'x-goog-request-params',
35+
'name=projects/{0}/locations/{1}/namespaces/{2}'.format(
36+
project_id, location_id, namespace_id)
37+
]])
38+
39+
print('Created namespace {0}.'.format(response.name))
40+
41+
return response
42+
43+
44+
def delete_namespace(project_id, location_id, namespace_id):
45+
"""Deletes a namespace in the given location."""
46+
47+
client = servicedirectory_v1beta1.RegistrationServiceClient()
48+
49+
namespace_name = 'projects/{0}/locations/{1}/namespaces/{2}'.format(
50+
project_id, location_id, namespace_id)
51+
52+
client.delete_namespace(
53+
name=namespace_name,
54+
metadata=[[
55+
'x-goog-request-params',
56+
'name=projects/{0}/locations/{1}/namespaces/{2}'.format(
57+
project_id, location_id, namespace_id)
58+
]])
59+
60+
print('Deleted namespace {0}.'.format(namespace_name))
61+
62+
63+
def create_service(project_id, location_id, namespace_id, service_id):
64+
"""Creates a service in the given namespace."""
65+
66+
client = servicedirectory_v1beta1.RegistrationServiceClient()
67+
68+
service = servicedirectory_v1beta1.Service(
69+
name='projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
70+
project_id, location_id, namespace_id, service_id))
71+
72+
response = client.create_service(
73+
parent='projects/{0}/locations/{1}/namespaces/{2}'.format(
74+
project_id, location_id, namespace_id),
75+
service=service,
76+
service_id=service_id,
77+
metadata=[[
78+
'x-goog-request-params',
79+
'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
80+
project_id, location_id, namespace_id, service_id)
81+
]])
82+
83+
print('Created service {0}.'.format(response.name))
84+
85+
return response
86+
87+
88+
def delete_service(project_id, location_id, namespace_id, service_id):
89+
"""Deletes a service in the given namespace."""
90+
91+
client = servicedirectory_v1beta1.RegistrationServiceClient()
92+
93+
service_name = 'projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
94+
project_id, location_id, namespace_id, service_id)
95+
96+
client.delete_service(
97+
name=service_name,
98+
metadata=[[
99+
'x-goog-request-params',
100+
'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
101+
project_id, location_id, namespace_id, service_id)
102+
]])
103+
104+
print('Deleted service {0}.'.format(service_name))
105+
106+
107+
def resolve_service(project_id, location_id, namespace_id, service_id):
108+
"""Resolves a service in the given namespace."""
109+
110+
client = servicedirectory_v1beta1.LookupServiceClient()
111+
112+
request = servicedirectory_v1beta1.ResolveServiceRequest(
113+
name='projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
114+
project_id, location_id, namespace_id, service_id))
115+
116+
response = client.resolve_service(
117+
request=request,
118+
metadata=[[
119+
'x-goog-request-params',
120+
'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
121+
project_id, location_id, namespace_id, service_id)
122+
]])
123+
124+
print('Endpoints found:')
125+
for endpoint in response.service.endpoints:
126+
print('{0} -- {1}:{2}'.format(endpoint.name, endpoint.address,
127+
endpoint.port))
128+
129+
return response
130+
131+
132+
def create_endpoint(project_id, location_id, namespace_id, service_id,
133+
endpoint_id, address, port):
134+
"""Creates a endpoint in the given service."""
135+
136+
client = servicedirectory_v1beta1.RegistrationServiceClient()
137+
138+
endpoint = servicedirectory_v1beta1.Endpoint(
139+
name='projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}'
140+
.format(project_id, location_id, namespace_id, service_id, endpoint_id),
141+
address=address,
142+
port=port)
143+
144+
response = client.create_endpoint(
145+
parent='projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
146+
project_id, location_id, namespace_id, service_id),
147+
endpoint=endpoint,
148+
endpoint_id=endpoint_id,
149+
metadata=[[
150+
'x-goog-request-params',
151+
'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}'
152+
.format(project_id, location_id, namespace_id, service_id,
153+
endpoint_id)
154+
]])
155+
156+
print('Created endpoint {0}.'.format(response.name))
157+
158+
return response
159+
160+
161+
def delete_endpoint(project_id, location_id, namespace_id, service_id,
162+
endpoint_id):
163+
"""Deletes a endpoin in the given service."""
164+
165+
client = servicedirectory_v1beta1.RegistrationServiceClient()
166+
167+
endpoint_name = 'projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}'.format(
168+
project_id, location_id, namespace_id, service_id, endpoint_id)
169+
170+
client.delete_endpoint(
171+
name=endpoint_name,
172+
metadata=[[
173+
'x-goog-request-params',
174+
'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}'
175+
.format(project_id, location_id, namespace_id, service_id,
176+
endpoint_id)
177+
]])
178+
179+
print('Deleted endpoint {0}.'.format(endpoint_name))

servicedirectory/snippets_test.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from os import environ
18+
import pytest
19+
import snippets
20+
from google.cloud import servicedirectory_v1beta1
21+
22+
PROJECT_ID = environ['GCLOUD_PROJECT']
23+
LOCATION_ID = environ['GCLOUD_LOCATION']
24+
NAMESPACE_ID = 'test-namespace'
25+
SERVICE_ID = 'test-service'
26+
ENDPOINT_ID = 'test-endpoint'
27+
ADDRESS = '1.2.3.4'
28+
PORT = 443
29+
30+
31+
def teardown_module(module):
32+
client = servicedirectory_v1beta1.RegistrationServiceClient()
33+
response = client.list_namespaces(
34+
parent='projects/{0}/locations/{1}'.format(PROJECT_ID, LOCATION_ID))
35+
for namespace in response.namespaces:
36+
client.delete_namespace(
37+
name=namespace.name,
38+
metadata=[[
39+
'x-goog-request-params',
40+
'name=projects/{0}/locations/{1}/namespaces/{2}'.format(
41+
PROJECT_ID, LOCATION_ID, namespace.name)
42+
]])
43+
44+
45+
def test_create_namespace():
46+
response = snippets.create_namespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID)
47+
48+
assert NAMESPACE_ID in response.name
49+
50+
51+
def test_create_service():
52+
response = snippets.create_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID,
53+
SERVICE_ID)
54+
55+
assert SERVICE_ID in response.name
56+
57+
58+
def test_create_endpoint():
59+
response = snippets.create_endpoint(PROJECT_ID, LOCATION_ID, NAMESPACE_ID,
60+
SERVICE_ID, ENDPOINT_ID, ADDRESS, PORT)
61+
62+
assert ENDPOINT_ID in response.name
63+
64+
65+
def test_resolve_service():
66+
response = snippets.resolve_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID,
67+
SERVICE_ID)
68+
69+
assert len(response.service.endpoints) == 1
70+
assert ENDPOINT_ID in response.service.endpoints[0].name
71+
72+
73+
def test_delete_endpoint(capsys):
74+
snippets.delete_endpoint(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID,
75+
ENDPOINT_ID)
76+
77+
out, _ = capsys.readouterr()
78+
assert ENDPOINT_ID in out
79+
80+
81+
def test_delete_service(capsys):
82+
snippets.delete_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID)
83+
84+
out, _ = capsys.readouterr()
85+
assert SERVICE_ID in out
86+
87+
88+
def test_delete_namespace(capsys):
89+
snippets.delete_namespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID)
90+
91+
out, _ = capsys.readouterr()
92+
assert NAMESPACE_ID in out

0 commit comments

Comments
 (0)