From c12c571606cb7f6467479d7f3ddf7fd4f44dbbee Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Thu, 7 Oct 2021 00:40:45 +0000 Subject: [PATCH] feat: add context manager support in client (#92) - [ ] 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/org_policy/async_client.py | 6 +++ .../services/org_policy/client.py | 18 +++++-- .../services/org_policy/transports/base.py | 9 ++++ .../services/org_policy/transports/grpc.py | 3 ++ .../org_policy/transports/grpc_asyncio.py | 3 ++ google/cloud/orgpolicy_v2/types/constraint.py | 3 +- google/cloud/orgpolicy_v2/types/orgpolicy.py | 1 + .../gapic/orgpolicy_v2/test_org_policy.py | 50 +++++++++++++++++++ 8 files changed, 88 insertions(+), 5 deletions(-) diff --git a/google/cloud/orgpolicy_v2/services/org_policy/async_client.py b/google/cloud/orgpolicy_v2/services/org_policy/async_client.py index e7ed930..68902b1 100644 --- a/google/cloud/orgpolicy_v2/services/org_policy/async_client.py +++ b/google/cloud/orgpolicy_v2/services/org_policy/async_client.py @@ -827,6 +827,12 @@ async def delete_policy( request, retry=retry, timeout=timeout, metadata=metadata, ) + 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/orgpolicy_v2/services/org_policy/client.py b/google/cloud/orgpolicy_v2/services/org_policy/client.py index da80f60..2858c57 100644 --- a/google/cloud/orgpolicy_v2/services/org_policy/client.py +++ b/google/cloud/orgpolicy_v2/services/org_policy/client.py @@ -377,10 +377,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_constraints( @@ -960,6 +957,19 @@ def delete_policy( request, retry=retry, timeout=timeout, metadata=metadata, ) + 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/orgpolicy_v2/services/org_policy/transports/base.py b/google/cloud/orgpolicy_v2/services/org_policy/transports/base.py index 9b77084..5958f12 100644 --- a/google/cloud/orgpolicy_v2/services/org_policy/transports/base.py +++ b/google/cloud/orgpolicy_v2/services/org_policy/transports/base.py @@ -262,6 +262,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 list_constraints( self, diff --git a/google/cloud/orgpolicy_v2/services/org_policy/transports/grpc.py b/google/cloud/orgpolicy_v2/services/org_policy/transports/grpc.py index 297d8fe..9422b23 100644 --- a/google/cloud/orgpolicy_v2/services/org_policy/transports/grpc.py +++ b/google/cloud/orgpolicy_v2/services/org_policy/transports/grpc.py @@ -459,5 +459,8 @@ def delete_policy( ) return self._stubs["delete_policy"] + def close(self): + self.grpc_channel.close() + __all__ = ("OrgPolicyGrpcTransport",) diff --git a/google/cloud/orgpolicy_v2/services/org_policy/transports/grpc_asyncio.py b/google/cloud/orgpolicy_v2/services/org_policy/transports/grpc_asyncio.py index 8c1dbdb..7de6118 100644 --- a/google/cloud/orgpolicy_v2/services/org_policy/transports/grpc_asyncio.py +++ b/google/cloud/orgpolicy_v2/services/org_policy/transports/grpc_asyncio.py @@ -466,5 +466,8 @@ def delete_policy( ) return self._stubs["delete_policy"] + def close(self): + return self.grpc_channel.close() + __all__ = ("OrgPolicyGrpcAsyncIOTransport",) diff --git a/google/cloud/orgpolicy_v2/types/constraint.py b/google/cloud/orgpolicy_v2/types/constraint.py index d09029c..e37e605 100644 --- a/google/cloud/orgpolicy_v2/types/constraint.py +++ b/google/cloud/orgpolicy_v2/types/constraint.py @@ -110,7 +110,8 @@ class BooleanConstraint(proto.Message): ``constraints/compute.disableSerialPortAccess``. If it is enforced on a VM instance, serial port connections will not be opened to that instance. - """ + + """ name = proto.Field(proto.STRING, number=1,) display_name = proto.Field(proto.STRING, number=2,) diff --git a/google/cloud/orgpolicy_v2/types/orgpolicy.py b/google/cloud/orgpolicy_v2/types/orgpolicy.py index cd83288..14b2327 100644 --- a/google/cloud/orgpolicy_v2/types/orgpolicy.py +++ b/google/cloud/orgpolicy_v2/types/orgpolicy.py @@ -149,6 +149,7 @@ class PolicySpec(proto.Message): class PolicyRule(proto.Message): r"""A rule used to express this policy. + Attributes: values (google.cloud.orgpolicy_v2.types.PolicySpec.PolicyRule.StringValues): List of values to be used for this diff --git a/tests/unit/gapic/orgpolicy_v2/test_org_policy.py b/tests/unit/gapic/orgpolicy_v2/test_org_policy.py index c73482d..bf25f14 100644 --- a/tests/unit/gapic/orgpolicy_v2/test_org_policy.py +++ b/tests/unit/gapic/orgpolicy_v2/test_org_policy.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.orgpolicy_v2.services.org_policy import OrgPolicyAsyncClient @@ -2224,6 +2225,9 @@ def test_org_policy_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_org_policy_base_transport_with_credentials_file(): @@ -2709,3 +2713,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 = OrgPolicyAsyncClient( + 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 = OrgPolicyClient( + 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 = OrgPolicyClient( + 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()