From b439623760bc9f84ce8472f38c3db32439f01bf9 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:42:27 +0000 Subject: [PATCH] feat: add context manager support in client (#67) - [ ] 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/iam_checker/async_client.py | 6 +++ .../services/iam_checker/client.py | 18 +++++-- .../services/iam_checker/transports/base.py | 9 ++++ .../services/iam_checker/transports/grpc.py | 3 ++ .../iam_checker/transports/grpc_asyncio.py | 3 ++ .../types/explanations.py | 1 + .../test_iam_checker.py | 50 +++++++++++++++++++ 7 files changed, 86 insertions(+), 4 deletions(-) diff --git a/google/cloud/policytroubleshooter_v1/services/iam_checker/async_client.py b/google/cloud/policytroubleshooter_v1/services/iam_checker/async_client.py index b577b0e..6e20fb8 100644 --- a/google/cloud/policytroubleshooter_v1/services/iam_checker/async_client.py +++ b/google/cloud/policytroubleshooter_v1/services/iam_checker/async_client.py @@ -201,6 +201,12 @@ async def troubleshoot_iam_policy( # 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/policytroubleshooter_v1/services/iam_checker/client.py b/google/cloud/policytroubleshooter_v1/services/iam_checker/client.py index 41a79b3..621779d 100644 --- a/google/cloud/policytroubleshooter_v1/services/iam_checker/client.py +++ b/google/cloud/policytroubleshooter_v1/services/iam_checker/client.py @@ -329,10 +329,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 troubleshoot_iam_policy( @@ -381,6 +378,19 @@ def troubleshoot_iam_policy( # 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/policytroubleshooter_v1/services/iam_checker/transports/base.py b/google/cloud/policytroubleshooter_v1/services/iam_checker/transports/base.py index 75ec22e..cde8fc7 100644 --- a/google/cloud/policytroubleshooter_v1/services/iam_checker/transports/base.py +++ b/google/cloud/policytroubleshooter_v1/services/iam_checker/transports/base.py @@ -161,6 +161,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 troubleshoot_iam_policy( self, diff --git a/google/cloud/policytroubleshooter_v1/services/iam_checker/transports/grpc.py b/google/cloud/policytroubleshooter_v1/services/iam_checker/transports/grpc.py index 6acb76f..8273827 100644 --- a/google/cloud/policytroubleshooter_v1/services/iam_checker/transports/grpc.py +++ b/google/cloud/policytroubleshooter_v1/services/iam_checker/transports/grpc.py @@ -257,5 +257,8 @@ def troubleshoot_iam_policy( ) return self._stubs["troubleshoot_iam_policy"] + def close(self): + self.grpc_channel.close() + __all__ = ("IamCheckerGrpcTransport",) diff --git a/google/cloud/policytroubleshooter_v1/services/iam_checker/transports/grpc_asyncio.py b/google/cloud/policytroubleshooter_v1/services/iam_checker/transports/grpc_asyncio.py index fe58cfc..ff8edf8 100644 --- a/google/cloud/policytroubleshooter_v1/services/iam_checker/transports/grpc_asyncio.py +++ b/google/cloud/policytroubleshooter_v1/services/iam_checker/transports/grpc_asyncio.py @@ -261,5 +261,8 @@ def troubleshoot_iam_policy( ) return self._stubs["troubleshoot_iam_policy"] + def close(self): + return self.grpc_channel.close() + __all__ = ("IamCheckerGrpcAsyncIOTransport",) diff --git a/google/cloud/policytroubleshooter_v1/types/explanations.py b/google/cloud/policytroubleshooter_v1/types/explanations.py index b06fbf8..672aa59 100644 --- a/google/cloud/policytroubleshooter_v1/types/explanations.py +++ b/google/cloud/policytroubleshooter_v1/types/explanations.py @@ -222,6 +222,7 @@ class Membership(proto.Enum): class AnnotatedMembership(proto.Message): r"""Details about whether the binding includes the member. + Attributes: membership (google.cloud.policytroubleshooter_v1.types.BindingExplanation.Membership): Indicates whether the binding includes the diff --git a/tests/unit/gapic/policytroubleshooter_v1/test_iam_checker.py b/tests/unit/gapic/policytroubleshooter_v1/test_iam_checker.py index e824a5b..181ebc3 100644 --- a/tests/unit/gapic/policytroubleshooter_v1/test_iam_checker.py +++ b/tests/unit/gapic/policytroubleshooter_v1/test_iam_checker.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.policytroubleshooter_v1.services.iam_checker import ( @@ -672,6 +673,9 @@ def test_iam_checker_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_iam_checker_base_transport_with_credentials_file(): @@ -1113,3 +1117,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 = IamCheckerAsyncClient( + 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 = IamCheckerClient( + 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 = IamCheckerClient( + 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()