From eb132c0f62725edcdb2c6e239229bfc5ce31e7ce Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Fri, 8 Oct 2021 14:22:25 +0000 Subject: [PATCH] feat: add context manager support in client (#8) - [ ] Regenerate this pull request now. chore: fix docstring for first attribute of protos committer: @busunkim96 PiperOrigin-RevId: 401271153 Source-Link: https://github.com/googleapis/googleapis/commit/787f8c9a731f44e74a90b9847d48659ca9462d10 Source-Link: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9 --- .../migration_service/async_client.py | 6 +++ .../services/migration_service/client.py | 18 +++++-- .../migration_service/transports/base.py | 9 ++++ .../migration_service/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../types/migration_error_details.py | 2 + .../types/migration_metrics.py | 3 ++ .../types/migration_service.py | 9 ++++ .../test_migration_service.py | 50 +++++++++++++++++++ 9 files changed, 99 insertions(+), 4 deletions(-) diff --git a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/async_client.py b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/async_client.py index 93200fac0424..ca12b6fdc51f 100644 --- a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/async_client.py +++ b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/async_client.py @@ -744,6 +744,12 @@ async def list_migration_subtasks( # Done; return the response. return response + async def __aenter__(self): + return self + + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/client.py b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/client.py index 0a70fb7a90dd..6c064e515eb1 100644 --- a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/client.py +++ b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/client.py @@ -367,10 +367,7 @@ def __init__( client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, - always_use_jwt_access=( - Transport == type(self).get_transport_class("grpc") - or Transport == type(self).get_transport_class("grpc_asyncio") - ), + always_use_jwt_access=True, ) def create_migration_workflow( @@ -902,6 +899,19 @@ def list_migration_subtasks( # Done; return the response. return response + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/transports/base.py b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/transports/base.py index a37def0b91ef..9fc2dd94237f 100644 --- a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/transports/base.py +++ b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/transports/base.py @@ -238,6 +238,15 @@ def _prep_wrapped_messages(self, client_info): ), } + def close(self): + """Closes resources associated with the transport. + + .. warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ + raise NotImplementedError() + @property def create_migration_workflow( self, diff --git a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/transports/grpc.py b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/transports/grpc.py index 359b9684837c..f4c740ab0948 100644 --- a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/transports/grpc.py +++ b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/transports/grpc.py @@ -428,5 +428,8 @@ def list_migration_subtasks( ) return self._stubs["list_migration_subtasks"] + def close(self): + self.grpc_channel.close() + __all__ = ("MigrationServiceGrpcTransport",) diff --git a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/transports/grpc_asyncio.py b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/transports/grpc_asyncio.py index 3384e26d9086..a0ef95910c41 100644 --- a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/transports/grpc_asyncio.py +++ b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/services/migration_service/transports/grpc_asyncio.py @@ -435,5 +435,8 @@ def list_migration_subtasks( ) return self._stubs["list_migration_subtasks"] + def close(self): + return self.grpc_channel.close() + __all__ = ("MigrationServiceGrpcAsyncIOTransport",) diff --git a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/types/migration_error_details.py b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/types/migration_error_details.py index 1880b2b4b7ac..c9df602e84e8 100644 --- a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/types/migration_error_details.py +++ b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/types/migration_error_details.py @@ -26,6 +26,7 @@ class ResourceErrorDetail(proto.Message): r"""Provides details for errors and the corresponding resources. + Attributes: resource_info (google.rpc.error_details_pb2.ResourceInfo): Required. Information about the resource @@ -67,6 +68,7 @@ class ErrorDetail(proto.Message): class ErrorLocation(proto.Message): r"""Holds information about where the error is located. + Attributes: line (int): Optional. If applicable, denotes the line diff --git a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/types/migration_metrics.py b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/types/migration_metrics.py index 6f08b9ee90ee..35c16439c173 100644 --- a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/types/migration_metrics.py +++ b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/types/migration_metrics.py @@ -28,6 +28,7 @@ class TimeSeries(proto.Message): r"""The metrics object for a SubTask. + Attributes: metric (str): Required. The name of the metric. @@ -67,6 +68,7 @@ class TimeSeries(proto.Message): class Point(proto.Message): r"""A single data point in a time series. + Attributes: interval (google.cloud.bigquery_migration_v2alpha.types.TimeInterval): The time interval to which the data point applies. For @@ -109,6 +111,7 @@ class TimeInterval(proto.Message): class TypedValue(proto.Message): r"""A single strongly-typed value. + Attributes: bool_value (bool): A Boolean value: ``true`` or ``false``. diff --git a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/types/migration_service.py b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/types/migration_service.py index 279bc193f083..51cfa8bbaca7 100644 --- a/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/types/migration_service.py +++ b/packages/google-cloud-bigquery-migration/google/cloud/bigquery_migration_v2alpha/types/migration_service.py @@ -37,6 +37,7 @@ class CreateMigrationWorkflowRequest(proto.Message): r"""Request to create a migration workflow resource. + Attributes: parent (str): Required. The name of the project to which this migration @@ -53,6 +54,7 @@ class CreateMigrationWorkflowRequest(proto.Message): class GetMigrationWorkflowRequest(proto.Message): r"""A request to get a previously created migration workflow. + Attributes: name (str): Required. The unique identifier for the migration workflow. @@ -67,6 +69,7 @@ class GetMigrationWorkflowRequest(proto.Message): class ListMigrationWorkflowsRequest(proto.Message): r"""A request to list previously created migration workflows. + Attributes: parent (str): Required. The project and location of the migration @@ -95,6 +98,7 @@ class ListMigrationWorkflowsRequest(proto.Message): class ListMigrationWorkflowsResponse(proto.Message): r"""Response object for a ``ListMigrationWorkflows`` call. + Attributes: migration_workflows (Sequence[google.cloud.bigquery_migration_v2alpha.types.MigrationWorkflow]): The migration workflows for the specified @@ -117,6 +121,7 @@ def raw_page(self): class DeleteMigrationWorkflowRequest(proto.Message): r"""A request to delete a previously created migration workflow. + Attributes: name (str): Required. The unique identifier for the migration workflow. @@ -128,6 +133,7 @@ class DeleteMigrationWorkflowRequest(proto.Message): class StartMigrationWorkflowRequest(proto.Message): r"""A request to start a previously created migration workflow. + Attributes: name (str): Required. The unique identifier for the migration workflow. @@ -139,6 +145,7 @@ class StartMigrationWorkflowRequest(proto.Message): class GetMigrationSubtaskRequest(proto.Message): r"""A request to get a previously created migration subtasks. + Attributes: name (str): Required. The unique identifier for the migration subtask. @@ -154,6 +161,7 @@ class GetMigrationSubtaskRequest(proto.Message): class ListMigrationSubtasksRequest(proto.Message): r"""A request to list previously created migration subtasks. + Attributes: parent (str): Required. The migration task of the subtasks to list. @@ -188,6 +196,7 @@ class ListMigrationSubtasksRequest(proto.Message): class ListMigrationSubtasksResponse(proto.Message): r"""Response object for a ``ListMigrationSubtasks`` call. + Attributes: migration_subtasks (Sequence[google.cloud.bigquery_migration_v2alpha.types.MigrationSubtask]): The migration subtasks for the specified diff --git a/packages/google-cloud-bigquery-migration/tests/unit/gapic/bigquery_migration_v2alpha/test_migration_service.py b/packages/google-cloud-bigquery-migration/tests/unit/gapic/bigquery_migration_v2alpha/test_migration_service.py index 75203c8be9b9..23b0239f716c 100644 --- a/packages/google-cloud-bigquery-migration/tests/unit/gapic/bigquery_migration_v2alpha/test_migration_service.py +++ b/packages/google-cloud-bigquery-migration/tests/unit/gapic/bigquery_migration_v2alpha/test_migration_service.py @@ -29,6 +29,7 @@ from google.api_core import gapic_v1 from google.api_core import grpc_helpers from google.api_core import grpc_helpers_async +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.bigquery_migration_v2alpha.services.migration_service import ( @@ -2554,6 +2555,9 @@ def test_migration_service_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + @requires_google_auth_gte_1_25_0 def test_migration_service_base_transport_with_credentials_file(): @@ -3064,3 +3068,49 @@ def test_client_withDEFAULT_CLIENT_INFO(): credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) + + +@pytest.mark.asyncio +async def test_transport_close_async(): + client = MigrationServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio", + ) + with mock.patch.object( + type(getattr(client.transport, "grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() + + +def test_transport_close(): + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): + client = MigrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + with mock.patch.object( + type(getattr(client.transport, close_name)), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +def test_client_ctx(): + transports = [ + "grpc", + ] + for transport in transports: + client = MigrationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called()