From ce558acef9ec9c9bcc54243cddb708ef168c05f0 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Wed, 19 May 2021 15:05:17 -0600 Subject: [PATCH] fix: remove support for google-api-core<1.26.0 (#893) --- .../services/%service/transports/base.py.j2 | 26 +----- .../services/%service/transports/grpc.py.j2 | 6 +- .../%service/transports/grpc_asyncio.py.j2 | 6 +- .../services/%service/transports/rest.py.j2 | 6 +- gapic/templates/setup.py.j2 | 2 +- .../%name_%version/%sub/test_%service.py.j2 | 89 +------------------ 6 files changed, 15 insertions(+), 120 deletions(-) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 index 819448a18b..0570b93093 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 @@ -46,8 +46,6 @@ except AttributeError: except pkg_resources.DistributionNotFound: # pragma: NO COVER _GOOGLE_AUTH_VERSION = None -_API_CORE_VERSION = google.api_core.__version__ - class {{ service.name }}Transport(abc.ABC): """Abstract transport class for {{ service.name }}.""" @@ -121,10 +119,9 @@ class {{ service.name }}Transport(abc.ABC): self._credentials = credentials - # TODO(busunkim): These two class methods are in the base transport + # TODO(busunkim): This method is in the base transport # to avoid duplicating code across the transport classes. These functions - # should be deleted once the minimum required versions of google-api-core - # and google-auth are increased. + # should be deleted once the minimum required versions of google-auth is increased. # TODO: Remove this function once google-auth >= 1.25.0 is required @classmethod @@ -143,25 +140,6 @@ class {{ service.name }}Transport(abc.ABC): return scopes_kwargs - # TODO: Remove this function once google-api-core >= 1.26.0 is required - @classmethod - def _get_self_signed_jwt_kwargs(cls, host: str, scopes: Optional[Sequence[str]]) -> Dict[str, Union[Optional[Sequence[str]], str]]: - """Returns kwargs to pass to grpc_helpers.create_channel depending on the google-api-core version""" - - self_signed_jwt_kwargs: Dict[str, Union[Optional[Sequence[str]], str]] = {} - - if _API_CORE_VERSION and ( - packaging.version.parse(_API_CORE_VERSION) - >= packaging.version.parse("1.26.0") - ): - self_signed_jwt_kwargs["default_scopes"] = cls.AUTH_SCOPES - self_signed_jwt_kwargs["scopes"] = scopes - self_signed_jwt_kwargs["default_host"] = cls.DEFAULT_HOST - else: - self_signed_jwt_kwargs["scopes"] = scopes or cls.AUTH_SCOPES - - return self_signed_jwt_kwargs - def _prep_wrapped_messages(self, client_info): # Precompute the wrapped methods. diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 index c266473ace..3d9fbf9820 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 @@ -204,14 +204,14 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): and ``credentials_file`` are passed. """ - self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes) - return grpc_helpers.create_channel( host, credentials=credentials, credentials_file=credentials_file, quota_project_id=quota_project_id, - **self_signed_jwt_kwargs, + default_scopes=cls.AUTH_SCOPES, + scopes=scopes, + default_host=cls.DEFAULT_HOST, **kwargs ) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 index 5c02730a19..0b72ecfe9e 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 @@ -77,14 +77,14 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport): aio.Channel: A gRPC AsyncIO channel object. """ - self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes) - return grpc_helpers_async.create_channel( host, credentials=credentials, credentials_file=credentials_file, quota_project_id=quota_project_id, - **self_signed_jwt_kwargs, + default_scopes=cls.AUTH_SCOPES, + scopes=scopes, + default_host=cls.DEFAULT_HOST, **kwargs ) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 index 24af43d763..bf3cec5efc 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 @@ -109,13 +109,13 @@ class {{ service.name }}RestTransport({{ service.name }}Transport): if self._operations_client is None: from google.api_core import grpc_helpers - self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(self._host, self._scopes) - self._operations_client = operations_v1.OperationsClient( grpc_helpers.create_channel( self._host, credentials=self._credentials, - **self_signed_jwt_kwargs, + default_scopes=cls.AUTH_SCOPES, + scopes=self._scopes, + default_host=cls.DEFAULT_HOST, options=[ ("grpc.max_send_message_length", -1), ("grpc.max_receive_message_length", -1), diff --git a/gapic/templates/setup.py.j2 b/gapic/templates/setup.py.j2 index 9c97573fc0..07d9535c6a 100644 --- a/gapic/templates/setup.py.j2 +++ b/gapic/templates/setup.py.j2 @@ -27,7 +27,7 @@ setuptools.setup( platforms='Posix; MacOS X; Windows', include_package_data=True, install_requires=( - 'google-api-core[grpc] >= 1.22.2, < 2.0.0dev', + 'google-api-core[grpc] >= 1.26.0, < 2.0.0dev', 'libcst >= 0.2.5', 'proto-plus >= 1.15.0', 'packaging >= 14.3', diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index f6ef2fba9b..5e436e33bf 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -29,7 +29,6 @@ from {{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + ser {% endif %} from {{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }} import transports from {{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.base import _GOOGLE_AUTH_VERSION -from {{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.base import _API_CORE_VERSION from google.api_core import client_options from google.api_core import exceptions as core_exceptions from google.api_core import grpc_helpers @@ -55,8 +54,9 @@ from google.iam.v1 import policy_pb2 # type: ignore {% endfilter %} -# TODO(busunkim): Once google-api-core >= 1.26.0 is required: -# - Delete all the api-core and auth "less than" test cases +# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively +# through google-api-core: +# - Delete the auth "less than" test cases # - Delete these pytest markers (Make the "greater than or equal to" tests the default). requires_google_auth_lt_1_25_0 = pytest.mark.skipif( packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"), @@ -67,16 +67,6 @@ requires_google_auth_gte_1_25_0 = pytest.mark.skipif( reason="This test requires google-auth >= 1.25.0", ) -requires_api_core_lt_1_26_0 = pytest.mark.skipif( - packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"), - reason="This test requires google-api-core < 1.26.0", -) - -requires_api_core_gte_1_26_0 = pytest.mark.skipif( - packaging.version.parse(_API_CORE_VERSION) < packaging.version.parse("1.26.0"), - reason="This test requires google-api-core >= 1.26.0", -) - def client_cert_source_callback(): return b"cert bytes", b"key bytes" @@ -1610,7 +1600,6 @@ def test_{{ service.name|snake_case }}_transport_auth_adc_old_google_auth(transp (transports.{{ service.name }}GrpcAsyncIOTransport, grpc_helpers_async) ], ) -@requires_api_core_gte_1_26_0 def test_{{ service.name|snake_case }}_transport_create_channel(transport_class, grpc_helpers): # If credentials and host are not provided, the transport class should use # ADC credentials. @@ -1644,78 +1633,6 @@ def test_{{ service.name|snake_case }}_transport_create_channel(transport_class, ) {% endwith %} - -@pytest.mark.parametrize( - "transport_class,grpc_helpers", - [ - (transports.{{ service.name }}GrpcTransport, grpc_helpers), - (transports.{{ service.name }}GrpcAsyncIOTransport, grpc_helpers_async) - ], -) -@requires_api_core_lt_1_26_0 -def test_{{ service.name|snake_case }}_transport_create_channel_old_api_core(transport_class, grpc_helpers): - # If credentials and host are not provided, the transport class should use - # ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch.object( - grpc_helpers, "create_channel", autospec=True - ) as create_channel: - creds = ga_credentials.AnonymousCredentials() - adc.return_value = (creds, None) - transport_class(quota_project_id="octopus") - - {% with host = (service.host|default('localhost', true)) %} - create_channel.assert_called_with( - "{{ host }}{% if ":" not in service.host %}:443{% endif %}", - credentials=creds, - credentials_file=None, - quota_project_id="octopus", - scopes=( - {% for scope in service.oauth_scopes %} - '{{ scope }}', - {% endfor %}), - ssl_credentials=None, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - {% endwith %} - - -@pytest.mark.parametrize( - "transport_class,grpc_helpers", - [ - (transports.{{ service.name }}GrpcTransport, grpc_helpers), - (transports.{{ service.name }}GrpcAsyncIOTransport, grpc_helpers_async) - ], -) -@requires_api_core_lt_1_26_0 -def test_{{ service.name|snake_case }}_transport_create_channel_user_scopes(transport_class, grpc_helpers): - # If credentials and host are not provided, the transport class should use - # ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch.object( - grpc_helpers, "create_channel", autospec=True - ) as create_channel: - creds = ga_credentials.AnonymousCredentials() - adc.return_value = (creds, None) - {% with host = (service.host|default('localhost', true)) %} - - transport_class(quota_project_id="octopus", scopes=["1", "2"]) - - create_channel.assert_called_with( - "{{ host }}{% if ":" not in service.host %}:443{% endif %}", - credentials=creds, - credentials_file=None, - quota_project_id="octopus", - scopes=["1", "2"], - ssl_credentials=None, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - {% endwith %} - {% endif %} {% if 'grpc' in opts.transport %}