diff --git a/google/cloud/assuredworkloads_v1/services/assured_workloads_service/async_client.py b/google/cloud/assuredworkloads_v1/services/assured_workloads_service/async_client.py index f1660f1..0c1c4f9 100644 --- a/google/cloud/assuredworkloads_v1/services/assured_workloads_service/async_client.py +++ b/google/cloud/assuredworkloads_v1/services/assured_workloads_service/async_client.py @@ -570,6 +570,12 @@ async def list_workloads( # 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/assuredworkloads_v1/services/assured_workloads_service/client.py b/google/cloud/assuredworkloads_v1/services/assured_workloads_service/client.py index 7d7540e..c1f3e0c 100644 --- a/google/cloud/assuredworkloads_v1/services/assured_workloads_service/client.py +++ b/google/cloud/assuredworkloads_v1/services/assured_workloads_service/client.py @@ -350,10 +350,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 create_workload( @@ -754,6 +751,19 @@ def list_workloads( # 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/assuredworkloads_v1/services/assured_workloads_service/transports/base.py b/google/cloud/assuredworkloads_v1/services/assured_workloads_service/transports/base.py index 3e190a5..7eb7985 100644 --- a/google/cloud/assuredworkloads_v1/services/assured_workloads_service/transports/base.py +++ b/google/cloud/assuredworkloads_v1/services/assured_workloads_service/transports/base.py @@ -174,6 +174,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/assuredworkloads_v1/services/assured_workloads_service/transports/grpc.py b/google/cloud/assuredworkloads_v1/services/assured_workloads_service/transports/grpc.py index 7a46461..8cdb040 100644 --- a/google/cloud/assuredworkloads_v1/services/assured_workloads_service/transports/grpc.py +++ b/google/cloud/assuredworkloads_v1/services/assured_workloads_service/transports/grpc.py @@ -380,5 +380,8 @@ def list_workloads( ) return self._stubs["list_workloads"] + def close(self): + self.grpc_channel.close() + __all__ = ("AssuredWorkloadsServiceGrpcTransport",) diff --git a/google/cloud/assuredworkloads_v1/services/assured_workloads_service/transports/grpc_asyncio.py b/google/cloud/assuredworkloads_v1/services/assured_workloads_service/transports/grpc_asyncio.py index d63efa5..09f3b36 100644 --- a/google/cloud/assuredworkloads_v1/services/assured_workloads_service/transports/grpc_asyncio.py +++ b/google/cloud/assuredworkloads_v1/services/assured_workloads_service/transports/grpc_asyncio.py @@ -392,5 +392,8 @@ def list_workloads( ) return self._stubs["list_workloads"] + def close(self): + return self.grpc_channel.close() + __all__ = ("AssuredWorkloadsServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/assuredworkloads_v1/types/assuredworkloads.py b/google/cloud/assuredworkloads_v1/types/assuredworkloads.py index daae8e9..ac153bc 100644 --- a/google/cloud/assuredworkloads_v1/types/assuredworkloads.py +++ b/google/cloud/assuredworkloads_v1/types/assuredworkloads.py @@ -37,6 +37,7 @@ class CreateWorkloadRequest(proto.Message): r"""Request for creating a workload. + Attributes: parent (str): Required. The resource name of the new Workload's parent. @@ -60,6 +61,7 @@ class CreateWorkloadRequest(proto.Message): class UpdateWorkloadRequest(proto.Message): r"""Request for Updating a workload. + Attributes: workload (google.cloud.assuredworkloads_v1.types.Workload): Required. The workload to update. The workload’s ``name`` @@ -78,6 +80,7 @@ class UpdateWorkloadRequest(proto.Message): class DeleteWorkloadRequest(proto.Message): r"""Request for deleting a Workload. + Attributes: name (str): Required. The ``name`` field is used to identify the @@ -95,6 +98,7 @@ class DeleteWorkloadRequest(proto.Message): class GetWorkloadRequest(proto.Message): r"""Request for fetching a workload. + Attributes: name (str): Required. The resource name of the Workload to fetch. This @@ -109,6 +113,7 @@ class GetWorkloadRequest(proto.Message): class ListWorkloadsRequest(proto.Message): r"""Request for fetching workloads in an organization. + Attributes: parent (str): Required. Parent Resource to list workloads from. Must be of @@ -134,6 +139,7 @@ class ListWorkloadsRequest(proto.Message): class ListWorkloadsResponse(proto.Message): r"""Response of ListWorkloads endpoint. + Attributes: workloads (Sequence[google.cloud.assuredworkloads_v1.types.Workload]): List of Workloads under a given parent. @@ -230,6 +236,7 @@ class ComplianceRegime(proto.Enum): class ResourceInfo(proto.Message): r"""Represent the resources that are children of this Workload. + Attributes: resource_id (int): Resource identifier. For a project this represents @@ -252,6 +259,7 @@ class ResourceType(proto.Enum): class KMSSettings(proto.Message): r"""Settings specific to the Key Management Service. + Attributes: next_rotation_time (google.protobuf.timestamp_pb2.Timestamp): Required. Input only. Immutable. The time at @@ -314,6 +322,7 @@ class ResourceSettings(proto.Message): class CreateWorkloadOperationMetadata(proto.Message): r"""Operation metadata to give request details of CreateWorkload. + Attributes: create_time (google.protobuf.timestamp_pb2.Timestamp): Optional. Time when the operation was diff --git a/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/async_client.py b/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/async_client.py index f91ce0d..d47e8f2 100644 --- a/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/async_client.py +++ b/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/async_client.py @@ -599,6 +599,12 @@ async def list_workloads( # 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/assuredworkloads_v1beta1/services/assured_workloads_service/client.py b/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/client.py index c7ce647..29ba693 100644 --- a/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/client.py +++ b/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_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 create_workload( @@ -756,6 +753,19 @@ def list_workloads( # 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/assuredworkloads_v1beta1/services/assured_workloads_service/transports/base.py b/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/transports/base.py index 335f4e2..f8d6133 100644 --- a/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/transports/base.py +++ b/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/transports/base.py @@ -207,6 +207,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/assuredworkloads_v1beta1/services/assured_workloads_service/transports/grpc.py b/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/transports/grpc.py index 0301374..f823610 100644 --- a/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/transports/grpc.py +++ b/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/transports/grpc.py @@ -388,5 +388,8 @@ def list_workloads( ) return self._stubs["list_workloads"] + def close(self): + self.grpc_channel.close() + __all__ = ("AssuredWorkloadsServiceGrpcTransport",) diff --git a/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/transports/grpc_asyncio.py b/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/transports/grpc_asyncio.py index 90fa1d2..9b43b8e 100644 --- a/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/transports/grpc_asyncio.py +++ b/google/cloud/assuredworkloads_v1beta1/services/assured_workloads_service/transports/grpc_asyncio.py @@ -397,5 +397,8 @@ def list_workloads( ) return self._stubs["list_workloads"] + def close(self): + return self.grpc_channel.close() + __all__ = ("AssuredWorkloadsServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/assuredworkloads_v1beta1/types/assuredworkloads_v1beta1.py b/google/cloud/assuredworkloads_v1beta1/types/assuredworkloads_v1beta1.py index 9fc3daa..02c04bc 100644 --- a/google/cloud/assuredworkloads_v1beta1/types/assuredworkloads_v1beta1.py +++ b/google/cloud/assuredworkloads_v1beta1/types/assuredworkloads_v1beta1.py @@ -37,6 +37,7 @@ class CreateWorkloadRequest(proto.Message): r"""Request for creating a workload. + Attributes: parent (str): Required. The resource name of the new Workload's parent. @@ -60,6 +61,7 @@ class CreateWorkloadRequest(proto.Message): class UpdateWorkloadRequest(proto.Message): r"""Request for Updating a workload. + Attributes: workload (google.cloud.assuredworkloads_v1beta1.types.Workload): Required. The workload to update. The workload’s ``name`` @@ -78,6 +80,7 @@ class UpdateWorkloadRequest(proto.Message): class DeleteWorkloadRequest(proto.Message): r"""Request for deleting a Workload. + Attributes: name (str): Required. The ``name`` field is used to identify the @@ -95,6 +98,7 @@ class DeleteWorkloadRequest(proto.Message): class GetWorkloadRequest(proto.Message): r"""Request for fetching a workload. + Attributes: name (str): Required. The resource name of the Workload to fetch. This @@ -109,6 +113,7 @@ class GetWorkloadRequest(proto.Message): class ListWorkloadsRequest(proto.Message): r"""Request for fetching workloads in an organization. + Attributes: parent (str): Required. Parent Resource to list workloads from. Must be of @@ -134,6 +139,7 @@ class ListWorkloadsRequest(proto.Message): class ListWorkloadsResponse(proto.Message): r"""Response of ListWorkloads endpoint. + Attributes: workloads (Sequence[google.cloud.assuredworkloads_v1beta1.types.Workload]): List of Workloads under a given parent. @@ -243,6 +249,7 @@ class ComplianceRegime(proto.Enum): class ResourceInfo(proto.Message): r"""Represent the resources that are children of this Workload. + Attributes: resource_id (int): Resource identifier. For a project this represents @@ -266,6 +273,7 @@ class ResourceType(proto.Enum): class KMSSettings(proto.Message): r"""Settings specific to the Key Management Service. + Attributes: next_rotation_time (google.protobuf.timestamp_pb2.Timestamp): Required. Input only. Immutable. The time at @@ -288,6 +296,7 @@ class KMSSettings(proto.Message): class IL4Settings(proto.Message): r"""Settings specific to resources needed for IL4. + Attributes: kms_settings (google.cloud.assuredworkloads_v1beta1.types.Workload.KMSSettings): Required. Input only. Immutable. Settings @@ -300,6 +309,7 @@ class IL4Settings(proto.Message): class CJISSettings(proto.Message): r"""Settings specific to resources needed for CJIS. + Attributes: kms_settings (google.cloud.assuredworkloads_v1beta1.types.Workload.KMSSettings): Required. Input only. Immutable. Settings @@ -312,6 +322,7 @@ class CJISSettings(proto.Message): class FedrampHighSettings(proto.Message): r"""Settings specific to resources needed for FedRAMP High. + Attributes: kms_settings (google.cloud.assuredworkloads_v1beta1.types.Workload.KMSSettings): Required. Input only. Immutable. Settings @@ -324,6 +335,7 @@ class FedrampHighSettings(proto.Message): class FedrampModerateSettings(proto.Message): r"""Settings specific to resources needed for FedRAMP Moderate. + Attributes: kms_settings (google.cloud.assuredworkloads_v1beta1.types.Workload.KMSSettings): Required. Input only. Immutable. Settings @@ -400,6 +412,7 @@ class ResourceSettings(proto.Message): class CreateWorkloadOperationMetadata(proto.Message): r"""Operation metadata to give request details of CreateWorkload. + Attributes: create_time (google.protobuf.timestamp_pb2.Timestamp): Optional. Time when the operation was diff --git a/tests/unit/gapic/assuredworkloads_v1/test_assured_workloads_service.py b/tests/unit/gapic/assuredworkloads_v1/test_assured_workloads_service.py index 88c5307..fcd385c 100644 --- a/tests/unit/gapic/assuredworkloads_v1/test_assured_workloads_service.py +++ b/tests/unit/gapic/assuredworkloads_v1/test_assured_workloads_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.assuredworkloads_v1.services.assured_workloads_service import ( @@ -1905,6 +1906,9 @@ def test_assured_workloads_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): @@ -2424,3 +2428,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 = AssuredWorkloadsServiceAsyncClient( + 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 = AssuredWorkloadsServiceClient( + 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 = AssuredWorkloadsServiceClient( + 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() diff --git a/tests/unit/gapic/assuredworkloads_v1beta1/test_assured_workloads_service.py b/tests/unit/gapic/assuredworkloads_v1beta1/test_assured_workloads_service.py index 080450b..65f6d92 100644 --- a/tests/unit/gapic/assuredworkloads_v1beta1/test_assured_workloads_service.py +++ b/tests/unit/gapic/assuredworkloads_v1beta1/test_assured_workloads_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.assuredworkloads_v1beta1.services.assured_workloads_service import ( @@ -1946,6 +1947,9 @@ def test_assured_workloads_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): @@ -2465,3 +2469,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 = AssuredWorkloadsServiceAsyncClient( + 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 = AssuredWorkloadsServiceClient( + 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 = AssuredWorkloadsServiceClient( + 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()