From 1490a111fe3e9cea1b1cc8afbf0d43c6521d5531 Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Thu, 16 Apr 2020 07:29:38 -0400 Subject: [PATCH 01/25] 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 000000000000..7b786751795a --- /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 000000000000..781d4326c947 --- /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 000000000000..4da4227119e5 --- /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 000000000000..c0e919f2fd60 --- /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 000000000000..a78004acbfa0 --- /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 02/25] 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 4da4227119e5..f6a6aafd1840 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 03/25] 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 a78004acbfa0..448f7d185516 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 04/25] 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 c0e919f2fd60..ca97d8edc11d 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 448f7d185516..4d02d9042a3a 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 05/25] 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 ca97d8edc11d..e75bcfeb45b1 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 4d02d9042a3a..1b239ce57bcc 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(): From 506c1dbd8ca54f9427c98e71e1cbdc8a97631745 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Thu, 11 Jun 2020 20:26:33 +0000 Subject: [PATCH 06/25] fix: use 0.3.0 and remove extra metadata --- servicedirectory/requirements.txt | 2 +- servicedirectory/snippets.py | 52 +--- .../cloud-client/hybrid_glossaries/noxfile.py | 225 ++++++++++++++++++ translate/cloud-client/noxfile.py | 225 ++++++++++++++++++ 4 files changed, 458 insertions(+), 46 deletions(-) create mode 100644 translate/cloud-client/hybrid_glossaries/noxfile.py create mode 100644 translate/cloud-client/noxfile.py diff --git a/servicedirectory/requirements.txt b/servicedirectory/requirements.txt index f6a6aafd1840..11a61a975176 100644 --- a/servicedirectory/requirements.txt +++ b/servicedirectory/requirements.txt @@ -1 +1 @@ -google-cloud-service-directory==0.1.0 +google-cloud-service-directory==0.3.0 diff --git a/servicedirectory/snippets.py b/servicedirectory/snippets.py index e75bcfeb45b1..a55dcf7b36a4 100644 --- a/servicedirectory/snippets.py +++ b/servicedirectory/snippets.py @@ -30,11 +30,7 @@ def create_namespace(project_id, location_id, namespace_id): parent='projects/{0}/locations/{1}'.format(project_id, location_id), namespace=namespace, 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)) @@ -49,13 +45,7 @@ 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, - metadata=[[ - 'x-goog-request-params', - '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)) @@ -74,11 +64,7 @@ def create_service(project_id, location_id, namespace_id, service_id): project_id, location_id, namespace_id), service=service, 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)) @@ -93,13 +79,7 @@ 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, - metadata=[[ - 'x-goog-request-params', - '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)) @@ -113,13 +93,7 @@ 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, - metadata=[[ - 'x-goog-request-params', - '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: @@ -146,12 +120,7 @@ def create_endpoint(project_id, location_id, namespace_id, service_id, project_id, location_id, namespace_id, service_id), endpoint=endpoint, 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)) @@ -167,13 +136,6 @@ 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, - 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) - ]]) + client.delete_endpoint(name=endpoint_name) print('Deleted endpoint {0}.'.format(endpoint_name)) diff --git a/translate/cloud-client/hybrid_glossaries/noxfile.py b/translate/cloud-client/hybrid_glossaries/noxfile.py new file mode 100644 index 000000000000..b23055f14a65 --- /dev/null +++ b/translate/cloud-client/hybrid_glossaries/noxfile.py @@ -0,0 +1,225 @@ +# Copyright 2019 Google LLC +# +# 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 __future__ import print_function + +import os +from pathlib import Path +import sys + +import nox + + +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING +# DO NOT EDIT THIS FILE EVER! +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING + +# Copy `noxfile_config.py` to your directory and modify it instead. + + +# `TEST_CONFIG` dict is a configuration hook that allows users to +# modify the test configurations. The values here should be in sync +# with `noxfile_config.py`. Users will copy `noxfile_config.py` into +# their directory and modify it. + +TEST_CONFIG = { + # You can opt out from the test for specific Python versions. + 'ignored_versions': ["2.7"], + + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. + 'gcloud_project_env': 'GCLOUD_PROJECT', + # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', + + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + 'envs': {}, +} + + +try: + # Ensure we can import noxfile_config in the project's directory. + sys.path.append('.') + from noxfile_config import TEST_CONFIG_OVERRIDE +except ImportError as e: + print("No user noxfile_config found: detail: {}".format(e)) + TEST_CONFIG_OVERRIDE = {} + +# Update the TEST_CONFIG with the user supplied values. +TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) + + +def get_pytest_env_vars(): + """Returns a dict for pytest invocation.""" + ret = {} + + # Override the GCLOUD_PROJECT and the alias. + env_key = TEST_CONFIG['gcloud_project_env'] + # This should error out if not set. + ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key] + ret['GCLOUD_PROJECT'] = os.environ[env_key] + + # Apply user supplied envs. + ret.update(TEST_CONFIG['envs']) + return ret + + +# DO NOT EDIT - automatically generated. +# All versions used to tested samples. +ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"] + +# Any default versions that should be ignored. +IGNORED_VERSIONS = TEST_CONFIG['ignored_versions'] + +TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) + +INSTALL_LIBRARY_FROM_SOURCE = bool(os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False)) +# +# Style Checks +# + + +def _determine_local_import_names(start_dir): + """Determines all import names that should be considered "local". + + This is used when running the linter to insure that import order is + properly checked. + """ + file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] + return [ + basename + for basename, extension in file_ext_pairs + if extension == ".py" + or os.path.isdir(os.path.join(start_dir, basename)) + and basename not in ("__pycache__") + ] + + +# Linting with flake8. +# +# We ignore the following rules: +# E203: whitespace before ‘:’ +# E266: too many leading ‘#’ for block comment +# E501: line too long +# I202: Additional newline in a section of imports +# +# We also need to specify the rules which are ignored by default: +# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] +FLAKE8_COMMON_ARGS = [ + "--show-source", + "--builtin=gettext", + "--max-complexity=20", + "--import-order-style=google", + "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", + "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202", + "--max-line-length=88", +] + + +@nox.session +def lint(session): + session.install("flake8", "flake8-import-order") + + local_names = _determine_local_import_names(".") + args = FLAKE8_COMMON_ARGS + [ + "--application-import-names", + ",".join(local_names), + "." + ] + session.run("flake8", *args) + + +# +# Sample Tests +# + + +PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] + + +def _session_tests(session, post_install=None): + """Runs py.test for a particular project.""" + if os.path.exists("requirements.txt"): + session.install("-r", "requirements.txt") + + if os.path.exists("requirements-test.txt"): + session.install("-r", "requirements-test.txt") + + if INSTALL_LIBRARY_FROM_SOURCE: + session.install("-e", _get_repo_root()) + + if post_install: + post_install(session) + + session.run( + "pytest", + *(PYTEST_COMMON_ARGS + session.posargs), + # Pytest will return 5 when no tests are collected. This can happen + # on travis where slow and flaky tests are excluded. + # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html + success_codes=[0, 5], + env=get_pytest_env_vars() + ) + + +@nox.session(python=ALL_VERSIONS) +def py(session): + """Runs py.test for a sample using the specified version of Python.""" + if session.python in TESTED_VERSIONS: + _session_tests(session) + else: + session.skip("SKIPPED: {} tests are disabled for this sample.".format( + session.python + )) + + +# +# Readmegen +# + + +def _get_repo_root(): + """ Returns the root folder of the project. """ + # Get root of this repository. Assume we don't have directories nested deeper than 10 items. + p = Path(os.getcwd()) + for i in range(10): + if p is None: + break + if Path(p / ".git").exists(): + return str(p) + p = p.parent + raise Exception("Unable to detect repository root.") + + +GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) + + +@nox.session +@nox.parametrize("path", GENERATED_READMES) +def readmegen(session, path): + """(Re-)generates the readme for a sample.""" + session.install("jinja2", "pyyaml") + dir_ = os.path.dirname(path) + + if os.path.exists(os.path.join(dir_, "requirements.txt")): + session.install("-r", os.path.join(dir_, "requirements.txt")) + + in_file = os.path.join(dir_, "README.rst.in") + session.run( + "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file + ) diff --git a/translate/cloud-client/noxfile.py b/translate/cloud-client/noxfile.py new file mode 100644 index 000000000000..b23055f14a65 --- /dev/null +++ b/translate/cloud-client/noxfile.py @@ -0,0 +1,225 @@ +# Copyright 2019 Google LLC +# +# 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 __future__ import print_function + +import os +from pathlib import Path +import sys + +import nox + + +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING +# DO NOT EDIT THIS FILE EVER! +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING + +# Copy `noxfile_config.py` to your directory and modify it instead. + + +# `TEST_CONFIG` dict is a configuration hook that allows users to +# modify the test configurations. The values here should be in sync +# with `noxfile_config.py`. Users will copy `noxfile_config.py` into +# their directory and modify it. + +TEST_CONFIG = { + # You can opt out from the test for specific Python versions. + 'ignored_versions': ["2.7"], + + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. + 'gcloud_project_env': 'GCLOUD_PROJECT', + # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', + + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + 'envs': {}, +} + + +try: + # Ensure we can import noxfile_config in the project's directory. + sys.path.append('.') + from noxfile_config import TEST_CONFIG_OVERRIDE +except ImportError as e: + print("No user noxfile_config found: detail: {}".format(e)) + TEST_CONFIG_OVERRIDE = {} + +# Update the TEST_CONFIG with the user supplied values. +TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) + + +def get_pytest_env_vars(): + """Returns a dict for pytest invocation.""" + ret = {} + + # Override the GCLOUD_PROJECT and the alias. + env_key = TEST_CONFIG['gcloud_project_env'] + # This should error out if not set. + ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key] + ret['GCLOUD_PROJECT'] = os.environ[env_key] + + # Apply user supplied envs. + ret.update(TEST_CONFIG['envs']) + return ret + + +# DO NOT EDIT - automatically generated. +# All versions used to tested samples. +ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"] + +# Any default versions that should be ignored. +IGNORED_VERSIONS = TEST_CONFIG['ignored_versions'] + +TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) + +INSTALL_LIBRARY_FROM_SOURCE = bool(os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False)) +# +# Style Checks +# + + +def _determine_local_import_names(start_dir): + """Determines all import names that should be considered "local". + + This is used when running the linter to insure that import order is + properly checked. + """ + file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] + return [ + basename + for basename, extension in file_ext_pairs + if extension == ".py" + or os.path.isdir(os.path.join(start_dir, basename)) + and basename not in ("__pycache__") + ] + + +# Linting with flake8. +# +# We ignore the following rules: +# E203: whitespace before ‘:’ +# E266: too many leading ‘#’ for block comment +# E501: line too long +# I202: Additional newline in a section of imports +# +# We also need to specify the rules which are ignored by default: +# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] +FLAKE8_COMMON_ARGS = [ + "--show-source", + "--builtin=gettext", + "--max-complexity=20", + "--import-order-style=google", + "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", + "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202", + "--max-line-length=88", +] + + +@nox.session +def lint(session): + session.install("flake8", "flake8-import-order") + + local_names = _determine_local_import_names(".") + args = FLAKE8_COMMON_ARGS + [ + "--application-import-names", + ",".join(local_names), + "." + ] + session.run("flake8", *args) + + +# +# Sample Tests +# + + +PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] + + +def _session_tests(session, post_install=None): + """Runs py.test for a particular project.""" + if os.path.exists("requirements.txt"): + session.install("-r", "requirements.txt") + + if os.path.exists("requirements-test.txt"): + session.install("-r", "requirements-test.txt") + + if INSTALL_LIBRARY_FROM_SOURCE: + session.install("-e", _get_repo_root()) + + if post_install: + post_install(session) + + session.run( + "pytest", + *(PYTEST_COMMON_ARGS + session.posargs), + # Pytest will return 5 when no tests are collected. This can happen + # on travis where slow and flaky tests are excluded. + # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html + success_codes=[0, 5], + env=get_pytest_env_vars() + ) + + +@nox.session(python=ALL_VERSIONS) +def py(session): + """Runs py.test for a sample using the specified version of Python.""" + if session.python in TESTED_VERSIONS: + _session_tests(session) + else: + session.skip("SKIPPED: {} tests are disabled for this sample.".format( + session.python + )) + + +# +# Readmegen +# + + +def _get_repo_root(): + """ Returns the root folder of the project. """ + # Get root of this repository. Assume we don't have directories nested deeper than 10 items. + p = Path(os.getcwd()) + for i in range(10): + if p is None: + break + if Path(p / ".git").exists(): + return str(p) + p = p.parent + raise Exception("Unable to detect repository root.") + + +GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) + + +@nox.session +@nox.parametrize("path", GENERATED_READMES) +def readmegen(session, path): + """(Re-)generates the readme for a sample.""" + session.install("jinja2", "pyyaml") + dir_ = os.path.dirname(path) + + if os.path.exists(os.path.join(dir_, "requirements.txt")): + session.install("-r", os.path.join(dir_, "requirements.txt")) + + in_file = os.path.join(dir_, "README.rst.in") + session.run( + "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file + ) From 33440b66fd52a46975bb880c5816894cce817164 Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Sat, 18 Apr 2020 07:47:33 +0200 Subject: [PATCH 07/25] 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 <6719667+leahecole@users.noreply.github.com> Co-authored-by: Leah Cole Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Co-authored-by: Christopher Wilcox From a954aa763e1c85902973931ff6f30e9799248009 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Thu, 11 Jun 2020 20:32:03 +0000 Subject: [PATCH 08/25] undo accidental changes to translate dir --- translate/automl/dataset_test.py | 2 +- translate/automl/model_test.py | 2 +- translate/automl/predict_test.py | 2 +- ...v3_batch_translate_text_with_model_test.py | 4 +- translate/cloud-client/beta_snippets_test.py | 4 +- .../hybrid_glossaries/hybrid_tutorial.py | 21 ++-- .../hybrid_tutorial_tests.py | 114 ++++++++++++++++++ .../hybrid_glossaries/requirements.txt | 2 +- .../resources/non_standard_format.txt | 2 +- translate/cloud-client/snippets.py | 2 +- .../translate_v3_batch_translate_text_test.py | 2 +- ...batch_translate_text_with_glossary_test.py | 4 +- .../translate_v3_create_glossary_test.py | 2 +- .../translate_v3_delete_glossary_test.py | 2 +- .../translate_v3_detect_language_test.py | 2 +- .../translate_v3_get_glossary_test.py | 2 +- ...anslate_v3_get_supported_languages_test.py | 2 +- ...et_supported_languages_with_target_test.py | 2 +- .../translate_v3_list_glossary_test.py | 2 +- .../translate_v3_translate_text_test.py | 2 +- ...te_v3_translate_text_with_glossary_test.py | 2 +- ...slate_v3_translate_text_with_model_test.py | 2 +- 22 files changed, 144 insertions(+), 37 deletions(-) create mode 100644 translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py diff --git a/translate/automl/dataset_test.py b/translate/automl/dataset_test.py index 4430ec5400e0..29e3e5c9fe97 100644 --- a/translate/automl/dataset_test.py +++ b/translate/automl/dataset_test.py @@ -21,7 +21,7 @@ import automl_translation_dataset -project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +project_id = os.environ["GCLOUD_PROJECT"] compute_region = "us-central1" diff --git a/translate/automl/model_test.py b/translate/automl/model_test.py index e19a50ea7376..0d37a85c6740 100644 --- a/translate/automl/model_test.py +++ b/translate/automl/model_test.py @@ -22,7 +22,7 @@ import automl_translation_model -project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +project_id = os.environ["GCLOUD_PROJECT"] compute_region = "us-central1" diff --git a/translate/automl/predict_test.py b/translate/automl/predict_test.py index d00a4658d7bb..f9d98dfb053c 100644 --- a/translate/automl/predict_test.py +++ b/translate/automl/predict_test.py @@ -18,7 +18,7 @@ import automl_translation_predict -project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +project_id = os.environ["GCLOUD_PROJECT"] compute_region = "us-central1" diff --git a/translate/automl/translate_v3_batch_translate_text_with_model_test.py b/translate/automl/translate_v3_batch_translate_text_with_model_test.py index 4d0def04df3e..36c52ae22e4d 100644 --- a/translate/automl/translate_v3_batch_translate_text_with_model_test.py +++ b/translate/automl/translate_v3_batch_translate_text_with_model_test.py @@ -21,14 +21,14 @@ import translate_v3_batch_translate_text_with_model -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] MODEL_ID = "TRL3128559826197068699" @pytest.fixture(scope="function") def bucket(): """Create a temporary bucket to store annotation output.""" - bucket_name = f'tmp-{uuid.uuid4().hex}' + bucket_name = str(uuid.uuid1()) storage_client = storage.Client() bucket = storage_client.create_bucket(bucket_name) diff --git a/translate/cloud-client/beta_snippets_test.py b/translate/cloud-client/beta_snippets_test.py index a42ab08ce3fc..4852168567ac 100644 --- a/translate/cloud-client/beta_snippets_test.py +++ b/translate/cloud-client/beta_snippets_test.py @@ -22,13 +22,13 @@ import beta_snippets -PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] +PROJECT_ID = os.environ['GCLOUD_PROJECT'] @pytest.fixture(scope='function') def bucket(): """Create a temporary bucket to store annotation output.""" - bucket_name = f'tmp-{uuid.uuid4().hex}' + bucket_name = str(uuid.uuid1()) storage_client = storage.Client() bucket = storage_client.create_bucket(bucket_name) diff --git a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py index 7d36174b4766..0da84b722078 100644 --- a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py +++ b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py @@ -28,7 +28,7 @@ # [START translate_hybrid_project_id] # extract GCP project id -PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] +PROJECT_ID = os.environ['GCLOUD_PROJECT'] # [END translate_hybrid_project_id] @@ -193,28 +193,21 @@ def text_to_speech(text, outfile): client = texttospeech.TextToSpeechClient() # Sets the text input to be synthesized - synthesis_input = texttospeech.SynthesisInput(ssml=ssml) + synthesis_input = texttospeech.types.SynthesisInput(ssml=ssml) # Builds the voice request, selects the language code ("en-US") and # the SSML voice gender ("MALE") - voice = texttospeech.VoiceSelectionParams( + voice = texttospeech.types.VoiceSelectionParams( language_code='en-US', - ssml_gender=texttospeech.SsmlVoiceGender.MALE) + ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE) # Selects the type of audio file to return - audio_config = texttospeech.AudioConfig( - audio_encoding=texttospeech.AudioEncoding.MP3) + audio_config = texttospeech.types.AudioConfig( + audio_encoding=texttospeech.enums.AudioEncoding.MP3) # Performs the text-to-speech request on the text input with the selected # voice parameters and audio file type - - request = texttospeech.SynthesizeSpeechRequest( - input=synthesis_input, - voice=voice, - audio_config=audio_config - ) - - response = client.synthesize_speech(request=request) + response = client.synthesize_speech(synthesis_input, voice, audio_config) # Writes the synthetic audio to the output file. with open(outfile, 'wb') as out: diff --git a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py new file mode 100644 index 000000000000..5224ca524a91 --- /dev/null +++ b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py @@ -0,0 +1,114 @@ +# Copyright 2019 Google LLC +# +# 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 sys + +from hybrid_tutorial import create_glossary +from hybrid_tutorial import pic_to_text +from hybrid_tutorial import text_to_speech +from hybrid_tutorial import translate_text + + +PROJECT_ID = os.environ['GCLOUD_PROJECT'] + + +# VISION TESTS + + +def test_vision_standard_format(): + + expected_text = 'This is\na test!\n' + alt_expected_text = 'This\nis\na test!\n' + + # Generate text using Vision API + text = pic_to_text('resources/standard_format.jpeg') + + assert (text == expected_text) or (text == alt_expected_text) + + +def test_vision_non_standard_format(): + + # Generate text + text = pic_to_text('resources/non_standard_format.png') + + # Read expected text + with open('resources/non_standard_format.txt') as f: + expected_text = f.read() + + assert text == expected_text + + +# TRANSLATE TESTS + + +def test_create_and_delete_glossary(): + sys.path.insert(1, '../') + from beta_snippets import delete_glossary + + languages = ['fr', 'en'] + glossary_name = 'test-glossary' + glossary_uri = 'gs://cloud-samples-data/translation/bistro_glossary.csv' + + # create_glossary will raise an exception if creation fails + create_glossary(languages, PROJECT_ID, glossary_name, + glossary_uri) + + # Delete glossary so that future tests will pass + # delete_glossary will raise an exception if deletion fails + delete_glossary(PROJECT_ID, glossary_name) + + +def test_translate_standard(): + + expected_text = 'Hello' + + text = translate_text('Bonjour', 'fr', 'en', PROJECT_ID, + 'bistro-glossary') + + assert text == expected_text + + +def test_translate_glossary(): + + expected_text = 'I eat goat cheese' + input_text = 'Je mange du chevre' + + text = translate_text(input_text, 'fr', 'en', PROJECT_ID, + 'bistro-glossary') + + assert text == expected_text + + +# TEXT-TO-SPEECH TESTS + + +def test_tts_standard(capsys): + outfile = 'resources/test_standard_text.mp3' + textfile = 'resources/standard_format.txt' + + with open(textfile, 'r') as f: + text = f.read() + + text_to_speech(text, outfile) + + # Assert audio file generated + assert os.path.isfile(outfile) + out, err = capsys.readouterr() + + # Assert success message printed + assert 'Audio content written to file ' + outfile in out + + # Delete test file + os.remove(outfile) diff --git a/translate/cloud-client/hybrid_glossaries/requirements.txt b/translate/cloud-client/hybrid_glossaries/requirements.txt index ed9bbd1eae82..d13c5f7d8080 100644 --- a/translate/cloud-client/hybrid_glossaries/requirements.txt +++ b/translate/cloud-client/hybrid_glossaries/requirements.txt @@ -1,3 +1,3 @@ google-cloud-translate==2.0.1 google-cloud-vision==1.0.0 -google-cloud-texttospeech==2.0.0 +google-cloud-texttospeech==1.0.1 diff --git a/translate/cloud-client/hybrid_glossaries/resources/non_standard_format.txt b/translate/cloud-client/hybrid_glossaries/resources/non_standard_format.txt index 529799ee94b1..8a6e3c113d18 100644 --- a/translate/cloud-client/hybrid_glossaries/resources/non_standard_format.txt +++ b/translate/cloud-client/hybrid_glossaries/resources/non_standard_format.txt @@ -19,11 +19,11 @@ $10 Firebase Fruit Salad watermelon, honeydew melon, and pineapple -$6 Compute Engine Burger quarter-pound burger with cheddar cheese $10 +$6 BigQuery BLT bacon, lettuce, and tomato sandwich diff --git a/translate/cloud-client/snippets.py b/translate/cloud-client/snippets.py index 24b457162fac..2d5232e6fddd 100644 --- a/translate/cloud-client/snippets.py +++ b/translate/cloud-client/snippets.py @@ -76,7 +76,7 @@ def translate_text_with_model(target, text, model='nmt'): # [START translate_text_with_model] """Translates text into the target language. - Make sure your project is allowlisted. + Make sure your project is whitelisted. Target must be an ISO 639-1 language code. See https://g.co/cloud/translate/v2/translate-reference#supported_languages diff --git a/translate/cloud-client/translate_v3_batch_translate_text_test.py b/translate/cloud-client/translate_v3_batch_translate_text_test.py index e7919d022938..85d03e0b7d57 100644 --- a/translate/cloud-client/translate_v3_batch_translate_text_test.py +++ b/translate/cloud-client/translate_v3_batch_translate_text_test.py @@ -21,7 +21,7 @@ import translate_v3_batch_translate_text -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] @pytest.fixture(scope="function") diff --git a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py b/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py index 1a1850ac562e..dd2e688786ac 100644 --- a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py +++ b/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py @@ -26,7 +26,7 @@ import translate_v3_delete_glossary -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -57,7 +57,7 @@ def delete_glossary(): @pytest.fixture(scope="function") def bucket(): """Create a temporary bucket to store annotation output.""" - bucket_name = f'tmp-{uuid.uuid4().hex}' + bucket_name = str(uuid.uuid1()) storage_client = storage.Client() bucket = storage_client.create_bucket(bucket_name) diff --git a/translate/cloud-client/translate_v3_create_glossary_test.py b/translate/cloud-client/translate_v3_create_glossary_test.py index a24f461e05ab..e133fd3ff4c2 100644 --- a/translate/cloud-client/translate_v3_create_glossary_test.py +++ b/translate/cloud-client/translate_v3_create_glossary_test.py @@ -24,7 +24,7 @@ import translate_v3_delete_glossary -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" diff --git a/translate/cloud-client/translate_v3_delete_glossary_test.py b/translate/cloud-client/translate_v3_delete_glossary_test.py index a88c16a37a2b..4309ca01b959 100644 --- a/translate/cloud-client/translate_v3_delete_glossary_test.py +++ b/translate/cloud-client/translate_v3_delete_glossary_test.py @@ -20,7 +20,7 @@ import translate_v3_create_glossary import translate_v3_delete_glossary -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" diff --git a/translate/cloud-client/translate_v3_detect_language_test.py b/translate/cloud-client/translate_v3_detect_language_test.py index 7e9540cca009..2e9df86721c9 100644 --- a/translate/cloud-client/translate_v3_detect_language_test.py +++ b/translate/cloud-client/translate_v3_detect_language_test.py @@ -17,7 +17,7 @@ import translate_v3_detect_language -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] def test_detect_language(capsys): diff --git a/translate/cloud-client/translate_v3_get_glossary_test.py b/translate/cloud-client/translate_v3_get_glossary_test.py index 96ea6b78103d..a2df0a4ba455 100644 --- a/translate/cloud-client/translate_v3_get_glossary_test.py +++ b/translate/cloud-client/translate_v3_get_glossary_test.py @@ -25,7 +25,7 @@ import translate_v3_get_glossary -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" diff --git a/translate/cloud-client/translate_v3_get_supported_languages_test.py b/translate/cloud-client/translate_v3_get_supported_languages_test.py index 4bdc0fa4b14f..57ceeb203e8d 100644 --- a/translate/cloud-client/translate_v3_get_supported_languages_test.py +++ b/translate/cloud-client/translate_v3_get_supported_languages_test.py @@ -17,7 +17,7 @@ import translate_v3_get_supported_languages -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] def test_list_languages(capsys): diff --git a/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py b/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py index 8f3f52cfe54d..3b123d9f5c1f 100644 --- a/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py +++ b/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py @@ -17,7 +17,7 @@ import translate_v3_get_supported_languages_with_target as get_supported_langs -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] def test_list_languages_with_target(capsys): diff --git a/translate/cloud-client/translate_v3_list_glossary_test.py b/translate/cloud-client/translate_v3_list_glossary_test.py index 8f4eaa1ac512..04e93470e40e 100644 --- a/translate/cloud-client/translate_v3_list_glossary_test.py +++ b/translate/cloud-client/translate_v3_list_glossary_test.py @@ -25,7 +25,7 @@ import translate_v3_list_glossary -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" diff --git a/translate/cloud-client/translate_v3_translate_text_test.py b/translate/cloud-client/translate_v3_translate_text_test.py index c93cb91f6dd5..8897b9b527f9 100644 --- a/translate/cloud-client/translate_v3_translate_text_test.py +++ b/translate/cloud-client/translate_v3_translate_text_test.py @@ -17,7 +17,7 @@ import translate_v3_translate_text -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] def test_translate_text(capsys): diff --git a/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py b/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py index 1caa9e6e9e00..bc5403c681b8 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py +++ b/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py @@ -26,7 +26,7 @@ import translate_v3_translate_text_with_glossary -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" diff --git a/translate/cloud-client/translate_v3_translate_text_with_model_test.py b/translate/cloud-client/translate_v3_translate_text_with_model_test.py index f1cd59687521..16a5fc03db99 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model_test.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model_test.py @@ -18,7 +18,7 @@ import translate_v3_translate_text_with_model -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] MODEL_ID = "TRL3128559826197068699" From a429af022ce61e110ecafe89e3db8825e8550e57 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Thu, 11 Jun 2020 20:29:22 +0000 Subject: [PATCH 09/25] chore: remove extra files --- translate/automl/requirements.txt | 2 +- ...v3_batch_translate_text_with_model_test.py | 7 +-- translate/cloud-client/beta_snippets_test.py | 10 +---- .../hybrid_glossaries/hybrid_tutorial.py | 4 +- .../hybrid_tutorial_tests.py | 4 +- translate/cloud-client/requirements-test.txt | 4 +- translate/cloud-client/requirements.txt | 2 +- .../translate_v3_batch_translate_text_test.py | 8 +--- ...batch_translate_text_with_glossary_test.py | 27 +++--------- .../translate_v3_create_glossary_test.py | 44 ++++++------------- .../translate_v3_delete_glossary_test.py | 6 +-- .../translate_v3_detect_language.py | 2 +- .../translate_v3_detect_language_test.py | 4 +- .../translate_v3_get_glossary_test.py | 24 +++------- .../translate_v3_get_supported_languages.py | 2 +- ...anslate_v3_get_supported_languages_test.py | 4 +- ...et_supported_languages_with_target_test.py | 2 - .../translate_v3_list_glossary.py | 2 +- .../translate_v3_list_glossary_test.py | 26 +++-------- .../translate_v3_translate_text.py | 2 +- .../translate_v3_translate_text_test.py | 4 +- ...te_v3_translate_text_with_glossary_test.py | 24 +++------- ...slate_v3_translate_text_with_model_test.py | 2 - 23 files changed, 57 insertions(+), 159 deletions(-) diff --git a/translate/automl/requirements.txt b/translate/automl/requirements.txt index f291443e7302..0dbe115811e1 100644 --- a/translate/automl/requirements.txt +++ b/translate/automl/requirements.txt @@ -1,3 +1,3 @@ google-cloud-translate==2.0.1 -google-cloud-storage==1.28.1 +google-cloud-storage==1.26.0 google-cloud-automl==0.10.0 diff --git a/translate/automl/translate_v3_batch_translate_text_with_model_test.py b/translate/automl/translate_v3_batch_translate_text_with_model_test.py index 36c52ae22e4d..74b044f43527 100644 --- a/translate/automl/translate_v3_batch_translate_text_with_model_test.py +++ b/translate/automl/translate_v3_batch_translate_text_with_model_test.py @@ -13,13 +13,10 @@ # limitations under the License. import os -import uuid - -from google.cloud import storage import pytest - +import uuid import translate_v3_batch_translate_text_with_model - +from google.cloud import storage PROJECT_ID = os.environ["GCLOUD_PROJECT"] MODEL_ID = "TRL3128559826197068699" diff --git a/translate/cloud-client/beta_snippets_test.py b/translate/cloud-client/beta_snippets_test.py index 4852168567ac..4d65c3c2cd6a 100644 --- a/translate/cloud-client/beta_snippets_test.py +++ b/translate/cloud-client/beta_snippets_test.py @@ -14,13 +14,10 @@ # limitations under the License. import os -import uuid - -from google.cloud import storage import pytest - +import uuid import beta_snippets - +from google.cloud import storage PROJECT_ID = os.environ['GCLOUD_PROJECT'] @@ -70,7 +67,6 @@ def test_translate_text(capsys): assert 'Translated Text:' in out -@pytest.mark.flaky(max_runs=3, min_passes=1) def test_batch_translate_text(capsys, bucket): beta_snippets.batch_translate_text( PROJECT_ID, @@ -100,7 +96,6 @@ def test_list_languages_with_target(capsys): assert u'Display Name: albanska' in out -@pytest.mark.flaky(max_runs=3, min_passes=1) def test_create_glossary(capsys, unique_glossary_id): beta_snippets.create_glossary(PROJECT_ID, unique_glossary_id) out, _ = capsys.readouterr() @@ -130,7 +125,6 @@ def test_translate_text_with_glossary(capsys, glossary): assert 'cuenta' in out -@pytest.mark.flaky(max_runs=3, min_passes=1) def test_delete_glossary(capsys, unique_glossary_id): beta_snippets.create_glossary(PROJECT_ID, unique_glossary_id) beta_snippets.delete_glossary(PROJECT_ID, unique_glossary_id) diff --git a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py index 0da84b722078..42f854eb1471 100644 --- a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py +++ b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py @@ -14,15 +14,15 @@ # [START translate_hybrid_imports] -import html import io import os +import html # Imports the Google Cloud client libraries from google.api_core.exceptions import AlreadyExists -from google.cloud import texttospeech from google.cloud import translate_v3beta1 as translate from google.cloud import vision +from google.cloud import texttospeech # [END translate_hybrid_imports] diff --git a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py index 5224ca524a91..87e8420a2955 100644 --- a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py +++ b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py @@ -15,10 +15,10 @@ import os import sys -from hybrid_tutorial import create_glossary from hybrid_tutorial import pic_to_text -from hybrid_tutorial import text_to_speech +from hybrid_tutorial import create_glossary from hybrid_tutorial import translate_text +from hybrid_tutorial import text_to_speech PROJECT_ID = os.environ['GCLOUD_PROJECT'] diff --git a/translate/cloud-client/requirements-test.txt b/translate/cloud-client/requirements-test.txt index 1fccf2dd32ea..781d4326c947 100644 --- a/translate/cloud-client/requirements-test.txt +++ b/translate/cloud-client/requirements-test.txt @@ -1,3 +1 @@ -backoff==1.10.0 -flaky==3.6.1 -pytest==5.3.2 \ No newline at end of file +pytest==5.3.2 diff --git a/translate/cloud-client/requirements.txt b/translate/cloud-client/requirements.txt index 78de2e470bac..cd0aee0597c4 100644 --- a/translate/cloud-client/requirements.txt +++ b/translate/cloud-client/requirements.txt @@ -1,2 +1,2 @@ google-cloud-translate==2.0.1 -google-cloud-storage==1.28.1 +google-cloud-storage==1.26.0 diff --git a/translate/cloud-client/translate_v3_batch_translate_text_test.py b/translate/cloud-client/translate_v3_batch_translate_text_test.py index 85d03e0b7d57..2474f60d56a9 100644 --- a/translate/cloud-client/translate_v3_batch_translate_text_test.py +++ b/translate/cloud-client/translate_v3_batch_translate_text_test.py @@ -13,13 +13,10 @@ # limitations under the License. import os -import uuid - -from google.cloud import storage import pytest - import translate_v3_batch_translate_text - +import uuid +from google.cloud import storage PROJECT_ID = os.environ["GCLOUD_PROJECT"] @@ -36,7 +33,6 @@ def bucket(): bucket.delete(force=True) -@pytest.mark.flaky(max_runs=3, min_passes=1) def test_batch_translate_text(capsys, bucket): translate_v3_batch_translate_text.batch_translate_text( "gs://cloud-samples-data/translation/text.txt", diff --git a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py b/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py index dd2e688786ac..0e79bf9b8693 100644 --- a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py +++ b/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py @@ -13,18 +13,12 @@ # limitations under the License. import os -import uuid - -import backoff -from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError -from google.cloud import storage -from google.cloud.exceptions import NotFound import pytest - +import uuid import translate_v3_batch_translate_text_with_glossary import translate_v3_create_glossary import translate_v3_delete_glossary - +from google.cloud import storage PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -40,18 +34,10 @@ def glossary(): yield glossary_id - # cleanup - @backoff.on_exception( - backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 - ) - def delete_glossary(): - try: - translate_v3_delete_glossary.delete_glossary( - PROJECT_ID, glossary_id) - except NotFound as e: - # Ignoring this case. - print("Got NotFound, detail: {}".format(str(e))) - delete_glossary() + try: + translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) + except Exception: + pass @pytest.fixture(scope="function") @@ -66,7 +52,6 @@ def bucket(): bucket.delete(force=True) -@pytest.mark.flaky(max_runs=3, min_passes=1) def test_batch_translate_text_with_glossary(capsys, bucket, glossary): translate_v3_batch_translate_text_with_glossary.batch_translate_text_with_glossary( "gs://cloud-samples-data/translation/text_with_glossary.txt", diff --git a/translate/cloud-client/translate_v3_create_glossary_test.py b/translate/cloud-client/translate_v3_create_glossary_test.py index e133fd3ff4c2..77a2cf1435cc 100644 --- a/translate/cloud-client/translate_v3_create_glossary_test.py +++ b/translate/cloud-client/translate_v3_create_glossary_test.py @@ -13,42 +13,26 @@ # limitations under the License. import os -import uuid - -import backoff -from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError -from google.cloud.exceptions import NotFound -import pytest - import translate_v3_create_glossary import translate_v3_delete_glossary - +import uuid PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" -@pytest.mark.flaky(max_runs=3, min_passes=1) def test_create_glossary(capsys): + glossary_id = "test-{}".format(uuid.uuid4()) + translate_v3_create_glossary.create_glossary( + PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id + ) + out, _ = capsys.readouterr() + # assert + assert "Created:" in out + assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out + + # clean up after use try: - glossary_id = "test-{}".format(uuid.uuid4()) - translate_v3_create_glossary.create_glossary( - PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id - ) - out, _ = capsys.readouterr() - # assert - assert "Created:" in out - assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out - finally: - # cleanup - @backoff.on_exception( - backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 - ) - def delete_glossary(): - try: - translate_v3_delete_glossary.delete_glossary( - PROJECT_ID, glossary_id) - except NotFound as e: - # Ignoring this case. - print("Got NotFound, detail: {}".format(str(e))) - delete_glossary() + translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) + except Exception: + pass diff --git a/translate/cloud-client/translate_v3_delete_glossary_test.py b/translate/cloud-client/translate_v3_delete_glossary_test.py index 4309ca01b959..8d7fb2d92451 100644 --- a/translate/cloud-client/translate_v3_delete_glossary_test.py +++ b/translate/cloud-client/translate_v3_delete_glossary_test.py @@ -13,18 +13,14 @@ # limitations under the License. import os -import uuid - -import pytest - import translate_v3_create_glossary import translate_v3_delete_glossary +import uuid PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" -@pytest.mark.flaky(max_runs=3, min_passes=1) def test_delete_glossary(capsys): # setup glossary_id = "test-{}".format(uuid.uuid4()) diff --git a/translate/cloud-client/translate_v3_detect_language.py b/translate/cloud-client/translate_v3_detect_language.py index 759eb41a4d30..9b92853cfca1 100644 --- a/translate/cloud-client/translate_v3_detect_language.py +++ b/translate/cloud-client/translate_v3_detect_language.py @@ -16,7 +16,7 @@ from google.cloud import translate -def detect_language(project_id="YOUR_PROJECT_ID"): +def sample_detect_language(project_id="YOUR_PROJECT_ID"): """Detecting the language of a text string.""" client = translate.TranslationServiceClient() diff --git a/translate/cloud-client/translate_v3_detect_language_test.py b/translate/cloud-client/translate_v3_detect_language_test.py index 2e9df86721c9..82620bc550c1 100644 --- a/translate/cloud-client/translate_v3_detect_language_test.py +++ b/translate/cloud-client/translate_v3_detect_language_test.py @@ -13,14 +13,12 @@ # limitations under the License. import os - import translate_v3_detect_language - PROJECT_ID = os.environ["GCLOUD_PROJECT"] def test_detect_language(capsys): - translate_v3_detect_language.detect_language(PROJECT_ID) + translate_v3_detect_language.sample_detect_language(PROJECT_ID) out, _ = capsys.readouterr() assert "en" in out diff --git a/translate/cloud-client/translate_v3_get_glossary_test.py b/translate/cloud-client/translate_v3_get_glossary_test.py index a2df0a4ba455..7a4aa167cead 100644 --- a/translate/cloud-client/translate_v3_get_glossary_test.py +++ b/translate/cloud-client/translate_v3_get_glossary_test.py @@ -13,17 +13,11 @@ # limitations under the License. import os -import uuid - -import backoff -from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError -from google.cloud.exceptions import NotFound import pytest - import translate_v3_create_glossary import translate_v3_delete_glossary import translate_v3_get_glossary - +import uuid PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -39,18 +33,10 @@ def glossary(): yield glossary_id - # cleanup - @backoff.on_exception( - backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 - ) - def delete_glossary(): - try: - translate_v3_delete_glossary.delete_glossary( - PROJECT_ID, glossary_id) - except NotFound as e: - # Ignoring this case. - print("Got NotFound, detail: {}".format(str(e))) - delete_glossary() + try: + translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) + except Exception: + pass def test_get_glossary(capsys, glossary): diff --git a/translate/cloud-client/translate_v3_get_supported_languages.py b/translate/cloud-client/translate_v3_get_supported_languages.py index 6e15458d9cca..eb7d83666a43 100644 --- a/translate/cloud-client/translate_v3_get_supported_languages.py +++ b/translate/cloud-client/translate_v3_get_supported_languages.py @@ -16,7 +16,7 @@ from google.cloud import translate -def get_supported_languages(project_id="YOUR_PROJECT_ID"): +def sample_get_supported_languages(project_id="YOUR_PROJECT_ID"): """Getting a list of supported language codes.""" client = translate.TranslationServiceClient() diff --git a/translate/cloud-client/translate_v3_get_supported_languages_test.py b/translate/cloud-client/translate_v3_get_supported_languages_test.py index 57ceeb203e8d..b4ba4cd22b30 100644 --- a/translate/cloud-client/translate_v3_get_supported_languages_test.py +++ b/translate/cloud-client/translate_v3_get_supported_languages_test.py @@ -13,14 +13,12 @@ # limitations under the License. import os - import translate_v3_get_supported_languages - PROJECT_ID = os.environ["GCLOUD_PROJECT"] def test_list_languages(capsys): - translate_v3_get_supported_languages.get_supported_languages(PROJECT_ID) + translate_v3_get_supported_languages.sample_get_supported_languages(PROJECT_ID) out, _ = capsys.readouterr() assert "zh-CN" in out diff --git a/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py b/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py index 3b123d9f5c1f..aa40efe4bb7a 100644 --- a/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py +++ b/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py @@ -13,10 +13,8 @@ # limitations under the License. import os - import translate_v3_get_supported_languages_with_target as get_supported_langs - PROJECT_ID = os.environ["GCLOUD_PROJECT"] diff --git a/translate/cloud-client/translate_v3_list_glossary.py b/translate/cloud-client/translate_v3_list_glossary.py index 53e51d85447f..e52d6b260ed9 100644 --- a/translate/cloud-client/translate_v3_list_glossary.py +++ b/translate/cloud-client/translate_v3_list_glossary.py @@ -16,7 +16,7 @@ from google.cloud import translate -def list_glossaries(project_id="YOUR_PROJECT_ID"): +def sample_list_glossaries(project_id="YOUR_PROJECT_ID"): """List Glossaries.""" client = translate.TranslationServiceClient() diff --git a/translate/cloud-client/translate_v3_list_glossary_test.py b/translate/cloud-client/translate_v3_list_glossary_test.py index 04e93470e40e..de77daba8028 100644 --- a/translate/cloud-client/translate_v3_list_glossary_test.py +++ b/translate/cloud-client/translate_v3_list_glossary_test.py @@ -13,17 +13,11 @@ # limitations under the License. import os -import uuid - -import backoff -from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError -from google.cloud.exceptions import NotFound import pytest - import translate_v3_create_glossary import translate_v3_delete_glossary import translate_v3_list_glossary - +import uuid PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -39,22 +33,14 @@ def glossary(): yield glossary_id - # clean up - @backoff.on_exception( - backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 - ) - def delete_glossary(): - try: - translate_v3_delete_glossary.delete_glossary( - PROJECT_ID, glossary_id) - except NotFound as e: - # Ignoring this case. - print("Got NotFound, detail: {}".format(str(e))) - delete_glossary() + try: + translate_v3_delete_glossary.sample_delete_glossary(PROJECT_ID, glossary_id) + except Exception: + pass def test_list_glossary(capsys, glossary): - translate_v3_list_glossary.list_glossaries(PROJECT_ID) + translate_v3_list_glossary.sample_list_glossaries(PROJECT_ID) out, _ = capsys.readouterr() assert glossary in out assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out diff --git a/translate/cloud-client/translate_v3_translate_text.py b/translate/cloud-client/translate_v3_translate_text.py index 086f775a37fb..77fcec26deeb 100644 --- a/translate/cloud-client/translate_v3_translate_text.py +++ b/translate/cloud-client/translate_v3_translate_text.py @@ -16,7 +16,7 @@ from google.cloud import translate -def translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"): +def sample_translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"): """Translating Text.""" client = translate.TranslationServiceClient() diff --git a/translate/cloud-client/translate_v3_translate_text_test.py b/translate/cloud-client/translate_v3_translate_text_test.py index 8897b9b527f9..a63190197818 100644 --- a/translate/cloud-client/translate_v3_translate_text_test.py +++ b/translate/cloud-client/translate_v3_translate_text_test.py @@ -13,15 +13,13 @@ # limitations under the License. import os - import translate_v3_translate_text - PROJECT_ID = os.environ["GCLOUD_PROJECT"] def test_translate_text(capsys): - translate_v3_translate_text.translate_text( + translate_v3_translate_text.sample_translate_text( "Hello World!", PROJECT_ID) out, _ = capsys.readouterr() assert "Bonjour le monde" in out diff --git a/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py b/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py index bc5403c681b8..72f9d64e2f8d 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py +++ b/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py @@ -14,17 +14,11 @@ # limitations under the License. import os -import uuid - -import backoff -from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError -from google.cloud.exceptions import NotFound import pytest - import translate_v3_create_glossary import translate_v3_delete_glossary import translate_v3_translate_text_with_glossary - +import uuid PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -40,18 +34,10 @@ def glossary(): yield glossary_id - # cleanup - @backoff.on_exception( - backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 - ) - def delete_glossary(): - try: - translate_v3_delete_glossary.delete_glossary( - PROJECT_ID, glossary_id) - except NotFound as e: - # Ignoring this case. - print("Got NotFound, detail: {}".format(str(e))) - delete_glossary() + try: + translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) + except Exception: + pass def test_translate_text_with_glossary(capsys, glossary): diff --git a/translate/cloud-client/translate_v3_translate_text_with_model_test.py b/translate/cloud-client/translate_v3_translate_text_with_model_test.py index 16a5fc03db99..6e6ab37eee16 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model_test.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model_test.py @@ -14,10 +14,8 @@ # limitations under the License. import os - import translate_v3_translate_text_with_model - PROJECT_ID = os.environ["GCLOUD_PROJECT"] MODEL_ID = "TRL3128559826197068699" From 300370af72439008962638b580a6cbea4e59df13 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Thu, 11 Jun 2020 20:30:00 +0000 Subject: [PATCH 10/25] remove extra files --- .../cloud-client/hybrid_glossaries/noxfile.py | 225 ------------------ translate/cloud-client/noxfile.py | 225 ------------------ 2 files changed, 450 deletions(-) delete mode 100644 translate/cloud-client/hybrid_glossaries/noxfile.py delete mode 100644 translate/cloud-client/noxfile.py diff --git a/translate/cloud-client/hybrid_glossaries/noxfile.py b/translate/cloud-client/hybrid_glossaries/noxfile.py deleted file mode 100644 index b23055f14a65..000000000000 --- a/translate/cloud-client/hybrid_glossaries/noxfile.py +++ /dev/null @@ -1,225 +0,0 @@ -# Copyright 2019 Google LLC -# -# 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 __future__ import print_function - -import os -from pathlib import Path -import sys - -import nox - - -# WARNING - WARNING - WARNING - WARNING - WARNING -# WARNING - WARNING - WARNING - WARNING - WARNING -# DO NOT EDIT THIS FILE EVER! -# WARNING - WARNING - WARNING - WARNING - WARNING -# WARNING - WARNING - WARNING - WARNING - WARNING - -# Copy `noxfile_config.py` to your directory and modify it instead. - - -# `TEST_CONFIG` dict is a configuration hook that allows users to -# modify the test configurations. The values here should be in sync -# with `noxfile_config.py`. Users will copy `noxfile_config.py` into -# their directory and modify it. - -TEST_CONFIG = { - # You can opt out from the test for specific Python versions. - 'ignored_versions': ["2.7"], - - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a - # build specific Cloud project. You can also use your own string - # to use your own Cloud project. - 'gcloud_project_env': 'GCLOUD_PROJECT', - # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', - - # A dictionary you want to inject into your test. Don't put any - # secrets here. These values will override predefined values. - 'envs': {}, -} - - -try: - # Ensure we can import noxfile_config in the project's directory. - sys.path.append('.') - from noxfile_config import TEST_CONFIG_OVERRIDE -except ImportError as e: - print("No user noxfile_config found: detail: {}".format(e)) - TEST_CONFIG_OVERRIDE = {} - -# Update the TEST_CONFIG with the user supplied values. -TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) - - -def get_pytest_env_vars(): - """Returns a dict for pytest invocation.""" - ret = {} - - # Override the GCLOUD_PROJECT and the alias. - env_key = TEST_CONFIG['gcloud_project_env'] - # This should error out if not set. - ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key] - ret['GCLOUD_PROJECT'] = os.environ[env_key] - - # Apply user supplied envs. - ret.update(TEST_CONFIG['envs']) - return ret - - -# DO NOT EDIT - automatically generated. -# All versions used to tested samples. -ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"] - -# Any default versions that should be ignored. -IGNORED_VERSIONS = TEST_CONFIG['ignored_versions'] - -TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) - -INSTALL_LIBRARY_FROM_SOURCE = bool(os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False)) -# -# Style Checks -# - - -def _determine_local_import_names(start_dir): - """Determines all import names that should be considered "local". - - This is used when running the linter to insure that import order is - properly checked. - """ - file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] - return [ - basename - for basename, extension in file_ext_pairs - if extension == ".py" - or os.path.isdir(os.path.join(start_dir, basename)) - and basename not in ("__pycache__") - ] - - -# Linting with flake8. -# -# We ignore the following rules: -# E203: whitespace before ‘:’ -# E266: too many leading ‘#’ for block comment -# E501: line too long -# I202: Additional newline in a section of imports -# -# We also need to specify the rules which are ignored by default: -# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] -FLAKE8_COMMON_ARGS = [ - "--show-source", - "--builtin=gettext", - "--max-complexity=20", - "--import-order-style=google", - "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", - "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202", - "--max-line-length=88", -] - - -@nox.session -def lint(session): - session.install("flake8", "flake8-import-order") - - local_names = _determine_local_import_names(".") - args = FLAKE8_COMMON_ARGS + [ - "--application-import-names", - ",".join(local_names), - "." - ] - session.run("flake8", *args) - - -# -# Sample Tests -# - - -PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] - - -def _session_tests(session, post_install=None): - """Runs py.test for a particular project.""" - if os.path.exists("requirements.txt"): - session.install("-r", "requirements.txt") - - if os.path.exists("requirements-test.txt"): - session.install("-r", "requirements-test.txt") - - if INSTALL_LIBRARY_FROM_SOURCE: - session.install("-e", _get_repo_root()) - - if post_install: - post_install(session) - - session.run( - "pytest", - *(PYTEST_COMMON_ARGS + session.posargs), - # Pytest will return 5 when no tests are collected. This can happen - # on travis where slow and flaky tests are excluded. - # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html - success_codes=[0, 5], - env=get_pytest_env_vars() - ) - - -@nox.session(python=ALL_VERSIONS) -def py(session): - """Runs py.test for a sample using the specified version of Python.""" - if session.python in TESTED_VERSIONS: - _session_tests(session) - else: - session.skip("SKIPPED: {} tests are disabled for this sample.".format( - session.python - )) - - -# -# Readmegen -# - - -def _get_repo_root(): - """ Returns the root folder of the project. """ - # Get root of this repository. Assume we don't have directories nested deeper than 10 items. - p = Path(os.getcwd()) - for i in range(10): - if p is None: - break - if Path(p / ".git").exists(): - return str(p) - p = p.parent - raise Exception("Unable to detect repository root.") - - -GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) - - -@nox.session -@nox.parametrize("path", GENERATED_READMES) -def readmegen(session, path): - """(Re-)generates the readme for a sample.""" - session.install("jinja2", "pyyaml") - dir_ = os.path.dirname(path) - - if os.path.exists(os.path.join(dir_, "requirements.txt")): - session.install("-r", os.path.join(dir_, "requirements.txt")) - - in_file = os.path.join(dir_, "README.rst.in") - session.run( - "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file - ) diff --git a/translate/cloud-client/noxfile.py b/translate/cloud-client/noxfile.py deleted file mode 100644 index b23055f14a65..000000000000 --- a/translate/cloud-client/noxfile.py +++ /dev/null @@ -1,225 +0,0 @@ -# Copyright 2019 Google LLC -# -# 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 __future__ import print_function - -import os -from pathlib import Path -import sys - -import nox - - -# WARNING - WARNING - WARNING - WARNING - WARNING -# WARNING - WARNING - WARNING - WARNING - WARNING -# DO NOT EDIT THIS FILE EVER! -# WARNING - WARNING - WARNING - WARNING - WARNING -# WARNING - WARNING - WARNING - WARNING - WARNING - -# Copy `noxfile_config.py` to your directory and modify it instead. - - -# `TEST_CONFIG` dict is a configuration hook that allows users to -# modify the test configurations. The values here should be in sync -# with `noxfile_config.py`. Users will copy `noxfile_config.py` into -# their directory and modify it. - -TEST_CONFIG = { - # You can opt out from the test for specific Python versions. - 'ignored_versions': ["2.7"], - - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a - # build specific Cloud project. You can also use your own string - # to use your own Cloud project. - 'gcloud_project_env': 'GCLOUD_PROJECT', - # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', - - # A dictionary you want to inject into your test. Don't put any - # secrets here. These values will override predefined values. - 'envs': {}, -} - - -try: - # Ensure we can import noxfile_config in the project's directory. - sys.path.append('.') - from noxfile_config import TEST_CONFIG_OVERRIDE -except ImportError as e: - print("No user noxfile_config found: detail: {}".format(e)) - TEST_CONFIG_OVERRIDE = {} - -# Update the TEST_CONFIG with the user supplied values. -TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) - - -def get_pytest_env_vars(): - """Returns a dict for pytest invocation.""" - ret = {} - - # Override the GCLOUD_PROJECT and the alias. - env_key = TEST_CONFIG['gcloud_project_env'] - # This should error out if not set. - ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key] - ret['GCLOUD_PROJECT'] = os.environ[env_key] - - # Apply user supplied envs. - ret.update(TEST_CONFIG['envs']) - return ret - - -# DO NOT EDIT - automatically generated. -# All versions used to tested samples. -ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"] - -# Any default versions that should be ignored. -IGNORED_VERSIONS = TEST_CONFIG['ignored_versions'] - -TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) - -INSTALL_LIBRARY_FROM_SOURCE = bool(os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False)) -# -# Style Checks -# - - -def _determine_local_import_names(start_dir): - """Determines all import names that should be considered "local". - - This is used when running the linter to insure that import order is - properly checked. - """ - file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] - return [ - basename - for basename, extension in file_ext_pairs - if extension == ".py" - or os.path.isdir(os.path.join(start_dir, basename)) - and basename not in ("__pycache__") - ] - - -# Linting with flake8. -# -# We ignore the following rules: -# E203: whitespace before ‘:’ -# E266: too many leading ‘#’ for block comment -# E501: line too long -# I202: Additional newline in a section of imports -# -# We also need to specify the rules which are ignored by default: -# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] -FLAKE8_COMMON_ARGS = [ - "--show-source", - "--builtin=gettext", - "--max-complexity=20", - "--import-order-style=google", - "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", - "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202", - "--max-line-length=88", -] - - -@nox.session -def lint(session): - session.install("flake8", "flake8-import-order") - - local_names = _determine_local_import_names(".") - args = FLAKE8_COMMON_ARGS + [ - "--application-import-names", - ",".join(local_names), - "." - ] - session.run("flake8", *args) - - -# -# Sample Tests -# - - -PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] - - -def _session_tests(session, post_install=None): - """Runs py.test for a particular project.""" - if os.path.exists("requirements.txt"): - session.install("-r", "requirements.txt") - - if os.path.exists("requirements-test.txt"): - session.install("-r", "requirements-test.txt") - - if INSTALL_LIBRARY_FROM_SOURCE: - session.install("-e", _get_repo_root()) - - if post_install: - post_install(session) - - session.run( - "pytest", - *(PYTEST_COMMON_ARGS + session.posargs), - # Pytest will return 5 when no tests are collected. This can happen - # on travis where slow and flaky tests are excluded. - # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html - success_codes=[0, 5], - env=get_pytest_env_vars() - ) - - -@nox.session(python=ALL_VERSIONS) -def py(session): - """Runs py.test for a sample using the specified version of Python.""" - if session.python in TESTED_VERSIONS: - _session_tests(session) - else: - session.skip("SKIPPED: {} tests are disabled for this sample.".format( - session.python - )) - - -# -# Readmegen -# - - -def _get_repo_root(): - """ Returns the root folder of the project. """ - # Get root of this repository. Assume we don't have directories nested deeper than 10 items. - p = Path(os.getcwd()) - for i in range(10): - if p is None: - break - if Path(p / ".git").exists(): - return str(p) - p = p.parent - raise Exception("Unable to detect repository root.") - - -GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) - - -@nox.session -@nox.parametrize("path", GENERATED_READMES) -def readmegen(session, path): - """(Re-)generates the readme for a sample.""" - session.install("jinja2", "pyyaml") - dir_ = os.path.dirname(path) - - if os.path.exists(os.path.join(dir_, "requirements.txt")): - session.install("-r", os.path.join(dir_, "requirements.txt")) - - in_file = os.path.join(dir_, "README.rst.in") - session.run( - "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file - ) From 420e8ec9e22166bf746853164f9ee4dfd4757a46 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Thu, 11 Jun 2020 20:32:56 +0000 Subject: [PATCH 11/25] take 2 --- translate/automl/dataset_test.py | 2 +- translate/automl/model_test.py | 2 +- translate/automl/predict_test.py | 2 +- translate/automl/requirements.txt | 2 +- ...v3_batch_translate_text_with_model_test.py | 11 +++-- translate/cloud-client/beta_snippets_test.py | 14 ++++-- .../hybrid_glossaries/hybrid_tutorial.py | 25 ++++++---- .../hybrid_glossaries/requirements.txt | 2 +- .../resources/non_standard_format.txt | 2 +- translate/cloud-client/requirements-test.txt | 4 +- translate/cloud-client/requirements.txt | 2 +- translate/cloud-client/snippets.py | 2 +- .../translate_v3_batch_translate_text_test.py | 10 ++-- ...batch_translate_text_with_glossary_test.py | 31 +++++++++---- .../translate_v3_create_glossary_test.py | 46 +++++++++++++------ .../translate_v3_delete_glossary_test.py | 8 +++- .../translate_v3_detect_language.py | 2 +- .../translate_v3_detect_language_test.py | 6 ++- .../translate_v3_get_glossary_test.py | 26 ++++++++--- .../translate_v3_get_supported_languages.py | 2 +- ...anslate_v3_get_supported_languages_test.py | 6 ++- ...et_supported_languages_with_target_test.py | 4 +- .../translate_v3_list_glossary.py | 2 +- .../translate_v3_list_glossary_test.py | 28 ++++++++--- .../translate_v3_translate_text.py | 2 +- .../translate_v3_translate_text_test.py | 6 ++- ...te_v3_translate_text_with_glossary_test.py | 26 ++++++++--- ...slate_v3_translate_text_with_model_test.py | 4 +- 28 files changed, 194 insertions(+), 85 deletions(-) diff --git a/translate/automl/dataset_test.py b/translate/automl/dataset_test.py index 29e3e5c9fe97..4430ec5400e0 100644 --- a/translate/automl/dataset_test.py +++ b/translate/automl/dataset_test.py @@ -21,7 +21,7 @@ import automl_translation_dataset -project_id = os.environ["GCLOUD_PROJECT"] +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] compute_region = "us-central1" diff --git a/translate/automl/model_test.py b/translate/automl/model_test.py index 0d37a85c6740..e19a50ea7376 100644 --- a/translate/automl/model_test.py +++ b/translate/automl/model_test.py @@ -22,7 +22,7 @@ import automl_translation_model -project_id = os.environ["GCLOUD_PROJECT"] +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] compute_region = "us-central1" diff --git a/translate/automl/predict_test.py b/translate/automl/predict_test.py index f9d98dfb053c..d00a4658d7bb 100644 --- a/translate/automl/predict_test.py +++ b/translate/automl/predict_test.py @@ -18,7 +18,7 @@ import automl_translation_predict -project_id = os.environ["GCLOUD_PROJECT"] +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] compute_region = "us-central1" diff --git a/translate/automl/requirements.txt b/translate/automl/requirements.txt index 0dbe115811e1..f291443e7302 100644 --- a/translate/automl/requirements.txt +++ b/translate/automl/requirements.txt @@ -1,3 +1,3 @@ google-cloud-translate==2.0.1 -google-cloud-storage==1.26.0 +google-cloud-storage==1.28.1 google-cloud-automl==0.10.0 diff --git a/translate/automl/translate_v3_batch_translate_text_with_model_test.py b/translate/automl/translate_v3_batch_translate_text_with_model_test.py index 74b044f43527..4d0def04df3e 100644 --- a/translate/automl/translate_v3_batch_translate_text_with_model_test.py +++ b/translate/automl/translate_v3_batch_translate_text_with_model_test.py @@ -13,19 +13,22 @@ # limitations under the License. import os -import pytest import uuid -import translate_v3_batch_translate_text_with_model + from google.cloud import storage +import pytest + +import translate_v3_batch_translate_text_with_model + -PROJECT_ID = os.environ["GCLOUD_PROJECT"] +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] MODEL_ID = "TRL3128559826197068699" @pytest.fixture(scope="function") def bucket(): """Create a temporary bucket to store annotation output.""" - bucket_name = str(uuid.uuid1()) + bucket_name = f'tmp-{uuid.uuid4().hex}' storage_client = storage.Client() bucket = storage_client.create_bucket(bucket_name) diff --git a/translate/cloud-client/beta_snippets_test.py b/translate/cloud-client/beta_snippets_test.py index 4d65c3c2cd6a..a42ab08ce3fc 100644 --- a/translate/cloud-client/beta_snippets_test.py +++ b/translate/cloud-client/beta_snippets_test.py @@ -14,18 +14,21 @@ # limitations under the License. import os -import pytest import uuid -import beta_snippets + from google.cloud import storage +import pytest + +import beta_snippets + -PROJECT_ID = os.environ['GCLOUD_PROJECT'] +PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] @pytest.fixture(scope='function') def bucket(): """Create a temporary bucket to store annotation output.""" - bucket_name = str(uuid.uuid1()) + bucket_name = f'tmp-{uuid.uuid4().hex}' storage_client = storage.Client() bucket = storage_client.create_bucket(bucket_name) @@ -67,6 +70,7 @@ def test_translate_text(capsys): assert 'Translated Text:' in out +@pytest.mark.flaky(max_runs=3, min_passes=1) def test_batch_translate_text(capsys, bucket): beta_snippets.batch_translate_text( PROJECT_ID, @@ -96,6 +100,7 @@ def test_list_languages_with_target(capsys): assert u'Display Name: albanska' in out +@pytest.mark.flaky(max_runs=3, min_passes=1) def test_create_glossary(capsys, unique_glossary_id): beta_snippets.create_glossary(PROJECT_ID, unique_glossary_id) out, _ = capsys.readouterr() @@ -125,6 +130,7 @@ def test_translate_text_with_glossary(capsys, glossary): assert 'cuenta' in out +@pytest.mark.flaky(max_runs=3, min_passes=1) def test_delete_glossary(capsys, unique_glossary_id): beta_snippets.create_glossary(PROJECT_ID, unique_glossary_id) beta_snippets.delete_glossary(PROJECT_ID, unique_glossary_id) diff --git a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py index 42f854eb1471..7d36174b4766 100644 --- a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py +++ b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py @@ -14,21 +14,21 @@ # [START translate_hybrid_imports] +import html import io import os -import html # Imports the Google Cloud client libraries from google.api_core.exceptions import AlreadyExists +from google.cloud import texttospeech from google.cloud import translate_v3beta1 as translate from google.cloud import vision -from google.cloud import texttospeech # [END translate_hybrid_imports] # [START translate_hybrid_project_id] # extract GCP project id -PROJECT_ID = os.environ['GCLOUD_PROJECT'] +PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] # [END translate_hybrid_project_id] @@ -193,21 +193,28 @@ def text_to_speech(text, outfile): client = texttospeech.TextToSpeechClient() # Sets the text input to be synthesized - synthesis_input = texttospeech.types.SynthesisInput(ssml=ssml) + synthesis_input = texttospeech.SynthesisInput(ssml=ssml) # Builds the voice request, selects the language code ("en-US") and # the SSML voice gender ("MALE") - voice = texttospeech.types.VoiceSelectionParams( + voice = texttospeech.VoiceSelectionParams( language_code='en-US', - ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE) + ssml_gender=texttospeech.SsmlVoiceGender.MALE) # Selects the type of audio file to return - audio_config = texttospeech.types.AudioConfig( - audio_encoding=texttospeech.enums.AudioEncoding.MP3) + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3) # Performs the text-to-speech request on the text input with the selected # voice parameters and audio file type - response = client.synthesize_speech(synthesis_input, voice, audio_config) + + request = texttospeech.SynthesizeSpeechRequest( + input=synthesis_input, + voice=voice, + audio_config=audio_config + ) + + response = client.synthesize_speech(request=request) # Writes the synthetic audio to the output file. with open(outfile, 'wb') as out: diff --git a/translate/cloud-client/hybrid_glossaries/requirements.txt b/translate/cloud-client/hybrid_glossaries/requirements.txt index d13c5f7d8080..ed9bbd1eae82 100644 --- a/translate/cloud-client/hybrid_glossaries/requirements.txt +++ b/translate/cloud-client/hybrid_glossaries/requirements.txt @@ -1,3 +1,3 @@ google-cloud-translate==2.0.1 google-cloud-vision==1.0.0 -google-cloud-texttospeech==1.0.1 +google-cloud-texttospeech==2.0.0 diff --git a/translate/cloud-client/hybrid_glossaries/resources/non_standard_format.txt b/translate/cloud-client/hybrid_glossaries/resources/non_standard_format.txt index 8a6e3c113d18..529799ee94b1 100644 --- a/translate/cloud-client/hybrid_glossaries/resources/non_standard_format.txt +++ b/translate/cloud-client/hybrid_glossaries/resources/non_standard_format.txt @@ -19,11 +19,11 @@ $10 Firebase Fruit Salad watermelon, honeydew melon, and pineapple +$6 Compute Engine Burger quarter-pound burger with cheddar cheese $10 -$6 BigQuery BLT bacon, lettuce, and tomato sandwich diff --git a/translate/cloud-client/requirements-test.txt b/translate/cloud-client/requirements-test.txt index 781d4326c947..1fccf2dd32ea 100644 --- a/translate/cloud-client/requirements-test.txt +++ b/translate/cloud-client/requirements-test.txt @@ -1 +1,3 @@ -pytest==5.3.2 +backoff==1.10.0 +flaky==3.6.1 +pytest==5.3.2 \ No newline at end of file diff --git a/translate/cloud-client/requirements.txt b/translate/cloud-client/requirements.txt index cd0aee0597c4..78de2e470bac 100644 --- a/translate/cloud-client/requirements.txt +++ b/translate/cloud-client/requirements.txt @@ -1,2 +1,2 @@ google-cloud-translate==2.0.1 -google-cloud-storage==1.26.0 +google-cloud-storage==1.28.1 diff --git a/translate/cloud-client/snippets.py b/translate/cloud-client/snippets.py index 2d5232e6fddd..24b457162fac 100644 --- a/translate/cloud-client/snippets.py +++ b/translate/cloud-client/snippets.py @@ -76,7 +76,7 @@ def translate_text_with_model(target, text, model='nmt'): # [START translate_text_with_model] """Translates text into the target language. - Make sure your project is whitelisted. + Make sure your project is allowlisted. Target must be an ISO 639-1 language code. See https://g.co/cloud/translate/v2/translate-reference#supported_languages diff --git a/translate/cloud-client/translate_v3_batch_translate_text_test.py b/translate/cloud-client/translate_v3_batch_translate_text_test.py index 2474f60d56a9..e7919d022938 100644 --- a/translate/cloud-client/translate_v3_batch_translate_text_test.py +++ b/translate/cloud-client/translate_v3_batch_translate_text_test.py @@ -13,12 +13,15 @@ # limitations under the License. import os -import pytest -import translate_v3_batch_translate_text import uuid + from google.cloud import storage +import pytest + +import translate_v3_batch_translate_text + -PROJECT_ID = os.environ["GCLOUD_PROJECT"] +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] @pytest.fixture(scope="function") @@ -33,6 +36,7 @@ def bucket(): bucket.delete(force=True) +@pytest.mark.flaky(max_runs=3, min_passes=1) def test_batch_translate_text(capsys, bucket): translate_v3_batch_translate_text.batch_translate_text( "gs://cloud-samples-data/translation/text.txt", diff --git a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py b/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py index 0e79bf9b8693..1a1850ac562e 100644 --- a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py +++ b/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py @@ -13,14 +13,20 @@ # limitations under the License. import os -import pytest import uuid + +import backoff +from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError +from google.cloud import storage +from google.cloud.exceptions import NotFound +import pytest + import translate_v3_batch_translate_text_with_glossary import translate_v3_create_glossary import translate_v3_delete_glossary -from google.cloud import storage -PROJECT_ID = os.environ["GCLOUD_PROJECT"] + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -34,16 +40,24 @@ def glossary(): yield glossary_id - try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except Exception: - pass + # cleanup + @backoff.on_exception( + backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 + ) + def delete_glossary(): + try: + translate_v3_delete_glossary.delete_glossary( + PROJECT_ID, glossary_id) + except NotFound as e: + # Ignoring this case. + print("Got NotFound, detail: {}".format(str(e))) + delete_glossary() @pytest.fixture(scope="function") def bucket(): """Create a temporary bucket to store annotation output.""" - bucket_name = str(uuid.uuid1()) + bucket_name = f'tmp-{uuid.uuid4().hex}' storage_client = storage.Client() bucket = storage_client.create_bucket(bucket_name) @@ -52,6 +66,7 @@ def bucket(): bucket.delete(force=True) +@pytest.mark.flaky(max_runs=3, min_passes=1) def test_batch_translate_text_with_glossary(capsys, bucket, glossary): translate_v3_batch_translate_text_with_glossary.batch_translate_text_with_glossary( "gs://cloud-samples-data/translation/text_with_glossary.txt", diff --git a/translate/cloud-client/translate_v3_create_glossary_test.py b/translate/cloud-client/translate_v3_create_glossary_test.py index 77a2cf1435cc..a24f461e05ab 100644 --- a/translate/cloud-client/translate_v3_create_glossary_test.py +++ b/translate/cloud-client/translate_v3_create_glossary_test.py @@ -13,26 +13,42 @@ # limitations under the License. import os +import uuid + +import backoff +from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError +from google.cloud.exceptions import NotFound +import pytest + import translate_v3_create_glossary import translate_v3_delete_glossary -import uuid -PROJECT_ID = os.environ["GCLOUD_PROJECT"] + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" +@pytest.mark.flaky(max_runs=3, min_passes=1) def test_create_glossary(capsys): - glossary_id = "test-{}".format(uuid.uuid4()) - translate_v3_create_glossary.create_glossary( - PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id - ) - out, _ = capsys.readouterr() - # assert - assert "Created:" in out - assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out - - # clean up after use try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except Exception: - pass + glossary_id = "test-{}".format(uuid.uuid4()) + translate_v3_create_glossary.create_glossary( + PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id + ) + out, _ = capsys.readouterr() + # assert + assert "Created:" in out + assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out + finally: + # cleanup + @backoff.on_exception( + backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 + ) + def delete_glossary(): + try: + translate_v3_delete_glossary.delete_glossary( + PROJECT_ID, glossary_id) + except NotFound as e: + # Ignoring this case. + print("Got NotFound, detail: {}".format(str(e))) + delete_glossary() diff --git a/translate/cloud-client/translate_v3_delete_glossary_test.py b/translate/cloud-client/translate_v3_delete_glossary_test.py index 8d7fb2d92451..a88c16a37a2b 100644 --- a/translate/cloud-client/translate_v3_delete_glossary_test.py +++ b/translate/cloud-client/translate_v3_delete_glossary_test.py @@ -13,14 +13,18 @@ # limitations under the License. import os +import uuid + +import pytest + import translate_v3_create_glossary import translate_v3_delete_glossary -import uuid -PROJECT_ID = os.environ["GCLOUD_PROJECT"] +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" +@pytest.mark.flaky(max_runs=3, min_passes=1) def test_delete_glossary(capsys): # setup glossary_id = "test-{}".format(uuid.uuid4()) diff --git a/translate/cloud-client/translate_v3_detect_language.py b/translate/cloud-client/translate_v3_detect_language.py index 9b92853cfca1..759eb41a4d30 100644 --- a/translate/cloud-client/translate_v3_detect_language.py +++ b/translate/cloud-client/translate_v3_detect_language.py @@ -16,7 +16,7 @@ from google.cloud import translate -def sample_detect_language(project_id="YOUR_PROJECT_ID"): +def detect_language(project_id="YOUR_PROJECT_ID"): """Detecting the language of a text string.""" client = translate.TranslationServiceClient() diff --git a/translate/cloud-client/translate_v3_detect_language_test.py b/translate/cloud-client/translate_v3_detect_language_test.py index 82620bc550c1..7e9540cca009 100644 --- a/translate/cloud-client/translate_v3_detect_language_test.py +++ b/translate/cloud-client/translate_v3_detect_language_test.py @@ -13,12 +13,14 @@ # limitations under the License. import os + import translate_v3_detect_language -PROJECT_ID = os.environ["GCLOUD_PROJECT"] + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] def test_detect_language(capsys): - translate_v3_detect_language.sample_detect_language(PROJECT_ID) + translate_v3_detect_language.detect_language(PROJECT_ID) out, _ = capsys.readouterr() assert "en" in out diff --git a/translate/cloud-client/translate_v3_get_glossary_test.py b/translate/cloud-client/translate_v3_get_glossary_test.py index 7a4aa167cead..96ea6b78103d 100644 --- a/translate/cloud-client/translate_v3_get_glossary_test.py +++ b/translate/cloud-client/translate_v3_get_glossary_test.py @@ -13,13 +13,19 @@ # limitations under the License. import os +import uuid + +import backoff +from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError +from google.cloud.exceptions import NotFound import pytest + import translate_v3_create_glossary import translate_v3_delete_glossary import translate_v3_get_glossary -import uuid -PROJECT_ID = os.environ["GCLOUD_PROJECT"] + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -33,10 +39,18 @@ def glossary(): yield glossary_id - try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except Exception: - pass + # cleanup + @backoff.on_exception( + backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 + ) + def delete_glossary(): + try: + translate_v3_delete_glossary.delete_glossary( + PROJECT_ID, glossary_id) + except NotFound as e: + # Ignoring this case. + print("Got NotFound, detail: {}".format(str(e))) + delete_glossary() def test_get_glossary(capsys, glossary): diff --git a/translate/cloud-client/translate_v3_get_supported_languages.py b/translate/cloud-client/translate_v3_get_supported_languages.py index eb7d83666a43..6e15458d9cca 100644 --- a/translate/cloud-client/translate_v3_get_supported_languages.py +++ b/translate/cloud-client/translate_v3_get_supported_languages.py @@ -16,7 +16,7 @@ from google.cloud import translate -def sample_get_supported_languages(project_id="YOUR_PROJECT_ID"): +def get_supported_languages(project_id="YOUR_PROJECT_ID"): """Getting a list of supported language codes.""" client = translate.TranslationServiceClient() diff --git a/translate/cloud-client/translate_v3_get_supported_languages_test.py b/translate/cloud-client/translate_v3_get_supported_languages_test.py index b4ba4cd22b30..4bdc0fa4b14f 100644 --- a/translate/cloud-client/translate_v3_get_supported_languages_test.py +++ b/translate/cloud-client/translate_v3_get_supported_languages_test.py @@ -13,12 +13,14 @@ # limitations under the License. import os + import translate_v3_get_supported_languages -PROJECT_ID = os.environ["GCLOUD_PROJECT"] + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] def test_list_languages(capsys): - translate_v3_get_supported_languages.sample_get_supported_languages(PROJECT_ID) + translate_v3_get_supported_languages.get_supported_languages(PROJECT_ID) out, _ = capsys.readouterr() assert "zh-CN" in out diff --git a/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py b/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py index aa40efe4bb7a..8f3f52cfe54d 100644 --- a/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py +++ b/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py @@ -13,9 +13,11 @@ # limitations under the License. import os + import translate_v3_get_supported_languages_with_target as get_supported_langs -PROJECT_ID = os.environ["GCLOUD_PROJECT"] + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] def test_list_languages_with_target(capsys): diff --git a/translate/cloud-client/translate_v3_list_glossary.py b/translate/cloud-client/translate_v3_list_glossary.py index e52d6b260ed9..53e51d85447f 100644 --- a/translate/cloud-client/translate_v3_list_glossary.py +++ b/translate/cloud-client/translate_v3_list_glossary.py @@ -16,7 +16,7 @@ from google.cloud import translate -def sample_list_glossaries(project_id="YOUR_PROJECT_ID"): +def list_glossaries(project_id="YOUR_PROJECT_ID"): """List Glossaries.""" client = translate.TranslationServiceClient() diff --git a/translate/cloud-client/translate_v3_list_glossary_test.py b/translate/cloud-client/translate_v3_list_glossary_test.py index de77daba8028..8f4eaa1ac512 100644 --- a/translate/cloud-client/translate_v3_list_glossary_test.py +++ b/translate/cloud-client/translate_v3_list_glossary_test.py @@ -13,13 +13,19 @@ # limitations under the License. import os +import uuid + +import backoff +from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError +from google.cloud.exceptions import NotFound import pytest + import translate_v3_create_glossary import translate_v3_delete_glossary import translate_v3_list_glossary -import uuid -PROJECT_ID = os.environ["GCLOUD_PROJECT"] + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -33,14 +39,22 @@ def glossary(): yield glossary_id - try: - translate_v3_delete_glossary.sample_delete_glossary(PROJECT_ID, glossary_id) - except Exception: - pass + # clean up + @backoff.on_exception( + backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 + ) + def delete_glossary(): + try: + translate_v3_delete_glossary.delete_glossary( + PROJECT_ID, glossary_id) + except NotFound as e: + # Ignoring this case. + print("Got NotFound, detail: {}".format(str(e))) + delete_glossary() def test_list_glossary(capsys, glossary): - translate_v3_list_glossary.sample_list_glossaries(PROJECT_ID) + translate_v3_list_glossary.list_glossaries(PROJECT_ID) out, _ = capsys.readouterr() assert glossary in out assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out diff --git a/translate/cloud-client/translate_v3_translate_text.py b/translate/cloud-client/translate_v3_translate_text.py index 77fcec26deeb..086f775a37fb 100644 --- a/translate/cloud-client/translate_v3_translate_text.py +++ b/translate/cloud-client/translate_v3_translate_text.py @@ -16,7 +16,7 @@ from google.cloud import translate -def sample_translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"): +def translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"): """Translating Text.""" client = translate.TranslationServiceClient() diff --git a/translate/cloud-client/translate_v3_translate_text_test.py b/translate/cloud-client/translate_v3_translate_text_test.py index a63190197818..c93cb91f6dd5 100644 --- a/translate/cloud-client/translate_v3_translate_text_test.py +++ b/translate/cloud-client/translate_v3_translate_text_test.py @@ -13,13 +13,15 @@ # limitations under the License. import os + import translate_v3_translate_text -PROJECT_ID = os.environ["GCLOUD_PROJECT"] + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] def test_translate_text(capsys): - translate_v3_translate_text.sample_translate_text( + translate_v3_translate_text.translate_text( "Hello World!", PROJECT_ID) out, _ = capsys.readouterr() assert "Bonjour le monde" in out diff --git a/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py b/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py index 72f9d64e2f8d..1caa9e6e9e00 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py +++ b/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py @@ -14,13 +14,19 @@ # limitations under the License. import os +import uuid + +import backoff +from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError +from google.cloud.exceptions import NotFound import pytest + import translate_v3_create_glossary import translate_v3_delete_glossary import translate_v3_translate_text_with_glossary -import uuid -PROJECT_ID = os.environ["GCLOUD_PROJECT"] + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -34,10 +40,18 @@ def glossary(): yield glossary_id - try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except Exception: - pass + # cleanup + @backoff.on_exception( + backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 + ) + def delete_glossary(): + try: + translate_v3_delete_glossary.delete_glossary( + PROJECT_ID, glossary_id) + except NotFound as e: + # Ignoring this case. + print("Got NotFound, detail: {}".format(str(e))) + delete_glossary() def test_translate_text_with_glossary(capsys, glossary): diff --git a/translate/cloud-client/translate_v3_translate_text_with_model_test.py b/translate/cloud-client/translate_v3_translate_text_with_model_test.py index 6e6ab37eee16..f1cd59687521 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model_test.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model_test.py @@ -14,9 +14,11 @@ # limitations under the License. import os + import translate_v3_translate_text_with_model -PROJECT_ID = os.environ["GCLOUD_PROJECT"] + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] MODEL_ID = "TRL3128559826197068699" From 89a40fbb017bbacd34eccc055bd88e33304deb89 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Thu, 11 Jun 2020 20:33:52 +0000 Subject: [PATCH 12/25] remove accidentally committed translate test --- .../hybrid_tutorial_tests.py | 114 ------------------ 1 file changed, 114 deletions(-) delete mode 100644 translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py diff --git a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py deleted file mode 100644 index 87e8420a2955..000000000000 --- a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py +++ /dev/null @@ -1,114 +0,0 @@ -# Copyright 2019 Google LLC -# -# 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 sys - -from hybrid_tutorial import pic_to_text -from hybrid_tutorial import create_glossary -from hybrid_tutorial import translate_text -from hybrid_tutorial import text_to_speech - - -PROJECT_ID = os.environ['GCLOUD_PROJECT'] - - -# VISION TESTS - - -def test_vision_standard_format(): - - expected_text = 'This is\na test!\n' - alt_expected_text = 'This\nis\na test!\n' - - # Generate text using Vision API - text = pic_to_text('resources/standard_format.jpeg') - - assert (text == expected_text) or (text == alt_expected_text) - - -def test_vision_non_standard_format(): - - # Generate text - text = pic_to_text('resources/non_standard_format.png') - - # Read expected text - with open('resources/non_standard_format.txt') as f: - expected_text = f.read() - - assert text == expected_text - - -# TRANSLATE TESTS - - -def test_create_and_delete_glossary(): - sys.path.insert(1, '../') - from beta_snippets import delete_glossary - - languages = ['fr', 'en'] - glossary_name = 'test-glossary' - glossary_uri = 'gs://cloud-samples-data/translation/bistro_glossary.csv' - - # create_glossary will raise an exception if creation fails - create_glossary(languages, PROJECT_ID, glossary_name, - glossary_uri) - - # Delete glossary so that future tests will pass - # delete_glossary will raise an exception if deletion fails - delete_glossary(PROJECT_ID, glossary_name) - - -def test_translate_standard(): - - expected_text = 'Hello' - - text = translate_text('Bonjour', 'fr', 'en', PROJECT_ID, - 'bistro-glossary') - - assert text == expected_text - - -def test_translate_glossary(): - - expected_text = 'I eat goat cheese' - input_text = 'Je mange du chevre' - - text = translate_text(input_text, 'fr', 'en', PROJECT_ID, - 'bistro-glossary') - - assert text == expected_text - - -# TEXT-TO-SPEECH TESTS - - -def test_tts_standard(capsys): - outfile = 'resources/test_standard_text.mp3' - textfile = 'resources/standard_format.txt' - - with open(textfile, 'r') as f: - text = f.read() - - text_to_speech(text, outfile) - - # Assert audio file generated - assert os.path.isfile(outfile) - out, err = capsys.readouterr() - - # Assert success message printed - assert 'Audio content written to file ' + outfile in out - - # Delete test file - os.remove(outfile) From 657b38dfb1b4aeb788f2cf964bd8664dfceddbf2 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Thu, 11 Jun 2020 20:42:55 +0000 Subject: [PATCH 13/25] fix: use GOOGLE_CLOUD_PROJECT --- 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 1b239ce57bcc..13e6456c9d38 100644 --- a/servicedirectory/snippets_test.py +++ b/servicedirectory/snippets_test.py @@ -19,7 +19,7 @@ import snippets from google.cloud import servicedirectory_v1beta1 -PROJECT_ID = environ['GCLOUD_PROJECT'] +PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] LOCATION_ID = environ['GCLOUD_LOCATION'] NAMESPACE_ID = 'test-namespace' SERVICE_ID = 'test-service' From 322e70b186ff3329ebf5839006c7d042da74294c Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Wed, 17 Jun 2020 14:15:49 -0400 Subject: [PATCH 14/25] add quickstart files,generate README.rst, remove metadata from snippets_test --- servicedirectory/README.rst | 85 +++++++++++++++++++++++++++++ servicedirectory/quickstart.py | 32 +++++++++++ servicedirectory/quickstart_test.py | 51 +++++++++++++++++ servicedirectory/snippets_test.py | 10 +--- 4 files changed, 170 insertions(+), 8 deletions(-) create mode 100644 servicedirectory/README.rst create mode 100644 servicedirectory/quickstart.py create mode 100644 servicedirectory/quickstart_test.py diff --git a/servicedirectory/README.rst b/servicedirectory/README.rst new file mode 100644 index 000000000000..896bee9a1112 --- /dev/null +++ b/servicedirectory/README.rst @@ -0,0 +1,85 @@ +.\" Man page generated from reStructuredText. +. +.TH "" "" "" +.SH NAME + \- +. +.nr rst2man-indent-level 0 +. +.de1 rstReportMargin +\\$1 \\n[an-margin] +level \\n[rst2man-indent-level] +level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] +- +\\n[rst2man-indent0] +\\n[rst2man-indent1] +\\n[rst2man-indent2] +.. +.de1 INDENT +.\" .rstReportMargin pre: +. RS \\$1 +. nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] +. nr rst2man-indent-level +1 +.\" .rstReportMargin post: +.. +.de UNINDENT +. RE +.\" indent \\n[an-margin] +.\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] +.nr rst2man-indent-level -1 +.\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] +.in \\n[rst2man-indent\\n[rst2man-indent-level]]u +.. +# This file is used to generate README.rst +.INDENT 0.0 +.TP +.B product: +name: Google Cloud KMS API +short_name: Cloud KMS API +url: \fI\%https://cloud.google.com/kms/docs/\fP +description: > +.IP "System Message: ERROR/3 (../kms/api-client/README.rst.in:, line 8)" +Unexpected indentation. +.INDENT 7.0 +.INDENT 3.5 +The +.nf +\(gaGoogle Cloud KMS API\(ga_ +.fi + is a service that allows you to keep encryption +keys centrally in the cloud, for direct use by cloud services. +.UNINDENT +.UNINDENT +.UNINDENT +.sp +setup: +\- auth +\- install_deps +.sp +samples: +\- name: Quickstart +.IP "System Message: ERROR/3 (../kms/api-client/README.rst.in:, line 17)" +Unexpected indentation. +.INDENT 0.0 +.INDENT 3.5 +file: quickstart.py +.UNINDENT +.UNINDENT +.IP "System Message: WARNING/2 (../kms/api-client/README.rst.in:, line 18)" +Block quote ends without a blank line; unexpected unindent. +.INDENT 0.0 +.IP \(bu 2 +name: Snippets +file: snippets.py +show_help: True +.IP \(bu 2 +name: Asymmetric +file: asymmetric.py +.UNINDENT +.sp +folder: kms/api\-client +.SH DOCUTILS SYSTEM MESSAGES +.IP "System Message: ERROR/3 (../kms/api-client/README.rst.in:, line 8)" +Unknown target name: "google cloud kms api". +.\" Generated by docutils manpage writer. +. diff --git a/servicedirectory/quickstart.py b/servicedirectory/quickstart.py new file mode 100644 index 000000000000..4d890ad11735 --- /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='projects/{0}/locations/{1}'.format(project_id, location_id)) + + print('Listed namespaces in {0}.'.format(location_id)) + for namespace in response: + print('Namespace: {0}'.format(namespace.name)) + + return response diff --git a/servicedirectory/quickstart_test.py b/servicedirectory/quickstart_test.py new file mode 100644 index 000000000000..6c2dfbed1f44 --- /dev/null +++ b/servicedirectory/quickstart_test.py @@ -0,0 +1,51 @@ +#!/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 pytest +import quickstart +from google.cloud import servicedirectory_v1beta1 +from google.api_core import exceptions + +PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] +LOCATION_ID = environ['GOOGLE_CLOUD_LOCATION'] +NAMESPACE_ID = 'test-namespace' + + +def test_list_namespace(): + client = servicedirectory_v1beta1.RegistrationServiceClient() + + namespace = servicedirectory_v1beta1.Namespace( + name='projects/{0}/locations/{1}/namespaces/{2}'.format( + PROJECT_ID, LOCATION_ID, NAMESPACE_ID)) + + created = False; + try: + response = client.get_namespace(name=namespace.name) + except exceptions.NotFound as e: + client.create_namespace( + parent='projects/{0}/locations/{1}'.format(PROJECT_ID, LOCATION_ID), + namespace=namespace, + namespace_id=NAMESPACE_ID, + ) + created = True + + response = quickstart.list_namespaces(PROJECT_ID, LOCATION_ID) + + assert namespace in response.namespaces + + if created: + client.delete_namespace(name=namespace.name) diff --git a/servicedirectory/snippets_test.py b/servicedirectory/snippets_test.py index 13e6456c9d38..9296322336d8 100644 --- a/servicedirectory/snippets_test.py +++ b/servicedirectory/snippets_test.py @@ -20,7 +20,7 @@ from google.cloud import servicedirectory_v1beta1 PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] -LOCATION_ID = environ['GCLOUD_LOCATION'] +LOCATION_ID = environ['GOOGLE_CLOUD_LOCATION'] NAMESPACE_ID = 'test-namespace' SERVICE_ID = 'test-service' ENDPOINT_ID = 'test-endpoint' @@ -33,13 +33,7 @@ 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, - metadata=[[ - 'x-goog-request-params', - 'name=projects/{0}/locations/{1}/namespaces/{2}'.format( - PROJECT_ID, LOCATION_ID, namespace.name) - ]]) + client.delete_namespace(name=namespace.name) def test_create_namespace(): From fb971a644e4803cee7254067aa4cb3f05bfc4b9f Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Wed, 17 Jun 2020 14:18:59 -0400 Subject: [PATCH 15/25] remove README.rst file --- servicedirectory/README.rst | 85 ------------------------------------- 1 file changed, 85 deletions(-) delete mode 100644 servicedirectory/README.rst diff --git a/servicedirectory/README.rst b/servicedirectory/README.rst deleted file mode 100644 index 896bee9a1112..000000000000 --- a/servicedirectory/README.rst +++ /dev/null @@ -1,85 +0,0 @@ -.\" Man page generated from reStructuredText. -. -.TH "" "" "" -.SH NAME - \- -. -.nr rst2man-indent-level 0 -. -.de1 rstReportMargin -\\$1 \\n[an-margin] -level \\n[rst2man-indent-level] -level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] -- -\\n[rst2man-indent0] -\\n[rst2man-indent1] -\\n[rst2man-indent2] -.. -.de1 INDENT -.\" .rstReportMargin pre: -. RS \\$1 -. nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] -. nr rst2man-indent-level +1 -.\" .rstReportMargin post: -.. -.de UNINDENT -. RE -.\" indent \\n[an-margin] -.\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] -.nr rst2man-indent-level -1 -.\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] -.in \\n[rst2man-indent\\n[rst2man-indent-level]]u -.. -# This file is used to generate README.rst -.INDENT 0.0 -.TP -.B product: -name: Google Cloud KMS API -short_name: Cloud KMS API -url: \fI\%https://cloud.google.com/kms/docs/\fP -description: > -.IP "System Message: ERROR/3 (../kms/api-client/README.rst.in:, line 8)" -Unexpected indentation. -.INDENT 7.0 -.INDENT 3.5 -The -.nf -\(gaGoogle Cloud KMS API\(ga_ -.fi - is a service that allows you to keep encryption -keys centrally in the cloud, for direct use by cloud services. -.UNINDENT -.UNINDENT -.UNINDENT -.sp -setup: -\- auth -\- install_deps -.sp -samples: -\- name: Quickstart -.IP "System Message: ERROR/3 (../kms/api-client/README.rst.in:, line 17)" -Unexpected indentation. -.INDENT 0.0 -.INDENT 3.5 -file: quickstart.py -.UNINDENT -.UNINDENT -.IP "System Message: WARNING/2 (../kms/api-client/README.rst.in:, line 18)" -Block quote ends without a blank line; unexpected unindent. -.INDENT 0.0 -.IP \(bu 2 -name: Snippets -file: snippets.py -show_help: True -.IP \(bu 2 -name: Asymmetric -file: asymmetric.py -.UNINDENT -.sp -folder: kms/api\-client -.SH DOCUTILS SYSTEM MESSAGES -.IP "System Message: ERROR/3 (../kms/api-client/README.rst.in:, line 8)" -Unknown target name: "google cloud kms api". -.\" Generated by docutils manpage writer. -. From ad957b854a770d5dc3c19660bcbd2210da25b70c Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Wed, 17 Jun 2020 14:34:18 -0400 Subject: [PATCH 16/25] use api for creating resource paths instead of string format --- servicedirectory/quickstart_test.py | 7 +++--- servicedirectory/snippets.py | 33 +++++++++++++---------------- servicedirectory/snippets_test.py | 3 +-- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/servicedirectory/quickstart_test.py b/servicedirectory/quickstart_test.py index 6c2dfbed1f44..430d3bd64326 100644 --- a/servicedirectory/quickstart_test.py +++ b/servicedirectory/quickstart_test.py @@ -17,8 +17,8 @@ from os import environ import pytest import quickstart -from google.cloud import servicedirectory_v1beta1 from google.api_core import exceptions +from google.cloud import servicedirectory_v1beta1 PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] LOCATION_ID = environ['GOOGLE_CLOUD_LOCATION'] @@ -29,10 +29,9 @@ def test_list_namespace(): client = servicedirectory_v1beta1.RegistrationServiceClient() namespace = servicedirectory_v1beta1.Namespace( - name='projects/{0}/locations/{1}/namespaces/{2}'.format( - PROJECT_ID, LOCATION_ID, NAMESPACE_ID)) + name=client.namespace_path(PROJECT_ID, LOCATION_ID, NAMESPACE_ID)) - created = False; + created = False try: response = client.get_namespace(name=namespace.name) except exceptions.NotFound as e: diff --git a/servicedirectory/snippets.py b/servicedirectory/snippets.py index a55dcf7b36a4..0d5f5185da0e 100644 --- a/servicedirectory/snippets.py +++ b/servicedirectory/snippets.py @@ -23,8 +23,7 @@ def create_namespace(project_id, location_id, namespace_id): client = servicedirectory_v1beta1.RegistrationServiceClient() namespace = servicedirectory_v1beta1.Namespace( - name='projects/{0}/locations/{1}/namespaces/{2}'.format( - project_id, location_id, namespace_id)) + name=client.namespace_path(project_id, location_id, namespace_id)) response = client.create_namespace( parent='projects/{0}/locations/{1}'.format(project_id, location_id), @@ -42,8 +41,7 @@ def delete_namespace(project_id, location_id, namespace_id): client = servicedirectory_v1beta1.RegistrationServiceClient() - namespace_name = 'projects/{0}/locations/{1}/namespaces/{2}'.format( - project_id, location_id, namespace_id) + namespace_name = client.namespace_path(project_id, location_id, namespace_id) client.delete_namespace(name=namespace_name) @@ -56,12 +54,11 @@ def create_service(project_id, location_id, namespace_id, service_id): 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)) + name=client.service_path(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), + parent=client.namespace_path(project_id, location_id, namespace_id), service=service, service_id=service_id, ) @@ -76,8 +73,8 @@ def delete_service(project_id, location_id, namespace_id, service_id): client = servicedirectory_v1beta1.RegistrationServiceClient() - service_name = 'projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format( - project_id, location_id, namespace_id, service_id) + service_name = client.service_path(project_id, location_id, namespace_id, + service_id) client.delete_service(name=service_name) @@ -90,7 +87,7 @@ def resolve_service(project_id, location_id, namespace_id, service_id): client = servicedirectory_v1beta1.LookupServiceClient() request = servicedirectory_v1beta1.ResolveServiceRequest( - name='projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format( + name=servicedirectory_v1beta1.RegistrationServiceClient().service_path( project_id, location_id, namespace_id, service_id)) response = client.resolve_service(request=request) @@ -110,17 +107,17 @@ def create_endpoint(project_id, location_id, namespace_id, service_id, 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), + name=client.endpoint_path(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), + parent=client.service_path(project_id, location_id, namespace_id, + service_id), endpoint=endpoint, endpoint_id=endpoint_id, - ) + ) print('Created endpoint {0}.'.format(response.name)) @@ -133,8 +130,8 @@ def delete_endpoint(project_id, location_id, namespace_id, service_id, 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) + endpoint_name = client.endpoint_path(project_id, location_id, namespace_id, + service_id, endpoint_id) client.delete_endpoint(name=endpoint_name) diff --git a/servicedirectory/snippets_test.py b/servicedirectory/snippets_test.py index 9296322336d8..e5cc451ccaf0 100644 --- a/servicedirectory/snippets_test.py +++ b/servicedirectory/snippets_test.py @@ -15,7 +15,6 @@ # limitations under the License. from os import environ -import pytest import snippets from google.cloud import servicedirectory_v1beta1 @@ -28,7 +27,7 @@ PORT = 443 -def teardown_module(module): +def teardown_module(): client = servicedirectory_v1beta1.RegistrationServiceClient() response = client.list_namespaces( parent='projects/{0}/locations/{1}'.format(PROJECT_ID, LOCATION_ID)) From c3a32feb42984f5df6bf44e32303be259996c81a Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Wed, 24 Jun 2020 11:20:19 -0400 Subject: [PATCH 17/25] change the region tags to be set to 'us-east1', change the namespace test to be a fixture --- servicedirectory/quickstart_test.py | 3 ++- servicedirectory/snippets_test.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/servicedirectory/quickstart_test.py b/servicedirectory/quickstart_test.py index 430d3bd64326..b862157f58a8 100644 --- a/servicedirectory/quickstart_test.py +++ b/servicedirectory/quickstart_test.py @@ -21,10 +21,11 @@ from google.cloud import servicedirectory_v1beta1 PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] -LOCATION_ID = environ['GOOGLE_CLOUD_LOCATION'] +LOCATION_ID = 'us-east1' NAMESPACE_ID = 'test-namespace' +@pytest.fixture(scope='module') def test_list_namespace(): client = servicedirectory_v1beta1.RegistrationServiceClient() diff --git a/servicedirectory/snippets_test.py b/servicedirectory/snippets_test.py index e5cc451ccaf0..e80f09084cc6 100644 --- a/servicedirectory/snippets_test.py +++ b/servicedirectory/snippets_test.py @@ -19,7 +19,7 @@ from google.cloud import servicedirectory_v1beta1 PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] -LOCATION_ID = environ['GOOGLE_CLOUD_LOCATION'] +LOCATION_ID = 'us-east1' NAMESPACE_ID = 'test-namespace' SERVICE_ID = 'test-service' ENDPOINT_ID = 'test-endpoint' From 36b2da6cf2f09aa46c82ee5b0260b5ed418f26dc Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Wed, 24 Jun 2020 11:52:51 -0400 Subject: [PATCH 18/25] change test to fixture --- servicedirectory/quickstart_test.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/servicedirectory/quickstart_test.py b/servicedirectory/quickstart_test.py index b862157f58a8..b9e521ef6fc0 100644 --- a/servicedirectory/quickstart_test.py +++ b/servicedirectory/quickstart_test.py @@ -26,15 +26,21 @@ @pytest.fixture(scope='module') -def test_list_namespace(): - client = servicedirectory_v1beta1.RegistrationServiceClient() +def client(): + return servicedirectory_v1beta1.RegistrationServiceClient() - namespace = servicedirectory_v1beta1.Namespace( + +@pytest.fixture(scope='module') +def namespace(client): + return servicedirectory_v1beta1.Namespace( name=client.namespace_path(PROJECT_ID, LOCATION_ID, NAMESPACE_ID)) + +@pytest.fixture(scope='module') +def list_namespace(client, namespace): created = False try: - response = client.get_namespace(name=namespace.name) + client.get_namespace(name=namespace.name) except exceptions.NotFound as e: client.create_namespace( parent='projects/{0}/locations/{1}'.format(PROJECT_ID, LOCATION_ID), @@ -43,9 +49,11 @@ def test_list_namespace(): ) created = True - response = quickstart.list_namespaces(PROJECT_ID, LOCATION_ID) - - assert namespace in response.namespaces + yield quickstart.list_namespaces(PROJECT_ID, LOCATION_ID) if created: client.delete_namespace(name=namespace.name) + + +def test_list_namespace(namespace, list_namespace): + assert namespace in list_namespace.namespaces From 8a991f530216c18f9c86774b7ed68a001213a6d6 Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Thu, 16 Jul 2020 10:59:05 -0400 Subject: [PATCH 19/25] change strings to f-strings, make namespace, service, and endpoint ids unique --- servicedirectory/quickstart.py | 6 +++--- servicedirectory/quickstart_test.py | 23 ++++++++++------------- servicedirectory/requirements-test.txt | 2 +- servicedirectory/snippets.py | 17 ++++++++--------- servicedirectory/snippets_test.py | 11 +++++++---- 5 files changed, 29 insertions(+), 30 deletions(-) diff --git a/servicedirectory/quickstart.py b/servicedirectory/quickstart.py index 4d890ad11735..79f6eae369ee 100644 --- a/servicedirectory/quickstart.py +++ b/servicedirectory/quickstart.py @@ -23,10 +23,10 @@ def list_namespaces(project_id, location_id): client = servicedirectory_v1beta1.RegistrationServiceClient() response = client.list_namespaces( - parent='projects/{0}/locations/{1}'.format(project_id, location_id)) + parent=f'projects/{project_id}/locations/{location_id}') - print('Listed namespaces in {0}.'.format(location_id)) + print(f'Listed namespaces in {location_id}.') for namespace in response: - print('Namespace: {0}'.format(namespace.name)) + print(f'Namespace: {namespace.name}') return response diff --git a/servicedirectory/quickstart_test.py b/servicedirectory/quickstart_test.py index b9e521ef6fc0..294425f96a62 100644 --- a/servicedirectory/quickstart_test.py +++ b/servicedirectory/quickstart_test.py @@ -15,14 +15,17 @@ # limitations under the License. from os import environ +import uuid + import pytest import quickstart + from google.api_core import exceptions from google.cloud import servicedirectory_v1beta1 PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] LOCATION_ID = 'us-east1' -NAMESPACE_ID = 'test-namespace' +NAMESPACE_ID = f'test-namespace-{uuid.uuid4().hex}' @pytest.fixture(scope='module') @@ -38,21 +41,15 @@ def namespace(client): @pytest.fixture(scope='module') def list_namespace(client, namespace): - created = False - try: - client.get_namespace(name=namespace.name) - except exceptions.NotFound as e: - client.create_namespace( - parent='projects/{0}/locations/{1}'.format(PROJECT_ID, LOCATION_ID), - namespace=namespace, - namespace_id=NAMESPACE_ID, - ) - created = True + client.create_namespace( + parent=f'projects/{PROJECT_ID}/locations/{LOCATION_ID}', + namespace=namespace, + namespace_id=NAMESPACE_ID, + ) yield quickstart.list_namespaces(PROJECT_ID, LOCATION_ID) - if created: - client.delete_namespace(name=namespace.name) + client.delete_namespace(name=namespace.name) def test_list_namespace(namespace, list_namespace): diff --git a/servicedirectory/requirements-test.txt b/servicedirectory/requirements-test.txt index 781d4326c947..79738af5f268 100644 --- a/servicedirectory/requirements-test.txt +++ b/servicedirectory/requirements-test.txt @@ -1 +1 @@ -pytest==5.3.2 +pytest==5.4.3 diff --git a/servicedirectory/snippets.py b/servicedirectory/snippets.py index 0d5f5185da0e..a3c7d9acf476 100644 --- a/servicedirectory/snippets.py +++ b/servicedirectory/snippets.py @@ -26,12 +26,12 @@ def create_namespace(project_id, location_id, namespace_id): name=client.namespace_path(project_id, location_id, namespace_id)) response = client.create_namespace( - parent='projects/{0}/locations/{1}'.format(project_id, location_id), + parent=f'projects/{project_id}/locations/{location_id}', namespace=namespace, namespace_id=namespace_id, ) - print('Created namespace {0}.'.format(response.name)) + print(f'Created namespace {response.name}.') return response @@ -45,7 +45,7 @@ def delete_namespace(project_id, location_id, namespace_id): client.delete_namespace(name=namespace_name) - print('Deleted namespace {0}.'.format(namespace_name)) + print('Deleted namespace {namespace_name}.') def create_service(project_id, location_id, namespace_id, service_id): @@ -63,7 +63,7 @@ def create_service(project_id, location_id, namespace_id, service_id): service_id=service_id, ) - print('Created service {0}.'.format(response.name)) + print(f'Created service {response.name}.') return response @@ -78,7 +78,7 @@ def delete_service(project_id, location_id, namespace_id, service_id): client.delete_service(name=service_name) - print('Deleted service {0}.'.format(service_name)) + print(f'Deleted service {service_name}.') def resolve_service(project_id, location_id, namespace_id, service_id): @@ -94,8 +94,7 @@ def resolve_service(project_id, location_id, namespace_id, service_id): print('Endpoints found:') for endpoint in response.service.endpoints: - print('{0} -- {1}:{2}'.format(endpoint.name, endpoint.address, - endpoint.port)) + print(f'{endpoint.name} -- {endpoint.address}:{endpoint.port}') return response @@ -119,7 +118,7 @@ def create_endpoint(project_id, location_id, namespace_id, service_id, endpoint_id=endpoint_id, ) - print('Created endpoint {0}.'.format(response.name)) + print(f'Created endpoint {response.name}.') return response @@ -135,4 +134,4 @@ def delete_endpoint(project_id, location_id, namespace_id, service_id, client.delete_endpoint(name=endpoint_name) - print('Deleted endpoint {0}.'.format(endpoint_name)) + print(f'Deleted endpoint {endpoint_name}.') diff --git a/servicedirectory/snippets_test.py b/servicedirectory/snippets_test.py index e80f09084cc6..a3e622a9a293 100644 --- a/servicedirectory/snippets_test.py +++ b/servicedirectory/snippets_test.py @@ -15,14 +15,17 @@ # limitations under the License. from os import environ +import uuid + import snippets + from google.cloud import servicedirectory_v1beta1 PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] LOCATION_ID = 'us-east1' -NAMESPACE_ID = 'test-namespace' -SERVICE_ID = 'test-service' -ENDPOINT_ID = 'test-endpoint' +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 @@ -30,7 +33,7 @@ def teardown_module(): client = servicedirectory_v1beta1.RegistrationServiceClient() response = client.list_namespaces( - parent='projects/{0}/locations/{1}'.format(PROJECT_ID, LOCATION_ID)) + parent=f'projects/{PROJECT_ID}/locations/{LOCATION_ID}') for namespace in response.namespaces: client.delete_namespace(name=namespace.name) From a64af6d8f9c97ad87456dec5750c39190b987681 Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Thu, 16 Jul 2020 11:14:39 -0400 Subject: [PATCH 20/25] missed an f-string, added it to delete namespace --- servicedirectory/snippets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/servicedirectory/snippets.py b/servicedirectory/snippets.py index a3c7d9acf476..2415f4831430 100644 --- a/servicedirectory/snippets.py +++ b/servicedirectory/snippets.py @@ -45,7 +45,7 @@ def delete_namespace(project_id, location_id, namespace_id): client.delete_namespace(name=namespace_name) - print('Deleted namespace {namespace_name}.') + print(f'Deleted namespace {namespace_name}.') def create_service(project_id, location_id, namespace_id, service_id): From 63e36f3fec6f8de7af9a46afc0bf3c8f07063297 Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Fri, 17 Jul 2020 07:36:27 -0400 Subject: [PATCH 21/25] move list_namespace fixture into namespace fixture, remove unnecessary import --- servicedirectory/quickstart_test.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/servicedirectory/quickstart_test.py b/servicedirectory/quickstart_test.py index 294425f96a62..343d5504e657 100644 --- a/servicedirectory/quickstart_test.py +++ b/servicedirectory/quickstart_test.py @@ -20,7 +20,6 @@ import pytest import quickstart -from google.api_core import exceptions from google.cloud import servicedirectory_v1beta1 PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] @@ -35,22 +34,20 @@ def client(): @pytest.fixture(scope='module') def namespace(client): - return servicedirectory_v1beta1.Namespace( + namespace = servicedirectory_v1beta1.Namespace( name=client.namespace_path(PROJECT_ID, LOCATION_ID, NAMESPACE_ID)) - -@pytest.fixture(scope='module') -def list_namespace(client, namespace): client.create_namespace( parent=f'projects/{PROJECT_ID}/locations/{LOCATION_ID}', namespace=namespace, namespace_id=NAMESPACE_ID, ) - yield quickstart.list_namespaces(PROJECT_ID, LOCATION_ID) + yield namespace client.delete_namespace(name=namespace.name) -def test_list_namespace(namespace, list_namespace): - assert namespace in list_namespace.namespaces +def test_list_namespace(namespace): + assert namespace in quickstart.list_namespaces(PROJECT_ID, + LOCATION_ID).namespaces From 542910aaf85c8795588bc266887f90fe3e1bf28f Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Wed, 22 Jul 2020 07:25:45 -0400 Subject: [PATCH 22/25] add generated README.rst file --- servicedirectory/README.rst | 87 +++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 servicedirectory/README.rst diff --git a/servicedirectory/README.rst b/servicedirectory/README.rst new file mode 100644 index 000000000000..c180e70f9ee4 --- /dev/null +++ b/servicedirectory/README.rst @@ -0,0 +1,87 @@ +.. 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/ + + + + + + +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 From e39311432e92bed07160e4619a2f2abe86ff809e Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Thu, 23 Jul 2020 08:47:20 -0400 Subject: [PATCH 23/25] add requirements to readme and regenerate the file --- servicedirectory/README.rst | 2 ++ servicedirectory/README.rst.in | 3 +++ 2 files changed, 5 insertions(+) diff --git a/servicedirectory/README.rst b/servicedirectory/README.rst index c180e70f9ee4..c021fef673c0 100644 --- a/servicedirectory/README.rst +++ b/servicedirectory/README.rst @@ -14,7 +14,9 @@ This directory contains samples for Google Cloud Service Directory. `Google Clou .. _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. diff --git a/servicedirectory/README.rst.in b/servicedirectory/README.rst.in index 7b786751795a..0ed61d222cca 100644 --- a/servicedirectory/README.rst.in +++ b/servicedirectory/README.rst.in @@ -13,6 +13,9 @@ product: 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 From c9d52f560c3b695aa35b2c1a04afb301d7b5a986 Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Fri, 24 Jul 2020 09:07:02 -0400 Subject: [PATCH 24/25] fix linter errors --- servicedirectory/quickstart.py | 16 ++-- servicedirectory/quickstart_test.py | 29 ++++--- servicedirectory/snippets.py | 130 ++++++++++++++-------------- servicedirectory/snippets_test.py | 58 ++++++------- 4 files changed, 117 insertions(+), 116 deletions(-) diff --git a/servicedirectory/quickstart.py b/servicedirectory/quickstart.py index 79f6eae369ee..c6ccfbc7fc0e 100644 --- a/servicedirectory/quickstart.py +++ b/servicedirectory/quickstart.py @@ -18,15 +18,15 @@ def list_namespaces(project_id, location_id): - """Lists all namespaces in the given location.""" + """Lists all namespaces in the given location.""" - client = servicedirectory_v1beta1.RegistrationServiceClient() + client = servicedirectory_v1beta1.RegistrationServiceClient() - response = client.list_namespaces( - parent=f'projects/{project_id}/locations/{location_id}') + 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}') + print(f'Listed namespaces in {location_id}.') + for namespace in response: + print(f'Namespace: {namespace.name}') - return response + return response diff --git a/servicedirectory/quickstart_test.py b/servicedirectory/quickstart_test.py index 343d5504e657..b17540e2636b 100644 --- a/servicedirectory/quickstart_test.py +++ b/servicedirectory/quickstart_test.py @@ -17,10 +17,11 @@ from os import environ import uuid +from google.cloud import servicedirectory_v1beta1 + import pytest -import quickstart -from google.cloud import servicedirectory_v1beta1 +import quickstart PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] LOCATION_ID = 'us-east1' @@ -29,25 +30,25 @@ @pytest.fixture(scope='module') def client(): - return servicedirectory_v1beta1.RegistrationServiceClient() + 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)) + 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, - ) + client.create_namespace( + parent=f'projects/{PROJECT_ID}/locations/{LOCATION_ID}', + namespace=namespace, + namespace_id=NAMESPACE_ID, + ) - yield namespace + yield namespace - client.delete_namespace(name=namespace.name) + client.delete_namespace(name=namespace.name) def test_list_namespace(namespace): - assert namespace in quickstart.list_namespaces(PROJECT_ID, - LOCATION_ID).namespaces + assert namespace in quickstart.list_namespaces(PROJECT_ID, + LOCATION_ID).namespaces diff --git a/servicedirectory/snippets.py b/servicedirectory/snippets.py index 2415f4831430..1bb17eb6ea1a 100644 --- a/servicedirectory/snippets.py +++ b/servicedirectory/snippets.py @@ -18,120 +18,120 @@ def create_namespace(project_id, location_id, namespace_id): - """Creates a namespace in the given location.""" + """Creates a namespace in the given location.""" - client = servicedirectory_v1beta1.RegistrationServiceClient() + client = servicedirectory_v1beta1.RegistrationServiceClient() - namespace = servicedirectory_v1beta1.Namespace( - name=client.namespace_path(project_id, location_id, namespace_id)) + 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, - ) + response = client.create_namespace( + parent=f'projects/{project_id}/locations/{location_id}', + namespace=namespace, + namespace_id=namespace_id, + ) - print(f'Created namespace {response.name}.') + print(f'Created namespace {response.name}.') - return response + return response def delete_namespace(project_id, location_id, namespace_id): - """Deletes a namespace in the given location.""" + """Deletes a namespace in the given location.""" - client = servicedirectory_v1beta1.RegistrationServiceClient() + client = servicedirectory_v1beta1.RegistrationServiceClient() - namespace_name = client.namespace_path(project_id, location_id, namespace_id) + namespace_name = client.namespace_path(project_id, location_id, namespace_id) - client.delete_namespace(name=namespace_name) + client.delete_namespace(name=namespace_name) - print(f'Deleted namespace {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.""" + """Creates a service in the given namespace.""" - client = servicedirectory_v1beta1.RegistrationServiceClient() + client = servicedirectory_v1beta1.RegistrationServiceClient() - service = servicedirectory_v1beta1.Service( - name=client.service_path(project_id, location_id, namespace_id, - service_id)) + 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, - ) + 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}.') + print(f'Created service {response.name}.') - return response + return response def delete_service(project_id, location_id, namespace_id, service_id): - """Deletes a service in the given namespace.""" + """Deletes a service in the given namespace.""" - client = servicedirectory_v1beta1.RegistrationServiceClient() + client = servicedirectory_v1beta1.RegistrationServiceClient() - service_name = client.service_path(project_id, location_id, namespace_id, - service_id) + service_name = client.service_path(project_id, location_id, namespace_id, + service_id) - client.delete_service(name=service_name) + client.delete_service(name=service_name) - print(f'Deleted service {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.""" + """Resolves a service in the given namespace.""" - client = servicedirectory_v1beta1.LookupServiceClient() + client = servicedirectory_v1beta1.LookupServiceClient() - request = servicedirectory_v1beta1.ResolveServiceRequest( - name=servicedirectory_v1beta1.RegistrationServiceClient().service_path( - project_id, location_id, namespace_id, service_id)) + request = servicedirectory_v1beta1.ResolveServiceRequest( + name=servicedirectory_v1beta1.RegistrationServiceClient().service_path( + project_id, location_id, namespace_id, service_id)) - response = client.resolve_service(request=request) + response = client.resolve_service(request=request) - print('Endpoints found:') - for endpoint in response.service.endpoints: - print(f'{endpoint.name} -- {endpoint.address}:{endpoint.port}') + print('Endpoints found:') + for endpoint in response.service.endpoints: + print(f'{endpoint.name} -- {endpoint.address}:{endpoint.port}') - return response + return response def create_endpoint(project_id, location_id, namespace_id, service_id, endpoint_id, address, port): - """Creates a endpoint in the given service.""" + """Creates a endpoint in the given service.""" - client = servicedirectory_v1beta1.RegistrationServiceClient() + 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) + 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, - ) + 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}.') + print(f'Created endpoint {response.name}.') - return response + return response def delete_endpoint(project_id, location_id, namespace_id, service_id, endpoint_id): - """Deletes a endpoin in the given service.""" + """Deletes a endpoin in the given service.""" - client = servicedirectory_v1beta1.RegistrationServiceClient() + client = servicedirectory_v1beta1.RegistrationServiceClient() - endpoint_name = client.endpoint_path(project_id, location_id, namespace_id, - service_id, endpoint_id) + endpoint_name = client.endpoint_path(project_id, location_id, namespace_id, + service_id, endpoint_id) - client.delete_endpoint(name=endpoint_name) + client.delete_endpoint(name=endpoint_name) - print(f'Deleted endpoint {endpoint_name}.') + print(f'Deleted endpoint {endpoint_name}.') diff --git a/servicedirectory/snippets_test.py b/servicedirectory/snippets_test.py index a3e622a9a293..d224adfbc2da 100644 --- a/servicedirectory/snippets_test.py +++ b/servicedirectory/snippets_test.py @@ -17,10 +17,10 @@ from os import environ import uuid -import snippets - 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}' @@ -31,58 +31,58 @@ def teardown_module(): - client = servicedirectory_v1beta1.RegistrationServiceClient() - response = client.list_namespaces( - parent=f'projects/{PROJECT_ID}/locations/{LOCATION_ID}') - for namespace in response.namespaces: - client.delete_namespace(name=namespace.name) + client = servicedirectory_v1beta1.RegistrationServiceClient() + response = client.list_namespaces( + parent=f'projects/{PROJECT_ID}/locations/{LOCATION_ID}') + 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) + response = snippets.create_namespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID) - assert NAMESPACE_ID in response.name + assert NAMESPACE_ID in response.name def test_create_service(): - response = snippets.create_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, - SERVICE_ID) + response = snippets.create_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, + SERVICE_ID) - assert SERVICE_ID in response.name + 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) + response = snippets.create_endpoint(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, + SERVICE_ID, ENDPOINT_ID, ADDRESS, PORT) - assert ENDPOINT_ID in response.name + assert ENDPOINT_ID in response.name def test_resolve_service(): - response = snippets.resolve_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, - SERVICE_ID) + 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 + 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) + snippets.delete_endpoint(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, + ENDPOINT_ID) - out, _ = capsys.readouterr() - assert ENDPOINT_ID in out + out, _ = capsys.readouterr() + assert ENDPOINT_ID in out def test_delete_service(capsys): - snippets.delete_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID) + snippets.delete_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID) - out, _ = capsys.readouterr() - assert SERVICE_ID in out + out, _ = capsys.readouterr() + assert SERVICE_ID in out def test_delete_namespace(capsys): - snippets.delete_namespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID) + snippets.delete_namespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID) - out, _ = capsys.readouterr() - assert NAMESPACE_ID in out + out, _ = capsys.readouterr() + assert NAMESPACE_ID in out From 6680dc80273dc22748e88cd1fd82cbe6b81589fe Mon Sep 17 00:00:00 2001 From: Rebecca Shaw Date: Fri, 24 Jul 2020 15:14:04 -0400 Subject: [PATCH 25/25] only delete the namespace created upon teardown --- servicedirectory/snippets_test.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/servicedirectory/snippets_test.py b/servicedirectory/snippets_test.py index d224adfbc2da..f9891b047766 100644 --- a/servicedirectory/snippets_test.py +++ b/servicedirectory/snippets_test.py @@ -17,6 +17,7 @@ from os import environ import uuid +from google.api_core import exceptions from google.cloud import servicedirectory_v1beta1 import snippets @@ -32,10 +33,12 @@ def teardown_module(): client = servicedirectory_v1beta1.RegistrationServiceClient() - response = client.list_namespaces( - parent=f'projects/{PROJECT_ID}/locations/{LOCATION_ID}') - for namespace in response.namespaces: + 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():