-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
a094a21
commit 6e83486
Showing
8 changed files
with
433 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pytest==5.4.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
google-cloud-service-directory==0.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}.') |
Oops, something went wrong.