diff --git a/google/cloud/shell_v1/services/cloud_shell_service/async_client.py b/google/cloud/shell_v1/services/cloud_shell_service/async_client.py index e7048c1..4270260 100644 --- a/google/cloud/shell_v1/services/cloud_shell_service/async_client.py +++ b/google/cloud/shell_v1/services/cloud_shell_service/async_client.py @@ -518,6 +518,12 @@ async def remove_public_key( # 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/shell_v1/services/cloud_shell_service/client.py b/google/cloud/shell_v1/services/cloud_shell_service/client.py index 747084d..7636ef9 100644 --- a/google/cloud/shell_v1/services/cloud_shell_service/client.py +++ b/google/cloud/shell_v1/services/cloud_shell_service/client.py @@ -352,10 +352,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 get_environment( @@ -699,6 +696,19 @@ def remove_public_key( # 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/shell_v1/services/cloud_shell_service/transports/base.py b/google/cloud/shell_v1/services/cloud_shell_service/transports/base.py index 1576c90..6d17783 100644 --- a/google/cloud/shell_v1/services/cloud_shell_service/transports/base.py +++ b/google/cloud/shell_v1/services/cloud_shell_service/transports/base.py @@ -184,6 +184,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/shell_v1/services/cloud_shell_service/transports/grpc.py b/google/cloud/shell_v1/services/cloud_shell_service/transports/grpc.py index 22f9e83..b9acc49 100644 --- a/google/cloud/shell_v1/services/cloud_shell_service/transports/grpc.py +++ b/google/cloud/shell_v1/services/cloud_shell_service/transports/grpc.py @@ -397,5 +397,8 @@ def remove_public_key( ) return self._stubs["remove_public_key"] + def close(self): + self.grpc_channel.close() + __all__ = ("CloudShellServiceGrpcTransport",) diff --git a/google/cloud/shell_v1/services/cloud_shell_service/transports/grpc_asyncio.py b/google/cloud/shell_v1/services/cloud_shell_service/transports/grpc_asyncio.py index 8878f21..a89f322 100644 --- a/google/cloud/shell_v1/services/cloud_shell_service/transports/grpc_asyncio.py +++ b/google/cloud/shell_v1/services/cloud_shell_service/transports/grpc_asyncio.py @@ -412,5 +412,8 @@ def remove_public_key( ) return self._stubs["remove_public_key"] + def close(self): + return self.grpc_channel.close() + __all__ = ("CloudShellServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/shell_v1/types/cloudshell.py b/google/cloud/shell_v1/types/cloudshell.py index 8d4ab7b..1149a24 100644 --- a/google/cloud/shell_v1/types/cloudshell.py +++ b/google/cloud/shell_v1/types/cloudshell.py @@ -129,13 +129,15 @@ class GetEnvironmentRequest(proto.Message): class CreateEnvironmentMetadata(proto.Message): r"""Message included in the metadata field of operations returned from [CreateEnvironment][google.cloud.shell.v1.CloudShellService.CreateEnvironment]. - """ + + """ class DeleteEnvironmentMetadata(proto.Message): r"""Message included in the metadata field of operations returned from [DeleteEnvironment][google.cloud.shell.v1.CloudShellService.DeleteEnvironment]. - """ + + """ class StartEnvironmentRequest(proto.Message): @@ -195,13 +197,15 @@ class AuthorizeEnvironmentRequest(proto.Message): class AuthorizeEnvironmentResponse(proto.Message): r"""Response message for [AuthorizeEnvironment][google.cloud.shell.v1.CloudShellService.AuthorizeEnvironment]. - """ + + """ class AuthorizeEnvironmentMetadata(proto.Message): r"""Message included in the metadata field of operations returned from [AuthorizeEnvironment][google.cloud.shell.v1.CloudShellService.AuthorizeEnvironment]. - """ + + """ class StartEnvironmentMetadata(proto.Message): @@ -280,7 +284,8 @@ class AddPublicKeyResponse(proto.Message): class AddPublicKeyMetadata(proto.Message): r"""Message included in the metadata field of operations returned from [AddPublicKey][google.cloud.shell.v1.CloudShellService.AddPublicKey]. - """ + + """ class RemovePublicKeyRequest(proto.Message): @@ -303,13 +308,15 @@ class RemovePublicKeyRequest(proto.Message): class RemovePublicKeyResponse(proto.Message): r"""Response message for [RemovePublicKey][google.cloud.shell.v1.CloudShellService.RemovePublicKey]. - """ + + """ class RemovePublicKeyMetadata(proto.Message): r"""Message included in the metadata field of operations returned from [RemovePublicKey][google.cloud.shell.v1.CloudShellService.RemovePublicKey]. - """ + + """ class CloudShellErrorDetails(proto.Message): diff --git a/tests/unit/gapic/shell_v1/test_cloud_shell_service.py b/tests/unit/gapic/shell_v1/test_cloud_shell_service.py index 5e005b0..c3df581 100644 --- a/tests/unit/gapic/shell_v1/test_cloud_shell_service.py +++ b/tests/unit/gapic/shell_v1/test_cloud_shell_service.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.shell_v1.services.cloud_shell_service import ( @@ -1414,6 +1415,9 @@ def test_cloud_shell_service_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): @@ -1927,3 +1931,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 = CloudShellServiceAsyncClient( + 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 = CloudShellServiceClient( + 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 = CloudShellServiceClient( + 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()