From fa369789687993fff0359f22110951393c849e70 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:18 +0000 Subject: [PATCH] feat: add context manager support in client (#35) - [ ] 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 --- .../services/datastream/async_client.py | 6 +++ .../services/datastream/client.py | 18 +++++-- .../services/datastream/transports/base.py | 9 ++++ .../services/datastream/transports/grpc.py | 3 ++ .../datastream/transports/grpc_asyncio.py | 3 ++ .../datastream_v1alpha1/types/datastream.py | 29 +++++++++++ .../types/datastream_resources.py | 38 ++++++++++++-- .../datastream_v1alpha1/test_datastream.py | 50 +++++++++++++++++++ 8 files changed, 148 insertions(+), 8 deletions(-) diff --git a/google/cloud/datastream_v1alpha1/services/datastream/async_client.py b/google/cloud/datastream_v1alpha1/services/datastream/async_client.py index 8096f6c..5c8f568 100644 --- a/google/cloud/datastream_v1alpha1/services/datastream/async_client.py +++ b/google/cloud/datastream_v1alpha1/services/datastream/async_client.py @@ -1922,6 +1922,12 @@ async def delete_route( # 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/google/cloud/datastream_v1alpha1/services/datastream/client.py b/google/cloud/datastream_v1alpha1/services/datastream/client.py index 442c8a1..07af2d3 100644 --- a/google/cloud/datastream_v1alpha1/services/datastream/client.py +++ b/google/cloud/datastream_v1alpha1/services/datastream/client.py @@ -405,10 +405,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 list_connection_profiles( @@ -2176,6 +2173,19 @@ def delete_route( # 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/google/cloud/datastream_v1alpha1/services/datastream/transports/base.py b/google/cloud/datastream_v1alpha1/services/datastream/transports/base.py index 76b62a4..e034088 100644 --- a/google/cloud/datastream_v1alpha1/services/datastream/transports/base.py +++ b/google/cloud/datastream_v1alpha1/services/datastream/transports/base.py @@ -242,6 +242,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 operations_client(self) -> operations_v1.OperationsClient: """Return the client designed to process long-running operations.""" diff --git a/google/cloud/datastream_v1alpha1/services/datastream/transports/grpc.py b/google/cloud/datastream_v1alpha1/services/datastream/transports/grpc.py index 6f5e27d..dd7c92c 100644 --- a/google/cloud/datastream_v1alpha1/services/datastream/transports/grpc.py +++ b/google/cloud/datastream_v1alpha1/services/datastream/transports/grpc.py @@ -834,5 +834,8 @@ def delete_route( ) return self._stubs["delete_route"] + def close(self): + self.grpc_channel.close() + __all__ = ("DatastreamGrpcTransport",) diff --git a/google/cloud/datastream_v1alpha1/services/datastream/transports/grpc_asyncio.py b/google/cloud/datastream_v1alpha1/services/datastream/transports/grpc_asyncio.py index 54004eb..b2ee457 100644 --- a/google/cloud/datastream_v1alpha1/services/datastream/transports/grpc_asyncio.py +++ b/google/cloud/datastream_v1alpha1/services/datastream/transports/grpc_asyncio.py @@ -853,5 +853,8 @@ def delete_route( ) return self._stubs["delete_route"] + def close(self): + return self.grpc_channel.close() + __all__ = ("DatastreamGrpcAsyncIOTransport",) diff --git a/google/cloud/datastream_v1alpha1/types/datastream.py b/google/cloud/datastream_v1alpha1/types/datastream.py index 46db339..9992189 100644 --- a/google/cloud/datastream_v1alpha1/types/datastream.py +++ b/google/cloud/datastream_v1alpha1/types/datastream.py @@ -58,6 +58,7 @@ class DiscoverConnectionProfileRequest(proto.Message): r"""Request message for 'discover' ConnectionProfile request. + Attributes: parent (str): Required. The parent resource of the ConnectionProfile type. @@ -107,6 +108,7 @@ class DiscoverConnectionProfileRequest(proto.Message): class DiscoverConnectionProfileResponse(proto.Message): r""" + Attributes: oracle_rdbms (google.cloud.datastream_v1alpha1.types.OracleRdbms): Enriched Oracle RDBMS object. @@ -130,6 +132,7 @@ class DiscoverConnectionProfileResponse(proto.Message): class FetchStaticIpsRequest(proto.Message): r"""Request message for 'FetchStaticIps' request. + Attributes: name (str): Required. The name resource of the Response type. Must be in @@ -149,6 +152,7 @@ class FetchStaticIpsRequest(proto.Message): class FetchStaticIpsResponse(proto.Message): r"""Response message for a 'FetchStaticIps' response. + Attributes: static_ips (Sequence[str]): list of static ips by account @@ -168,6 +172,7 @@ def raw_page(self): class FetchErrorsRequest(proto.Message): r"""Request message for 'FetchErrors' request. + Attributes: stream (str): Name of the Stream resource for which to @@ -179,6 +184,7 @@ class FetchErrorsRequest(proto.Message): class FetchErrorsResponse(proto.Message): r"""Response message for a 'FetchErrors' response. + Attributes: errors (Sequence[google.cloud.datastream_v1alpha1.types.Error]): The list of errors on the Stream. @@ -191,6 +197,7 @@ class FetchErrorsResponse(proto.Message): class ListConnectionProfilesRequest(proto.Message): r""" + Attributes: parent (str): Required. The parent that owns the collection @@ -223,6 +230,7 @@ class ListConnectionProfilesRequest(proto.Message): class ListConnectionProfilesResponse(proto.Message): r""" + Attributes: connection_profiles (Sequence[google.cloud.datastream_v1alpha1.types.ConnectionProfile]): List of connection profiles. @@ -247,6 +255,7 @@ def raw_page(self): class GetConnectionProfileRequest(proto.Message): r""" + Attributes: name (str): Required. The name of the connection profile @@ -258,6 +267,7 @@ class GetConnectionProfileRequest(proto.Message): class CreateConnectionProfileRequest(proto.Message): r""" + Attributes: parent (str): Required. The parent that owns the collection @@ -297,6 +307,7 @@ class CreateConnectionProfileRequest(proto.Message): class UpdateConnectionProfileRequest(proto.Message): r""" + Attributes: update_mask (google.protobuf.field_mask_pb2.FieldMask): Optional. Field mask is used to specify the fields to be @@ -338,6 +349,7 @@ class UpdateConnectionProfileRequest(proto.Message): class DeleteConnectionProfileRequest(proto.Message): r""" + Attributes: name (str): Required. The name of the connection profile @@ -368,6 +380,7 @@ class DeleteConnectionProfileRequest(proto.Message): class ListStreamsRequest(proto.Message): r""" + Attributes: parent (str): Required. The parent that owns the collection @@ -399,6 +412,7 @@ class ListStreamsRequest(proto.Message): class ListStreamsResponse(proto.Message): r""" + Attributes: streams (Sequence[google.cloud.datastream_v1alpha1.types.Stream]): List of streams @@ -423,6 +437,7 @@ def raw_page(self): class GetStreamRequest(proto.Message): r""" + Attributes: name (str): Required. The name of the stream resource to @@ -434,6 +449,7 @@ class GetStreamRequest(proto.Message): class CreateStreamRequest(proto.Message): r""" + Attributes: parent (str): Required. The parent that owns the collection @@ -478,6 +494,7 @@ class CreateStreamRequest(proto.Message): class UpdateStreamRequest(proto.Message): r""" + Attributes: update_mask (google.protobuf.field_mask_pb2.FieldMask): Optional. Field mask is used to specify the fields to be @@ -526,6 +543,7 @@ class UpdateStreamRequest(proto.Message): class DeleteStreamRequest(proto.Message): r""" + Attributes: name (str): Required. The name of the stream resource to @@ -556,6 +574,7 @@ class DeleteStreamRequest(proto.Message): class OperationMetadata(proto.Message): r"""Represents the metadata of the long-running operation. + Attributes: create_time (google.protobuf.timestamp_pb2.Timestamp): Output only. The time the operation was @@ -600,6 +619,7 @@ class OperationMetadata(proto.Message): class CreatePrivateConnectionRequest(proto.Message): r""" + Attributes: parent (str): Required. The parent that owns the collection @@ -640,6 +660,7 @@ class CreatePrivateConnectionRequest(proto.Message): class ListPrivateConnectionsRequest(proto.Message): r""" + Attributes: parent (str): Required. The parent that owns the collection @@ -673,6 +694,7 @@ class ListPrivateConnectionsRequest(proto.Message): class ListPrivateConnectionsResponse(proto.Message): r""" + Attributes: private_connections (Sequence[google.cloud.datastream_v1alpha1.types.PrivateConnection]): List of private connectivity configurations. @@ -697,6 +719,7 @@ def raw_page(self): class DeletePrivateConnectionRequest(proto.Message): r""" + Attributes: name (str): Required. The name of the private @@ -732,6 +755,7 @@ class DeletePrivateConnectionRequest(proto.Message): class GetPrivateConnectionRequest(proto.Message): r""" + Attributes: name (str): Required. The name of the private @@ -743,6 +767,7 @@ class GetPrivateConnectionRequest(proto.Message): class CreateRouteRequest(proto.Message): r"""route creation request + Attributes: parent (str): Required. The parent that owns the collection @@ -779,6 +804,7 @@ class CreateRouteRequest(proto.Message): class ListRoutesRequest(proto.Message): r"""route list request + Attributes: parent (str): Required. The parent that owns the collection @@ -811,6 +837,7 @@ class ListRoutesRequest(proto.Message): class ListRoutesResponse(proto.Message): r"""route list response + Attributes: routes (Sequence[google.cloud.datastream_v1alpha1.types.Route]): List of Routes. @@ -835,6 +862,7 @@ def raw_page(self): class DeleteRouteRequest(proto.Message): r"""route deletion request + Attributes: name (str): Required. The name of the Route resource to @@ -865,6 +893,7 @@ class DeleteRouteRequest(proto.Message): class GetRouteRequest(proto.Message): r"""route get request + Attributes: name (str): Required. The name of the Route resource to diff --git a/google/cloud/datastream_v1alpha1/types/datastream_resources.py b/google/cloud/datastream_v1alpha1/types/datastream_resources.py index 14d9bc8..670d704 100644 --- a/google/cloud/datastream_v1alpha1/types/datastream_resources.py +++ b/google/cloud/datastream_v1alpha1/types/datastream_resources.py @@ -76,6 +76,7 @@ class SchemaFileFormat(proto.Enum): class OracleProfile(proto.Message): r"""Oracle database profile. + Attributes: hostname (str): Required. Hostname for the Oracle connection. @@ -102,6 +103,7 @@ class OracleProfile(proto.Message): class MysqlProfile(proto.Message): r"""MySQL database profile. + Attributes: hostname (str): Required. Hostname for the MySQL connection. @@ -126,6 +128,7 @@ class MysqlProfile(proto.Message): class GcsProfile(proto.Message): r"""Cloud Storage bucket profile. + Attributes: bucket_name (str): Required. The full project and resource path @@ -140,15 +143,18 @@ class GcsProfile(proto.Message): class NoConnectivitySettings(proto.Message): - r"""No connectivity settings. """ + r"""No connectivity settings. + """ class StaticServiceIpConnectivity(proto.Message): - r"""Static IP address connectivity. """ + r"""Static IP address connectivity. + """ class ForwardSshTunnelConnectivity(proto.Message): r"""Forward SSH Tunnel connectivity. + Attributes: hostname (str): Required. Hostname for the SSH tunnel. @@ -232,6 +238,7 @@ class State(proto.Enum): class PrivateConnectivity(proto.Message): r"""Private Connectivity + Attributes: private_connection_name (str): @@ -273,6 +280,7 @@ class Route(proto.Message): class MysqlSslConfig(proto.Message): r"""MySQL SSL configuration information. + Attributes: client_key (str): Input only. PEM-encoded private key associated with the @@ -308,6 +316,7 @@ class MysqlSslConfig(proto.Message): class ConnectionProfile(proto.Message): r""" + Attributes: name (str): Output only. The resource's name. @@ -375,6 +384,7 @@ class ConnectionProfile(proto.Message): class OracleColumn(proto.Message): r"""Oracle Column. + Attributes: column_name (str): Column name. @@ -412,6 +422,7 @@ class OracleColumn(proto.Message): class OracleTable(proto.Message): r"""Oracle table. + Attributes: table_name (str): Table name. @@ -429,6 +440,7 @@ class OracleTable(proto.Message): class OracleSchema(proto.Message): r"""Oracle schema. + Attributes: schema_name (str): Schema name. @@ -442,6 +454,7 @@ class OracleSchema(proto.Message): class OracleRdbms(proto.Message): r"""Oracle database structure. + Attributes: oracle_schemas (Sequence[google.cloud.datastream_v1alpha1.types.OracleSchema]): Oracle schemas/databases in the database @@ -455,6 +468,7 @@ class OracleRdbms(proto.Message): class OracleSourceConfig(proto.Message): r"""Oracle data source configuration + Attributes: allowlist (google.cloud.datastream_v1alpha1.types.OracleRdbms): Oracle objects to include in the stream. @@ -468,6 +482,7 @@ class OracleSourceConfig(proto.Message): class MysqlColumn(proto.Message): r"""MySQL Column. + Attributes: column_name (str): Column name. @@ -502,6 +517,7 @@ class MysqlColumn(proto.Message): class MysqlTable(proto.Message): r"""MySQL table. + Attributes: table_name (str): Table name. @@ -517,6 +533,7 @@ class MysqlTable(proto.Message): class MysqlDatabase(proto.Message): r"""MySQL database. + Attributes: database_name (str): Database name. @@ -530,6 +547,7 @@ class MysqlDatabase(proto.Message): class MysqlRdbms(proto.Message): r"""MySQL database structure + Attributes: mysql_databases (Sequence[google.cloud.datastream_v1alpha1.types.MysqlDatabase]): Mysql databases on the server @@ -542,6 +560,7 @@ class MysqlRdbms(proto.Message): class MysqlSourceConfig(proto.Message): r"""MySQL source configuration + Attributes: allowlist (google.cloud.datastream_v1alpha1.types.MysqlRdbms): MySQL objects to retrieve from the source. @@ -555,6 +574,7 @@ class MysqlSourceConfig(proto.Message): class SourceConfig(proto.Message): r"""The configuration of the stream source. + Attributes: source_connection_profile_name (str): Required. Source connection profile @@ -581,11 +601,13 @@ class SourceConfig(proto.Message): class AvroFileFormat(proto.Message): - r"""AVRO file format configuration. """ + r"""AVRO file format configuration. + """ class JsonFileFormat(proto.Message): r"""JSON file format configuration. + Attributes: schema_file_format (google.cloud.datastream_v1alpha1.types.SchemaFileFormat): The schema file format along JSON data files. @@ -605,6 +627,7 @@ class JsonCompression(proto.Enum): class GcsDestinationConfig(proto.Message): r"""Google Cloud Storage destination configuration + Attributes: path (str): Path inside the Cloud Storage bucket to write @@ -641,6 +664,7 @@ class GcsDestinationConfig(proto.Message): class DestinationConfig(proto.Message): r"""The configuration of the stream destination. + Attributes: destination_connection_profile_name (str): Required. Destination connection profile @@ -660,6 +684,7 @@ class DestinationConfig(proto.Message): class Stream(proto.Message): r""" + Attributes: name (str): Output only. The stream's name. @@ -725,7 +750,8 @@ class BackfillAllStrategy(proto.Message): class BackfillNoneStrategy(proto.Message): r"""Backfill strategy to disable automatic backfill for the Stream's objects. - """ + + """ name = proto.Field(proto.STRING, number=1,) create_time = proto.Field(proto.MESSAGE, number=2, message=timestamp_pb2.Timestamp,) @@ -754,6 +780,7 @@ class BackfillNoneStrategy(proto.Message): class Error(proto.Message): r"""Represent a user-facing Error. + Attributes: reason (str): A title that explains the reason for the @@ -780,6 +807,7 @@ class Error(proto.Message): class ValidationResult(proto.Message): r"""Contains the current validation results. + Attributes: validations (Sequence[google.cloud.datastream_v1alpha1.types.Validation]): A list of validations (includes both executed @@ -791,6 +819,7 @@ class ValidationResult(proto.Message): class Validation(proto.Message): r""" + Attributes: description (str): A short description of the validation. @@ -817,6 +846,7 @@ class Status(proto.Enum): class ValidationMessage(proto.Message): r"""Represent user-facing validation result message. + Attributes: message (str): The result of the validation. diff --git a/tests/unit/gapic/datastream_v1alpha1/test_datastream.py b/tests/unit/gapic/datastream_v1alpha1/test_datastream.py index ccbd965..116b55e 100644 --- a/tests/unit/gapic/datastream_v1alpha1/test_datastream.py +++ b/tests/unit/gapic/datastream_v1alpha1/test_datastream.py @@ -32,6 +32,7 @@ from google.api_core import grpc_helpers_async from google.api_core import operation_async # type: ignore from google.api_core import operations_v1 +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.datastream_v1alpha1.services.datastream import DatastreamAsyncClient @@ -5686,6 +5687,9 @@ def test_datastream_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + # Additionally, the LRO client (a property) should # also raise NotImplementedError with pytest.raises(NotImplementedError): @@ -6263,3 +6267,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 = DatastreamAsyncClient( + 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 = DatastreamClient( + 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 = DatastreamClient( + 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()