-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Service Directory python snippets #3456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
1490a11
python snippets for service directory
390ee2d
update requirements.txt to correct version
29d9d19
fix location constant
aaa8360
limit scope of import of os, fix create namespace to not create endpo…
9995dc5
add request metadata for grpc routing
80df034
Merge pull request #1 from rebeccawshaw/sd-python-doc-samples
rebeccawshaw 83b878d
Merge branch 'master' into master
rebeccawshaw 506c1db
fix: use 0.3.0 and remove extra metadata
busunkim96 2fc3917
Merge branch 'master' into master
busunkim96 33440b6
chore(deps): update dependency google-auth-oauthlib to v0.4.1 (#2797)
renovate-bot a954aa7
undo accidental changes to translate dir
busunkim96 a429af0
chore: remove extra files
busunkim96 300370a
remove extra files
busunkim96 420e8ec
take 2
busunkim96 89a40fb
remove accidentally committed translate test
busunkim96 657b38d
fix: use GOOGLE_CLOUD_PROJECT
busunkim96 cd5febe
Merge branch 'master' of https://github.com/rebeccawshaw/python-docs-…
322e70b
add quickstart files,generate README.rst, remove metadata from snippe…
fb971a6
remove README.rst file
6dedabb
Merge branch 'master' into master
rebeccawshaw ad957b8
use api for creating resource paths instead of string format
b3a9282
Merge branch 'master' of https://github.com/rebeccawshaw/python-docs-…
c3a32fe
change the region tags to be set to 'us-east1', change the namespace …
36b2da6
change test to fixture
1210541
Merge branch 'master' into master
busunkim96 8a991f5
change strings to f-strings, make namespace, service, and endpoint id…
cd2ef68
Merge branch 'master' of https://github.com/rebeccawshaw/python-docs-…
a64af6d
missed an f-string, added it to delete namespace
63e36f3
move list_namespace fixture into namespace fixture, remove unnecessar…
29d6707
Merge branch 'master' into master
rebeccawshaw f37d48d
Merge branch 'master' of https://github.com/GoogleCloudPlatform/pytho…
542910a
add generated README.rst file
e393114
add requirements to readme and regenerate the file
ca3d5b3
Merge branch 'master' into master
rebeccawshaw 40334b2
Merge branch 'master' into master
leahecole c9d52f5
fix linter errors
4b54713
Merge branch 'master' of https://github.com/rebeccawshaw/python-docs-…
807730c
Merge branch 'master' into master
rebeccawshaw 3f42d08
Merge branch 'master' into master
rebeccawshaw 6680dc8
only delete the namespace created upon teardown
083801a
Merge branch 'master' of https://github.com/rebeccawshaw/python-docs-…
7f0f27b
Merge branch 'master' into master
rebeccawshaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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) | ||
rebeccawshaw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_list_namespace(namespace): | ||
| assert namespace in quickstart.list_namespaces(PROJECT_ID, | ||
| LOCATION_ID).namespaces | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.