diff --git a/google/cloud/iam_credentials_v1/services/iam_credentials/async_client.py b/google/cloud/iam_credentials_v1/services/iam_credentials/async_client.py index 14d5cbd..299e4be 100644 --- a/google/cloud/iam_credentials_v1/services/iam_credentials/async_client.py +++ b/google/cloud/iam_credentials_v1/services/iam_credentials/async_client.py @@ -659,6 +659,12 @@ async def sign_jwt( # 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/iam_credentials_v1/services/iam_credentials/client.py b/google/cloud/iam_credentials_v1/services/iam_credentials/client.py index 8890a0a..1d207fb 100644 --- a/google/cloud/iam_credentials_v1/services/iam_credentials/client.py +++ b/google/cloud/iam_credentials_v1/services/iam_credentials/client.py @@ -355,10 +355,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 generate_access_token( @@ -806,6 +803,19 @@ def sign_jwt( # 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/iam_credentials_v1/services/iam_credentials/transports/base.py b/google/cloud/iam_credentials_v1/services/iam_credentials/transports/base.py index 174e6af..1627b1b 100644 --- a/google/cloud/iam_credentials_v1/services/iam_credentials/transports/base.py +++ b/google/cloud/iam_credentials_v1/services/iam_credentials/transports/base.py @@ -214,6 +214,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 generate_access_token( self, diff --git a/google/cloud/iam_credentials_v1/services/iam_credentials/transports/grpc.py b/google/cloud/iam_credentials_v1/services/iam_credentials/transports/grpc.py index 047f3ff..d60689c 100644 --- a/google/cloud/iam_credentials_v1/services/iam_credentials/transports/grpc.py +++ b/google/cloud/iam_credentials_v1/services/iam_credentials/transports/grpc.py @@ -340,5 +340,8 @@ def sign_jwt(self) -> Callable[[common.SignJwtRequest], common.SignJwtResponse]: ) return self._stubs["sign_jwt"] + def close(self): + self.grpc_channel.close() + __all__ = ("IAMCredentialsGrpcTransport",) diff --git a/google/cloud/iam_credentials_v1/services/iam_credentials/transports/grpc_asyncio.py b/google/cloud/iam_credentials_v1/services/iam_credentials/transports/grpc_asyncio.py index 2ab7bc3..6f7ddb8 100644 --- a/google/cloud/iam_credentials_v1/services/iam_credentials/transports/grpc_asyncio.py +++ b/google/cloud/iam_credentials_v1/services/iam_credentials/transports/grpc_asyncio.py @@ -350,5 +350,8 @@ def sign_jwt( ) return self._stubs["sign_jwt"] + def close(self): + return self.grpc_channel.close() + __all__ = ("IAMCredentialsGrpcAsyncIOTransport",) diff --git a/google/cloud/iam_credentials_v1/types/common.py b/google/cloud/iam_credentials_v1/types/common.py index 5124bbc..0f0e996 100644 --- a/google/cloud/iam_credentials_v1/types/common.py +++ b/google/cloud/iam_credentials_v1/types/common.py @@ -36,6 +36,7 @@ class GenerateAccessTokenRequest(proto.Message): r""" + Attributes: name (str): Required. The resource name of the service account for which @@ -79,6 +80,7 @@ class GenerateAccessTokenRequest(proto.Message): class GenerateAccessTokenResponse(proto.Message): r""" + Attributes: access_token (str): The OAuth 2.0 access token. @@ -93,6 +95,7 @@ class GenerateAccessTokenResponse(proto.Message): class SignBlobRequest(proto.Message): r""" + Attributes: name (str): Required. The resource name of the service account for which @@ -125,6 +128,7 @@ class SignBlobRequest(proto.Message): class SignBlobResponse(proto.Message): r""" + Attributes: key_id (str): The ID of the key used to sign the blob. @@ -138,6 +142,7 @@ class SignBlobResponse(proto.Message): class SignJwtRequest(proto.Message): r""" + Attributes: name (str): Required. The resource name of the service account for which @@ -171,6 +176,7 @@ class SignJwtRequest(proto.Message): class SignJwtResponse(proto.Message): r""" + Attributes: key_id (str): The ID of the key used to sign the JWT. @@ -184,6 +190,7 @@ class SignJwtResponse(proto.Message): class GenerateIdTokenRequest(proto.Message): r""" + Attributes: name (str): Required. The resource name of the service account for which @@ -223,6 +230,7 @@ class GenerateIdTokenRequest(proto.Message): class GenerateIdTokenResponse(proto.Message): r""" + Attributes: token (str): The OpenId Connect ID token. diff --git a/tests/unit/gapic/credentials_v1/test_iam_credentials.py b/tests/unit/gapic/credentials_v1/test_iam_credentials.py index 009d48e..831b819 100644 --- a/tests/unit/gapic/credentials_v1/test_iam_credentials.py +++ b/tests/unit/gapic/credentials_v1/test_iam_credentials.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.iam_credentials_v1.services.iam_credentials import ( @@ -1531,6 +1532,9 @@ def test_iam_credentials_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_credentials_base_transport_with_credentials_file(): @@ -2011,3 +2015,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 = IAMCredentialsAsyncClient( + 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 = IAMCredentialsClient( + 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 = IAMCredentialsClient( + 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()