From 1490a111fe3e9cea1b1cc8afbf0d43c6521d5531 Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Thu, 16 Apr 2020 07:29:38 -0400 Subject: [PATCH 1/5] python snippets for service directory --- servicedirectory/README.rst.in | 25 +++++ servicedirectory/requirements-test.txt | 1 + servicedirectory/requirements.txt | 1 + servicedirectory/snippets.py | 138 +++++++++++++++++++++++++ servicedirectory/snippets_test.py | 86 +++++++++++++++ 5 files changed, 251 insertions(+) create mode 100644 servicedirectory/README.rst.in create mode 100644 servicedirectory/requirements-test.txt create mode 100644 servicedirectory/requirements.txt create mode 100644 servicedirectory/snippets.py create mode 100644 servicedirectory/snippets_test.py diff --git a/servicedirectory/README.rst.in b/servicedirectory/README.rst.in new file mode 100644 index 00000000000..7b786751795 --- /dev/null +++ b/servicedirectory/README.rst.in @@ -0,0 +1,25 @@ +# This file is used to generate README.rst + +product: + name: Google Cloud Service Directory + short_name: Service Directory + url: https://cloud.google.com/service-directory/docs/ + description: > + `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. + + +setup: +- auth +- install_deps + +samples: +- name: Snippets + file: snippets.py + + +folder: servicedirectory \ No newline at end of file diff --git a/servicedirectory/requirements-test.txt b/servicedirectory/requirements-test.txt new file mode 100644 index 00000000000..781d4326c94 --- /dev/null +++ b/servicedirectory/requirements-test.txt @@ -0,0 +1 @@ +pytest==5.3.2 diff --git a/servicedirectory/requirements.txt b/servicedirectory/requirements.txt new file mode 100644 index 00000000000..4da4227119e --- /dev/null +++ b/servicedirectory/requirements.txt @@ -0,0 +1 @@ +google-cloud-servicedirectory==0.1.1 diff --git a/servicedirectory/snippets.py b/servicedirectory/snippets.py new file mode 100644 index 00000000000..c0e919f2fd6 --- /dev/null +++ b/servicedirectory/snippets.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from google.cloud import servicedirectory_v1beta1 + + +def create_namespace(project_id, location_id, namespace_id): + """Creates a namespace in the given location.""" + + client = servicedirectory_v1beta1.RegistrationServiceClient() + + namespace = servicedirectory_v1beta1.Endpoint( + name='projects/{0}/locations/{1}/namespaces/{2}'.format( + project_id, location_id, namespace_id)) + + response = client.create_namespace( + parent='projects/{0}/locations/{1}'.format(project_id, location_id), + namespace=namespace, + namespace_id=namespace_id) + + print('Created namespace {0}.'.format(response.name)) + + return response + + +def delete_namespace(project_id, location_id, namespace_id): + """Deletes a namespace in the given location.""" + + client = servicedirectory_v1beta1.RegistrationServiceClient() + + namespace_name = 'projects/{0}/locations/{1}/namespaces/{2}'.format( + project_id, location_id, namespace_id) + + client.delete_namespace(name=namespace_name) + + print('Deleted namespace {0}.'.format(namespace_name)) + + +def create_service(project_id, location_id, namespace_id, service_id): + """Creates a service in the given namespace.""" + + client = servicedirectory_v1beta1.RegistrationServiceClient() + + service = servicedirectory_v1beta1.Service( + name='projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format( + project_id, location_id, namespace_id, service_id)) + + response = client.create_service( + parent='projects/{0}/locations/{1}/namespaces/{2}'.format( + project_id, location_id, namespace_id), + service=service, + service_id=service_id) + + print('Created service {0}.'.format(response.name)) + + return response + + +def delete_service(project_id, location_id, namespace_id, service_id): + """Deletes a service in the given namespace.""" + + client = servicedirectory_v1beta1.RegistrationServiceClient() + + service_name = 'projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format( + project_id, location_id, namespace_id, service_id) + + client.delete_service(name=service_name) + + print('Deleted service {0}.'.format(service_name)) + + +def resolve_service(project_id, location_id, namespace_id, service_id): + """Resolves a service in the given namespace.""" + + client = servicedirectory_v1beta1.LookupServiceClient() + + request = servicedirectory_v1beta1.ResolveServiceRequest( + name='projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format( + project_id, location_id, namespace_id, service_id)) + + response = client.resolve_service(request=request) + + print('Endpoints found:') + for endpoint in response.service.endpoints: + print('{0} -- {1}:{2}'.format(endpoint.name, endpoint.address, + endpoint.port)) + + return response + + +def create_endpoint(project_id, location_id, namespace_id, service_id, + endpoint_id, address, port): + """Creates a endpoint in the given service.""" + + client = servicedirectory_v1beta1.RegistrationServiceClient() + + endpoint = servicedirectory_v1beta1.Endpoint( + name='projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}' + .format(project_id, location_id, namespace_id, service_id, endpoint_id), + address=address, + port=port) + + response = client.create_endpoint( + parent='projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format( + project_id, location_id, namespace_id, service_id), + endpoint=endpoint, + endpoint_id=endpoint_id) + + print('Created endpoint {0}.'.format(response.name)) + + return response + + +def delete_endpoint(project_id, location_id, namespace_id, service_id, + endpoint_id): + """Deletes a endpoin in the given service.""" + + client = servicedirectory_v1beta1.RegistrationServiceClient() + + endpoint_name = 'projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}'.format( + project_id, location_id, namespace_id, service_id, endpoint_id) + + client.delete_endpoint(name=endpoint_name) + + print('Deleted endpoint {0}.'.format(endpoint_name)) diff --git a/servicedirectory/snippets_test.py b/servicedirectory/snippets_test.py new file mode 100644 index 00000000000..a78004acbfa --- /dev/null +++ b/servicedirectory/snippets_test.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import pytest +import snippets +from google.cloud import servicedirectory_v1beta1 + +PROJECT_ID = os.environ['GCLOUD_PROJECT'] +LOCATION_ID = 'us-east1' +NAMESPACE_ID = 'a-namespace' +SERVICE_ID = 'a-service' +ENDPOINT_ID = 'a-endpoint' +ADDRESS = '1.2.3.4' +PORT = 443 + + +def teardown_module(module): + client = servicedirectory_v1beta1.RegistrationServiceClient() + response = client.list_namespaces( + parent='projects/{0}/locations/{1}'.format(PROJECT_ID, LOCATION)) + for namespace in response.namespaces: + client.delete_namespace(name=namespace.name) + + +def test_create_namespace(): + response = snippets.create_namespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID) + + assert NAMESPACE_ID in response.name + + +def test_create_service(): + response = snippets.create_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, + SERVICE_ID) + + assert SERVICE_ID in response.name + + +def test_create_endpoint(): + response = snippets.create_endpoint(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, + SERVICE_ID, ENDPOINT_ID, ADDRESS, PORT) + + assert ENDPOINT_ID in response.name + + +def test_resolve_service(): + response = snippets.resolve_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, + SERVICE_ID) + + assert len(response.service.endpoints) == 1 + assert ENDPOINT_ID in response.service.endpoints[0].name + + +def test_delete_endpoint(capsys): + snippets.delete_endpoint(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, + ENDPOINT_ID) + + out, _ = capsys.readouterr() + assert ENDPOINT_ID in out + + +def test_delete_service(capsys): + snippets.delete_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID) + + out, _ = capsys.readouterr() + assert SERVICE_ID in out + + +def test_delete_namespace(capsys): + snippets.delete_namespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID) + + out, _ = capsys.readouterr() + assert NAMESPACE_ID in out From 390ee2df0ff5a7a9ef3da677d210bab4737bf124 Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Thu, 16 Apr 2020 16:57:53 -0400 Subject: [PATCH 2/5] update requirements.txt to correct version --- servicedirectory/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/servicedirectory/requirements.txt b/servicedirectory/requirements.txt index 4da4227119e..f6a6aafd184 100644 --- a/servicedirectory/requirements.txt +++ b/servicedirectory/requirements.txt @@ -1 +1 @@ -google-cloud-servicedirectory==0.1.1 +google-cloud-service-directory==0.1.0 From 29d9d19f9f4fa21de52ddefc453a23f600145473 Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Mon, 20 Apr 2020 20:48:35 -0400 Subject: [PATCH 3/5] fix location constant --- servicedirectory/snippets_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/servicedirectory/snippets_test.py b/servicedirectory/snippets_test.py index a78004acbfa..448f7d18551 100644 --- a/servicedirectory/snippets_test.py +++ b/servicedirectory/snippets_test.py @@ -31,7 +31,7 @@ def teardown_module(module): client = servicedirectory_v1beta1.RegistrationServiceClient() response = client.list_namespaces( - parent='projects/{0}/locations/{1}'.format(PROJECT_ID, LOCATION)) + parent='projects/{0}/locations/{1}'.format(PROJECT_ID, LOCATION_ID)) for namespace in response.namespaces: client.delete_namespace(name=namespace.name) From aaa8360a7e3538664d0b82ce38bbc01245553b89 Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Mon, 20 Apr 2020 21:27:22 -0400 Subject: [PATCH 4/5] limit scope of import of os, fix create namespace to not create endpoint instead --- servicedirectory/snippets.py | 2 +- servicedirectory/snippets_test.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/servicedirectory/snippets.py b/servicedirectory/snippets.py index c0e919f2fd6..ca97d8edc11 100644 --- a/servicedirectory/snippets.py +++ b/servicedirectory/snippets.py @@ -22,7 +22,7 @@ def create_namespace(project_id, location_id, namespace_id): client = servicedirectory_v1beta1.RegistrationServiceClient() - namespace = servicedirectory_v1beta1.Endpoint( + namespace = servicedirectory_v1beta1.Namespace( name='projects/{0}/locations/{1}/namespaces/{2}'.format( project_id, location_id, namespace_id)) diff --git a/servicedirectory/snippets_test.py b/servicedirectory/snippets_test.py index 448f7d18551..4d02d9042a3 100644 --- a/servicedirectory/snippets_test.py +++ b/servicedirectory/snippets_test.py @@ -14,12 +14,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os +from os import environ import pytest import snippets from google.cloud import servicedirectory_v1beta1 -PROJECT_ID = os.environ['GCLOUD_PROJECT'] +PROJECT_ID = environ['GCLOUD_PROJECT'] LOCATION_ID = 'us-east1' NAMESPACE_ID = 'a-namespace' SERVICE_ID = 'a-service' From 9995dc570ae20b9f4c080d0c9943a55b41fa99f3 Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Mon, 20 Apr 2020 22:29:42 -0400 Subject: [PATCH 5/5] add request metadata for grpc routing --- servicedirectory/snippets.py | 55 +++++++++++++++++++++++++++---- servicedirectory/snippets_test.py | 16 ++++++--- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/servicedirectory/snippets.py b/servicedirectory/snippets.py index ca97d8edc11..e75bcfeb45b 100644 --- a/servicedirectory/snippets.py +++ b/servicedirectory/snippets.py @@ -29,7 +29,12 @@ def create_namespace(project_id, location_id, namespace_id): response = client.create_namespace( parent='projects/{0}/locations/{1}'.format(project_id, location_id), namespace=namespace, - namespace_id=namespace_id) + namespace_id=namespace_id, + metadata=[[ + 'x-goog-request-params', + 'name=projects/{0}/locations/{1}/namespaces/{2}'.format( + project_id, location_id, namespace_id) + ]]) print('Created namespace {0}.'.format(response.name)) @@ -44,7 +49,13 @@ def delete_namespace(project_id, location_id, namespace_id): namespace_name = 'projects/{0}/locations/{1}/namespaces/{2}'.format( project_id, location_id, namespace_id) - client.delete_namespace(name=namespace_name) + client.delete_namespace( + name=namespace_name, + metadata=[[ + 'x-goog-request-params', + 'name=projects/{0}/locations/{1}/namespaces/{2}'.format( + project_id, location_id, namespace_id) + ]]) print('Deleted namespace {0}.'.format(namespace_name)) @@ -62,7 +73,12 @@ def create_service(project_id, location_id, namespace_id, service_id): parent='projects/{0}/locations/{1}/namespaces/{2}'.format( project_id, location_id, namespace_id), service=service, - service_id=service_id) + service_id=service_id, + metadata=[[ + 'x-goog-request-params', + 'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format( + project_id, location_id, namespace_id, service_id) + ]]) print('Created service {0}.'.format(response.name)) @@ -77,7 +93,13 @@ def delete_service(project_id, location_id, namespace_id, service_id): service_name = 'projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format( project_id, location_id, namespace_id, service_id) - client.delete_service(name=service_name) + client.delete_service( + name=service_name, + metadata=[[ + 'x-goog-request-params', + 'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format( + project_id, location_id, namespace_id, service_id) + ]]) print('Deleted service {0}.'.format(service_name)) @@ -91,7 +113,13 @@ def resolve_service(project_id, location_id, namespace_id, service_id): name='projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format( project_id, location_id, namespace_id, service_id)) - response = client.resolve_service(request=request) + response = client.resolve_service( + request=request, + metadata=[[ + 'x-goog-request-params', + 'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format( + project_id, location_id, namespace_id, service_id) + ]]) print('Endpoints found:') for endpoint in response.service.endpoints: @@ -117,7 +145,13 @@ def create_endpoint(project_id, location_id, namespace_id, service_id, parent='projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format( project_id, location_id, namespace_id, service_id), endpoint=endpoint, - endpoint_id=endpoint_id) + endpoint_id=endpoint_id, + metadata=[[ + 'x-goog-request-params', + 'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}' + .format(project_id, location_id, namespace_id, service_id, + endpoint_id) + ]]) print('Created endpoint {0}.'.format(response.name)) @@ -133,6 +167,13 @@ def delete_endpoint(project_id, location_id, namespace_id, service_id, endpoint_name = 'projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}'.format( project_id, location_id, namespace_id, service_id, endpoint_id) - client.delete_endpoint(name=endpoint_name) + client.delete_endpoint( + name=endpoint_name, + metadata=[[ + 'x-goog-request-params', + 'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}' + .format(project_id, location_id, namespace_id, service_id, + endpoint_id) + ]]) print('Deleted endpoint {0}.'.format(endpoint_name)) diff --git a/servicedirectory/snippets_test.py b/servicedirectory/snippets_test.py index 4d02d9042a3..1b239ce57bc 100644 --- a/servicedirectory/snippets_test.py +++ b/servicedirectory/snippets_test.py @@ -20,10 +20,10 @@ from google.cloud import servicedirectory_v1beta1 PROJECT_ID = environ['GCLOUD_PROJECT'] -LOCATION_ID = 'us-east1' -NAMESPACE_ID = 'a-namespace' -SERVICE_ID = 'a-service' -ENDPOINT_ID = 'a-endpoint' +LOCATION_ID = environ['GCLOUD_LOCATION'] +NAMESPACE_ID = 'test-namespace' +SERVICE_ID = 'test-service' +ENDPOINT_ID = 'test-endpoint' ADDRESS = '1.2.3.4' PORT = 443 @@ -33,7 +33,13 @@ def teardown_module(module): response = client.list_namespaces( parent='projects/{0}/locations/{1}'.format(PROJECT_ID, LOCATION_ID)) for namespace in response.namespaces: - client.delete_namespace(name=namespace.name) + client.delete_namespace( + name=namespace.name, + metadata=[[ + 'x-goog-request-params', + 'name=projects/{0}/locations/{1}/namespaces/{2}'.format( + PROJECT_ID, LOCATION_ID, namespace.name) + ]]) def test_create_namespace():