Skip to content

Commit 6e83486

Browse files
rebeccawshawRebecca Shawbusunkim96renovate-botleahecole
authored
Service Directory python snippets (#3456)
* python snippets for service directory * update requirements.txt to correct version * fix location constant * limit scope of import of os, fix create namespace to not create endpoint instead * add request metadata for grpc routing * fix: use 0.3.0 and remove extra metadata * chore(deps): update dependency google-auth-oauthlib to v0.4.1 (#2797) * chore(deps): update dependency google-auth-oauthlib to v0.4.1 * resolve dependency finding errors * fix new matplotlib error Co-authored-by: Leah E. Cole <[email protected]> Co-authored-by: Leah Cole <[email protected]> Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: Christopher Wilcox <[email protected]> * undo accidental changes to translate dir * chore: remove extra files * remove extra files * take 2 * remove accidentally committed translate test * fix: use GOOGLE_CLOUD_PROJECT * add quickstart files,generate README.rst, remove metadata from snippets_test * remove README.rst file * use api for creating resource paths instead of string format * change the region tags to be set to 'us-east1', change the namespace test to be a fixture * change test to fixture * change strings to f-strings, make namespace, service, and endpoint ids unique * missed an f-string, added it to delete namespace * move list_namespace fixture into namespace fixture, remove unnecessary import * add generated README.rst file * add requirements to readme and regenerate the file * fix linter errors * only delete the namespace created upon teardown Co-authored-by: Rebecca Shaw <[email protected]> Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: WhiteSource Renovate <[email protected]> Co-authored-by: Leah E. Cole <[email protected]> Co-authored-by: Leah Cole <[email protected]> Co-authored-by: Christopher Wilcox <[email protected]>
1 parent a094a21 commit 6e83486

File tree

8 files changed

+433
-0
lines changed

8 files changed

+433
-0
lines changed

servicedirectory/README.rst

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Service Directory Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=servicedirectory/README.rst
8+
9+
10+
This directory contains samples for Google Cloud Service Directory. `Google Cloud Service Directory`_ is a platform for discovering, publishing, and connecting services. It offers customers a single place to register and discover their services in a consistent and reliable way, regardless of their environment. These sample Java applications demonstrate how to access the Service Directory API using the Google Java API Client Libraries.
11+
12+
13+
14+
15+
.. _Google Cloud Service Directory: https://cloud.google.com/service-directory/docs/
16+
17+
To run the sample, you need to enable the API at: https://console.developers.google.com/apis/api/servicedirectory.googleapis.com/overview
18+
19+
To run the sample, you need to have `Service Directory Admin` role.
20+
21+
22+
23+
24+
Setup
25+
-------------------------------------------------------------------------------
26+
27+
28+
Authentication
29+
++++++++++++++
30+
31+
This sample requires you to have authentication setup. Refer to the
32+
`Authentication Getting Started Guide`_ for instructions on setting up
33+
credentials for applications.
34+
35+
.. _Authentication Getting Started Guide:
36+
https://cloud.google.com/docs/authentication/getting-started
37+
38+
Install Dependencies
39+
++++++++++++++++++++
40+
41+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
42+
43+
.. code-block:: bash
44+
45+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
46+
47+
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.
48+
49+
.. _Python Development Environment Setup Guide:
50+
https://cloud.google.com/python/setup
51+
52+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
53+
54+
.. code-block:: bash
55+
56+
$ virtualenv env
57+
$ source env/bin/activate
58+
59+
#. Install the dependencies needed to run the samples.
60+
61+
.. code-block:: bash
62+
63+
$ pip install -r requirements.txt
64+
65+
.. _pip: https://pip.pypa.io/
66+
.. _virtualenv: https://virtualenv.pypa.io/
67+
68+
Samples
69+
-------------------------------------------------------------------------------
70+
71+
Snippets
72+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
73+
74+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
75+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=servicedirectory/snippets.py,servicedirectory/README.rst
76+
77+
78+
79+
80+
To run this sample:
81+
82+
.. code-block:: bash
83+
84+
$ python snippets.py
85+
86+
87+
88+
89+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

servicedirectory/README.rst.in

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
required_api_url: https://console.developers.google.com/apis/api/servicedirectory.googleapis.com/overview
17+
required_role: Service Directory Admin
18+
19+
setup:
20+
- auth
21+
- install_deps
22+
23+
samples:
24+
- name: Snippets
25+
file: snippets.py
26+
27+
28+
folder: servicedirectory

servicedirectory/quickstart.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 list_namespaces(project_id, location_id):
21+
"""Lists all namespaces in the given location."""
22+
23+
client = servicedirectory_v1beta1.RegistrationServiceClient()
24+
25+
response = client.list_namespaces(
26+
parent=f'projects/{project_id}/locations/{location_id}')
27+
28+
print(f'Listed namespaces in {location_id}.')
29+
for namespace in response:
30+
print(f'Namespace: {namespace.name}')
31+
32+
return response

servicedirectory/quickstart_test.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 uuid
19+
20+
from google.cloud import servicedirectory_v1beta1
21+
22+
import pytest
23+
24+
import quickstart
25+
26+
PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT']
27+
LOCATION_ID = 'us-east1'
28+
NAMESPACE_ID = f'test-namespace-{uuid.uuid4().hex}'
29+
30+
31+
@pytest.fixture(scope='module')
32+
def client():
33+
return servicedirectory_v1beta1.RegistrationServiceClient()
34+
35+
36+
@pytest.fixture(scope='module')
37+
def namespace(client):
38+
namespace = servicedirectory_v1beta1.Namespace(
39+
name=client.namespace_path(PROJECT_ID, LOCATION_ID, NAMESPACE_ID))
40+
41+
client.create_namespace(
42+
parent=f'projects/{PROJECT_ID}/locations/{LOCATION_ID}',
43+
namespace=namespace,
44+
namespace_id=NAMESPACE_ID,
45+
)
46+
47+
yield namespace
48+
49+
client.delete_namespace(name=namespace.name)
50+
51+
52+
def test_list_namespace(namespace):
53+
assert namespace in quickstart.list_namespaces(PROJECT_ID,
54+
LOCATION_ID).namespaces
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==5.4.3

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.3.0

servicedirectory/snippets.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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=client.namespace_path(project_id, location_id, namespace_id))
27+
28+
response = client.create_namespace(
29+
parent=f'projects/{project_id}/locations/{location_id}',
30+
namespace=namespace,
31+
namespace_id=namespace_id,
32+
)
33+
34+
print(f'Created namespace {response.name}.')
35+
36+
return response
37+
38+
39+
def delete_namespace(project_id, location_id, namespace_id):
40+
"""Deletes a namespace in the given location."""
41+
42+
client = servicedirectory_v1beta1.RegistrationServiceClient()
43+
44+
namespace_name = client.namespace_path(project_id, location_id, namespace_id)
45+
46+
client.delete_namespace(name=namespace_name)
47+
48+
print(f'Deleted namespace {namespace_name}.')
49+
50+
51+
def create_service(project_id, location_id, namespace_id, service_id):
52+
"""Creates a service in the given namespace."""
53+
54+
client = servicedirectory_v1beta1.RegistrationServiceClient()
55+
56+
service = servicedirectory_v1beta1.Service(
57+
name=client.service_path(project_id, location_id, namespace_id,
58+
service_id))
59+
60+
response = client.create_service(
61+
parent=client.namespace_path(project_id, location_id, namespace_id),
62+
service=service,
63+
service_id=service_id,
64+
)
65+
66+
print(f'Created service {response.name}.')
67+
68+
return response
69+
70+
71+
def delete_service(project_id, location_id, namespace_id, service_id):
72+
"""Deletes a service in the given namespace."""
73+
74+
client = servicedirectory_v1beta1.RegistrationServiceClient()
75+
76+
service_name = client.service_path(project_id, location_id, namespace_id,
77+
service_id)
78+
79+
client.delete_service(name=service_name)
80+
81+
print(f'Deleted service {service_name}.')
82+
83+
84+
def resolve_service(project_id, location_id, namespace_id, service_id):
85+
"""Resolves a service in the given namespace."""
86+
87+
client = servicedirectory_v1beta1.LookupServiceClient()
88+
89+
request = servicedirectory_v1beta1.ResolveServiceRequest(
90+
name=servicedirectory_v1beta1.RegistrationServiceClient().service_path(
91+
project_id, location_id, namespace_id, service_id))
92+
93+
response = client.resolve_service(request=request)
94+
95+
print('Endpoints found:')
96+
for endpoint in response.service.endpoints:
97+
print(f'{endpoint.name} -- {endpoint.address}:{endpoint.port}')
98+
99+
return response
100+
101+
102+
def create_endpoint(project_id, location_id, namespace_id, service_id,
103+
endpoint_id, address, port):
104+
"""Creates a endpoint in the given service."""
105+
106+
client = servicedirectory_v1beta1.RegistrationServiceClient()
107+
108+
endpoint = servicedirectory_v1beta1.Endpoint(
109+
name=client.endpoint_path(project_id, location_id, namespace_id,
110+
service_id, endpoint_id),
111+
address=address,
112+
port=port)
113+
114+
response = client.create_endpoint(
115+
parent=client.service_path(project_id, location_id, namespace_id,
116+
service_id),
117+
endpoint=endpoint,
118+
endpoint_id=endpoint_id,
119+
)
120+
121+
print(f'Created endpoint {response.name}.')
122+
123+
return response
124+
125+
126+
def delete_endpoint(project_id, location_id, namespace_id, service_id,
127+
endpoint_id):
128+
"""Deletes a endpoin in the given service."""
129+
130+
client = servicedirectory_v1beta1.RegistrationServiceClient()
131+
132+
endpoint_name = client.endpoint_path(project_id, location_id, namespace_id,
133+
service_id, endpoint_id)
134+
135+
client.delete_endpoint(name=endpoint_name)
136+
137+
print(f'Deleted endpoint {endpoint_name}.')

0 commit comments

Comments
 (0)