Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

feat: add context manager support in client #94

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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(
Expand Down
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Expand Up @@ -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."""
Expand Down
Expand Up @@ -380,5 +380,8 @@ def list_workloads(
)
return self._stubs["list_workloads"]

def close(self):
self.grpc_channel.close()


__all__ = ("AssuredWorkloadsServiceGrpcTransport",)
Expand Up @@ -392,5 +392,8 @@ def list_workloads(
)
return self._stubs["list_workloads"]

def close(self):
return self.grpc_channel.close()


__all__ = ("AssuredWorkloadsServiceGrpcAsyncIOTransport",)
9 changes: 9 additions & 0 deletions google/cloud/assuredworkloads_v1/types/assuredworkloads.py
Expand Up @@ -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.
Expand All @@ -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``
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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(
Expand Down
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Expand Up @@ -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."""
Expand Down
Expand Up @@ -388,5 +388,8 @@ def list_workloads(
)
return self._stubs["list_workloads"]

def close(self):
self.grpc_channel.close()


__all__ = ("AssuredWorkloadsServiceGrpcTransport",)
Expand Up @@ -397,5 +397,8 @@ def list_workloads(
)
return self._stubs["list_workloads"]

def close(self):
return self.grpc_channel.close()


__all__ = ("AssuredWorkloadsServiceGrpcAsyncIOTransport",)
Expand Up @@ -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.
Expand All @@ -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``
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down