diff --git a/servicedirectory/README.rst b/servicedirectory/README.rst new file mode 100644 index 000000000000..c021fef673c0 --- /dev/null +++ b/servicedirectory/README.rst @@ -0,0 +1,89 @@ +.. This file is automatically generated. Do not edit this file directly. + +Google Cloud Service Directory Python Samples +=============================================================================== + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :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 + + +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. + + + + +.. _Google Cloud Service Directory: https://cloud.google.com/service-directory/docs/ + +To run the sample, you need to enable the API at: https://console.developers.google.com/apis/api/servicedirectory.googleapis.com/overview + +To run the sample, you need to have `Service Directory Admin` role. + + + + +Setup +------------------------------------------------------------------------------- + + +Authentication +++++++++++++++ + +This sample requires you to have authentication setup. Refer to the +`Authentication Getting Started Guide`_ for instructions on setting up +credentials for applications. + +.. _Authentication Getting Started Guide: + https://cloud.google.com/docs/authentication/getting-started + +Install Dependencies +++++++++++++++++++++ + +#. Clone python-docs-samples and change directory to the sample directory you want to use. + + .. code-block:: bash + + $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git + +#. 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. + + .. _Python Development Environment Setup Guide: + https://cloud.google.com/python/setup + +#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. + + .. code-block:: bash + + $ virtualenv env + $ source env/bin/activate + +#. Install the dependencies needed to run the samples. + + .. code-block:: bash + + $ pip install -r requirements.txt + +.. _pip: https://pip.pypa.io/ +.. _virtualenv: https://virtualenv.pypa.io/ + +Samples +------------------------------------------------------------------------------- + +Snippets ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :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 + + + + +To run this sample: + +.. code-block:: bash + + $ python snippets.py + + + + +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/servicedirectory/README.rst.in b/servicedirectory/README.rst.in new file mode 100644 index 000000000000..0ed61d222cca --- /dev/null +++ b/servicedirectory/README.rst.in @@ -0,0 +1,28 @@ +# 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. + + +required_api_url: https://console.developers.google.com/apis/api/servicedirectory.googleapis.com/overview +required_role: Service Directory Admin + +setup: +- auth +- install_deps + +samples: +- name: Snippets + file: snippets.py + + +folder: servicedirectory \ No newline at end of file diff --git a/servicedirectory/quickstart.py b/servicedirectory/quickstart.py new file mode 100644 index 000000000000..c6ccfbc7fc0e --- /dev/null +++ b/servicedirectory/quickstart.py @@ -0,0 +1,32 @@ +#!/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 list_namespaces(project_id, location_id): + """Lists all namespaces in the given location.""" + + client = servicedirectory_v1beta1.RegistrationServiceClient() + + response = client.list_namespaces( + parent=f'projects/{project_id}/locations/{location_id}') + + print(f'Listed namespaces in {location_id}.') + for namespace in response: + print(f'Namespace: {namespace.name}') + + return response diff --git a/servicedirectory/quickstart_test.py b/servicedirectory/quickstart_test.py new file mode 100644 index 000000000000..b17540e2636b --- /dev/null +++ b/servicedirectory/quickstart_test.py @@ -0,0 +1,54 @@ +#!/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 os import environ +import uuid + +from google.cloud import servicedirectory_v1beta1 + +import pytest + +import quickstart + +PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] +LOCATION_ID = 'us-east1' +NAMESPACE_ID = f'test-namespace-{uuid.uuid4().hex}' + + +@pytest.fixture(scope='module') +def client(): + return servicedirectory_v1beta1.RegistrationServiceClient() + + +@pytest.fixture(scope='module') +def namespace(client): + namespace = servicedirectory_v1beta1.Namespace( + name=client.namespace_path(PROJECT_ID, LOCATION_ID, NAMESPACE_ID)) + + client.create_namespace( + parent=f'projects/{PROJECT_ID}/locations/{LOCATION_ID}', + namespace=namespace, + namespace_id=NAMESPACE_ID, + ) + + yield namespace + + client.delete_namespace(name=namespace.name) + + +def test_list_namespace(namespace): + assert namespace in quickstart.list_namespaces(PROJECT_ID, + LOCATION_ID).namespaces diff --git a/servicedirectory/requirements-test.txt b/servicedirectory/requirements-test.txt new file mode 100644 index 000000000000..79738af5f268 --- /dev/null +++ b/servicedirectory/requirements-test.txt @@ -0,0 +1 @@ +pytest==5.4.3 diff --git a/servicedirectory/requirements.txt b/servicedirectory/requirements.txt new file mode 100644 index 000000000000..11a61a975176 --- /dev/null +++ b/servicedirectory/requirements.txt @@ -0,0 +1 @@ +google-cloud-service-directory==0.3.0 diff --git a/servicedirectory/snippets.py b/servicedirectory/snippets.py new file mode 100644 index 000000000000..1bb17eb6ea1a --- /dev/null +++ b/servicedirectory/snippets.py @@ -0,0 +1,137 @@ +#!/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.Namespace( + name=client.namespace_path(project_id, location_id, namespace_id)) + + response = client.create_namespace( + parent=f'projects/{project_id}/locations/{location_id}', + namespace=namespace, + namespace_id=namespace_id, + ) + + print(f'Created namespace {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 = client.namespace_path(project_id, location_id, namespace_id) + + client.delete_namespace(name=namespace_name) + + print(f'Deleted namespace {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=client.service_path(project_id, location_id, namespace_id, + service_id)) + + response = client.create_service( + parent=client.namespace_path(project_id, location_id, namespace_id), + service=service, + service_id=service_id, + ) + + print(f'Created service {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 = client.service_path(project_id, location_id, namespace_id, + service_id) + + client.delete_service(name=service_name) + + print(f'Deleted service {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=servicedirectory_v1beta1.RegistrationServiceClient().service_path( + project_id, location_id, namespace_id, service_id)) + + response = client.resolve_service(request=request) + + print('Endpoints found:') + for endpoint in response.service.endpoints: + print(f'{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=client.endpoint_path(project_id, location_id, namespace_id, + service_id, endpoint_id), + address=address, + port=port) + + response = client.create_endpoint( + parent=client.service_path(project_id, location_id, namespace_id, + service_id), + endpoint=endpoint, + endpoint_id=endpoint_id, + ) + + print(f'Created endpoint {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 = client.endpoint_path(project_id, location_id, namespace_id, + service_id, endpoint_id) + + client.delete_endpoint(name=endpoint_name) + + print(f'Deleted endpoint {endpoint_name}.') diff --git a/servicedirectory/snippets_test.py b/servicedirectory/snippets_test.py new file mode 100644 index 000000000000..f9891b047766 --- /dev/null +++ b/servicedirectory/snippets_test.py @@ -0,0 +1,91 @@ +#!/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 os import environ +import uuid + +from google.api_core import exceptions +from google.cloud import servicedirectory_v1beta1 + +import snippets + +PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] +LOCATION_ID = 'us-east1' +NAMESPACE_ID = f'test-namespace-{uuid.uuid4().hex}' +SERVICE_ID = f'test-service-{uuid.uuid4().hex}' +ENDPOINT_ID = f'test-endpoint-{uuid.uuid4().hex}' +ADDRESS = '1.2.3.4' +PORT = 443 + + +def teardown_module(): + client = servicedirectory_v1beta1.RegistrationServiceClient() + namespace_name = client.namespace_path(PROJECT_ID, LOCATION_ID, NAMESPACE_ID) + try: + namespace = client.get_namespace(name=namespace_name) + client.delete_namespace(name=namespace.name) + except exceptions.NotFound: + pass + + +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