From 740af33ce79a027c5592aabadb58cc367461d6ec 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:38:22 +0000 Subject: [PATCH] feat: add context manager support in client (#230) - [ ] 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/security_center/async_client.py | 6 +++ .../services/security_center/client.py | 18 +++++-- .../security_center/transports/base.py | 9 ++++ .../security_center/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../types/notification_message.py | 1 + .../types/organization_settings.py | 1 + .../cloud/securitycenter_v1/types/resource.py | 1 + .../types/run_asset_discovery_response.py | 1 + .../types/securitycenter_service.py | 27 ++++++++++ .../services/security_center/async_client.py | 6 +++ .../services/security_center/client.py | 18 +++++-- .../security_center/transports/base.py | 9 ++++ .../security_center/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../types/organization_settings.py | 1 + .../types/run_asset_discovery_response.py | 1 + .../types/securitycenter_service.py | 20 ++++++++ .../services/security_center/async_client.py | 6 +++ .../services/security_center/client.py | 18 +++++-- .../security_center/transports/base.py | 9 ++++ .../security_center/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../types/notification_message.py | 1 + .../types/organization_settings.py | 1 + .../types/resource.py | 1 + .../types/run_asset_discovery_response.py | 1 + .../types/securitycenter_service.py | 27 ++++++++++ .../securitycenter_v1/test_security_center.py | 50 +++++++++++++++++++ .../test_security_center.py | 50 +++++++++++++++++++ .../test_security_center.py | 50 +++++++++++++++++++ 31 files changed, 339 insertions(+), 12 deletions(-) diff --git a/google/cloud/securitycenter_v1/services/security_center/async_client.py b/google/cloud/securitycenter_v1/services/security_center/async_client.py index f0338337..fbe22cc0 100644 --- a/google/cloud/securitycenter_v1/services/security_center/async_client.py +++ b/google/cloud/securitycenter_v1/services/security_center/async_client.py @@ -2284,6 +2284,12 @@ async def update_security_marks( # 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/securitycenter_v1/services/security_center/client.py b/google/cloud/securitycenter_v1/services/security_center/client.py index f7c8b3fa..b1785db9 100644 --- a/google/cloud/securitycenter_v1/services/security_center/client.py +++ b/google/cloud/securitycenter_v1/services/security_center/client.py @@ -455,10 +455,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_source( @@ -2467,6 +2464,19 @@ def update_security_marks( # 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/securitycenter_v1/services/security_center/transports/base.py b/google/cloud/securitycenter_v1/services/security_center/transports/base.py index c94fca8f..78032152 100644 --- a/google/cloud/securitycenter_v1/services/security_center/transports/base.py +++ b/google/cloud/securitycenter_v1/services/security_center/transports/base.py @@ -385,6 +385,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/securitycenter_v1/services/security_center/transports/grpc.py b/google/cloud/securitycenter_v1/services/security_center/transports/grpc.py index 3da55ecf..78767185 100644 --- a/google/cloud/securitycenter_v1/services/security_center/transports/grpc.py +++ b/google/cloud/securitycenter_v1/services/security_center/transports/grpc.py @@ -920,5 +920,8 @@ def update_security_marks( ) return self._stubs["update_security_marks"] + def close(self): + self.grpc_channel.close() + __all__ = ("SecurityCenterGrpcTransport",) diff --git a/google/cloud/securitycenter_v1/services/security_center/transports/grpc_asyncio.py b/google/cloud/securitycenter_v1/services/security_center/transports/grpc_asyncio.py index e3848fca..e5cc5011 100644 --- a/google/cloud/securitycenter_v1/services/security_center/transports/grpc_asyncio.py +++ b/google/cloud/securitycenter_v1/services/security_center/transports/grpc_asyncio.py @@ -937,5 +937,8 @@ def update_security_marks( ) return self._stubs["update_security_marks"] + def close(self): + return self.grpc_channel.close() + __all__ = ("SecurityCenterGrpcAsyncIOTransport",) diff --git a/google/cloud/securitycenter_v1/types/notification_message.py b/google/cloud/securitycenter_v1/types/notification_message.py index 49dfce88..d63c2606 100644 --- a/google/cloud/securitycenter_v1/types/notification_message.py +++ b/google/cloud/securitycenter_v1/types/notification_message.py @@ -26,6 +26,7 @@ class NotificationMessage(proto.Message): r"""Cloud SCC's Notification + Attributes: notification_config_name (str): Name of the notification config that diff --git a/google/cloud/securitycenter_v1/types/organization_settings.py b/google/cloud/securitycenter_v1/types/organization_settings.py index 8462c7e7..9bf72dcb 100644 --- a/google/cloud/securitycenter_v1/types/organization_settings.py +++ b/google/cloud/securitycenter_v1/types/organization_settings.py @@ -43,6 +43,7 @@ class OrganizationSettings(proto.Message): class AssetDiscoveryConfig(proto.Message): r"""The configuration used for Asset Discovery runs. + Attributes: project_ids (Sequence[str]): The project ids to use for filtering asset diff --git a/google/cloud/securitycenter_v1/types/resource.py b/google/cloud/securitycenter_v1/types/resource.py index d3101244..20799d7e 100644 --- a/google/cloud/securitycenter_v1/types/resource.py +++ b/google/cloud/securitycenter_v1/types/resource.py @@ -25,6 +25,7 @@ class Resource(proto.Message): r"""Information related to the Google Cloud resource. + Attributes: name (str): The full resource name of the resource. See: diff --git a/google/cloud/securitycenter_v1/types/run_asset_discovery_response.py b/google/cloud/securitycenter_v1/types/run_asset_discovery_response.py index aabbe200..eeb3e34d 100644 --- a/google/cloud/securitycenter_v1/types/run_asset_discovery_response.py +++ b/google/cloud/securitycenter_v1/types/run_asset_discovery_response.py @@ -25,6 +25,7 @@ class RunAssetDiscoveryResponse(proto.Message): r"""Response of asset discovery run + Attributes: state (google.cloud.securitycenter_v1.types.RunAssetDiscoveryResponse.State): The state of an asset discovery run. diff --git a/google/cloud/securitycenter_v1/types/securitycenter_service.py b/google/cloud/securitycenter_v1/types/securitycenter_service.py index 91f76fe4..9f14b1c4 100644 --- a/google/cloud/securitycenter_v1/types/securitycenter_service.py +++ b/google/cloud/securitycenter_v1/types/securitycenter_service.py @@ -68,6 +68,7 @@ class CreateFindingRequest(proto.Message): r"""Request message for creating a finding. + Attributes: parent (str): Required. Resource name of the new finding's parent. Its @@ -92,6 +93,7 @@ class CreateFindingRequest(proto.Message): class CreateNotificationConfigRequest(proto.Message): r"""Request message for creating a notification config. + Attributes: parent (str): Required. Resource name of the new notification config's @@ -118,6 +120,7 @@ class CreateNotificationConfigRequest(proto.Message): class CreateSourceRequest(proto.Message): r"""Request message for creating a source. + Attributes: parent (str): Required. Resource name of the new source's parent. Its @@ -134,6 +137,7 @@ class CreateSourceRequest(proto.Message): class DeleteNotificationConfigRequest(proto.Message): r"""Request message for deleting a notification config. + Attributes: name (str): Required. Name of the notification config to delete. Its @@ -146,6 +150,7 @@ class DeleteNotificationConfigRequest(proto.Message): class GetNotificationConfigRequest(proto.Message): r"""Request message for getting a notification config. + Attributes: name (str): Required. Name of the notification config to get. Its format @@ -158,6 +163,7 @@ class GetNotificationConfigRequest(proto.Message): class GetOrganizationSettingsRequest(proto.Message): r"""Request message for getting organization settings. + Attributes: name (str): Required. Name of the organization to get organization @@ -170,6 +176,7 @@ class GetOrganizationSettingsRequest(proto.Message): class GetSourceRequest(proto.Message): r"""Request message for getting a source. + Attributes: name (str): Required. Relative resource name of the source. Its format @@ -181,6 +188,7 @@ class GetSourceRequest(proto.Message): class GroupAssetsRequest(proto.Message): r"""Request message for grouping by assets. + Attributes: parent (str): Required. Name of the organization to groupBy. Its format is @@ -351,6 +359,7 @@ class GroupAssetsRequest(proto.Message): class GroupAssetsResponse(proto.Message): r"""Response message for grouping by assets. + Attributes: group_by_results (Sequence[google.cloud.securitycenter_v1.types.GroupResult]): Group results. There exists an element for @@ -382,6 +391,7 @@ def raw_page(self): class GroupFindingsRequest(proto.Message): r"""Request message for grouping by findings. + Attributes: parent (str): Required. Name of the source to groupBy. Its format is @@ -553,6 +563,7 @@ class GroupFindingsRequest(proto.Message): class GroupFindingsResponse(proto.Message): r"""Response message for group by findings. + Attributes: group_by_results (Sequence[google.cloud.securitycenter_v1.types.GroupResult]): Group results. There exists an element for @@ -603,6 +614,7 @@ class GroupResult(proto.Message): class ListNotificationConfigsRequest(proto.Message): r"""Request message for listing notification configs. + Attributes: parent (str): Required. Name of the organization to list notification @@ -625,6 +637,7 @@ class ListNotificationConfigsRequest(proto.Message): class ListNotificationConfigsResponse(proto.Message): r"""Response message for listing notification configs. + Attributes: notification_configs (Sequence[google.cloud.securitycenter_v1.types.NotificationConfig]): Notification configs belonging to the @@ -646,6 +659,7 @@ def raw_page(self): class ListSourcesRequest(proto.Message): r"""Request message for listing sources. + Attributes: parent (str): Required. Resource name of the parent of sources to list. @@ -669,6 +683,7 @@ class ListSourcesRequest(proto.Message): class ListSourcesResponse(proto.Message): r"""Response message for listing sources. + Attributes: sources (Sequence[google.cloud.securitycenter_v1.types.Source]): Sources belonging to the requested parent. @@ -687,6 +702,7 @@ def raw_page(self): class ListAssetsRequest(proto.Message): r"""Request message for listing assets. + Attributes: parent (str): Required. Name of the organization assets should belong to. @@ -859,6 +875,7 @@ class ListAssetsRequest(proto.Message): class ListAssetsResponse(proto.Message): r"""Response message for listing assets. + Attributes: list_assets_results (Sequence[google.cloud.securitycenter_v1.types.ListAssetsResponse.ListAssetsResult]): Assets matching the list request. @@ -874,6 +891,7 @@ class ListAssetsResponse(proto.Message): class ListAssetsResult(proto.Message): r"""Result containing the Asset and its State. + Attributes: asset (google.cloud.securitycenter_v1.types.Asset): Asset matching the search request. @@ -916,6 +934,7 @@ def raw_page(self): class ListFindingsRequest(proto.Message): r"""Request message for listing findings. + Attributes: parent (str): Required. Name of the source the findings belong to. Its @@ -1088,6 +1107,7 @@ class ListFindingsRequest(proto.Message): class ListFindingsResponse(proto.Message): r"""Response message for listing findings. + Attributes: list_findings_results (Sequence[google.cloud.securitycenter_v1.types.ListFindingsResponse.ListFindingsResult]): Findings matching the list request. @@ -1103,6 +1123,7 @@ class ListFindingsResponse(proto.Message): class ListFindingsResult(proto.Message): r"""Result containing the Finding and its StateChange. + Attributes: finding (google.cloud.securitycenter_v1.types.Finding): Finding matching the search request. @@ -1190,6 +1211,7 @@ def raw_page(self): class SetFindingStateRequest(proto.Message): r"""Request message for updating a finding's state. + Attributes: name (str): Required. The relative resource name of the finding. See: @@ -1223,6 +1245,7 @@ class RunAssetDiscoveryRequest(proto.Message): class UpdateFindingRequest(proto.Message): r"""Request message for updating or creating a finding. + Attributes: finding (google.cloud.securitycenter_v1.types.Finding): Required. The finding resource to update or create if it @@ -1250,6 +1273,7 @@ class UpdateFindingRequest(proto.Message): class UpdateNotificationConfigRequest(proto.Message): r"""Request message for updating a notification config. + Attributes: notification_config (google.cloud.securitycenter_v1.types.NotificationConfig): Required. The notification config to update. @@ -1269,6 +1293,7 @@ class UpdateNotificationConfigRequest(proto.Message): class UpdateOrganizationSettingsRequest(proto.Message): r"""Request message for updating an organization's settings. + Attributes: organization_settings (google.cloud.securitycenter_v1.types.OrganizationSettings): Required. The organization settings resource @@ -1290,6 +1315,7 @@ class UpdateOrganizationSettingsRequest(proto.Message): class UpdateSourceRequest(proto.Message): r"""Request message for updating a source. + Attributes: source (google.cloud.securitycenter_v1.types.Source): Required. The source resource to update. @@ -1307,6 +1333,7 @@ class UpdateSourceRequest(proto.Message): class UpdateSecurityMarksRequest(proto.Message): r"""Request message for updating a SecurityMarks resource. + Attributes: security_marks (google.cloud.securitycenter_v1.types.SecurityMarks): Required. The security marks resource to diff --git a/google/cloud/securitycenter_v1beta1/services/security_center/async_client.py b/google/cloud/securitycenter_v1beta1/services/security_center/async_client.py index 7723dc7e..6da78690 100644 --- a/google/cloud/securitycenter_v1beta1/services/security_center/async_client.py +++ b/google/cloud/securitycenter_v1beta1/services/security_center/async_client.py @@ -1838,6 +1838,12 @@ async def update_security_marks( # 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/securitycenter_v1beta1/services/security_center/client.py b/google/cloud/securitycenter_v1beta1/services/security_center/client.py index 0cdb40ec..e9317e89 100644 --- a/google/cloud/securitycenter_v1beta1/services/security_center/client.py +++ b/google/cloud/securitycenter_v1beta1/services/security_center/client.py @@ -423,10 +423,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_source( @@ -1996,6 +1993,19 @@ def update_security_marks( # 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/securitycenter_v1beta1/services/security_center/transports/base.py b/google/cloud/securitycenter_v1beta1/services/security_center/transports/base.py index 92be13c1..06fe9815 100644 --- a/google/cloud/securitycenter_v1beta1/services/security_center/transports/base.py +++ b/google/cloud/securitycenter_v1beta1/services/security_center/transports/base.py @@ -337,6 +337,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/securitycenter_v1beta1/services/security_center/transports/grpc.py b/google/cloud/securitycenter_v1beta1/services/security_center/transports/grpc.py index b13d5dbd..816497c0 100644 --- a/google/cloud/securitycenter_v1beta1/services/security_center/transports/grpc.py +++ b/google/cloud/securitycenter_v1beta1/services/security_center/transports/grpc.py @@ -772,5 +772,8 @@ def update_security_marks( ) return self._stubs["update_security_marks"] + def close(self): + self.grpc_channel.close() + __all__ = ("SecurityCenterGrpcTransport",) diff --git a/google/cloud/securitycenter_v1beta1/services/security_center/transports/grpc_asyncio.py b/google/cloud/securitycenter_v1beta1/services/security_center/transports/grpc_asyncio.py index b7ef8bf8..70547c00 100644 --- a/google/cloud/securitycenter_v1beta1/services/security_center/transports/grpc_asyncio.py +++ b/google/cloud/securitycenter_v1beta1/services/security_center/transports/grpc_asyncio.py @@ -788,5 +788,8 @@ def update_security_marks( ) return self._stubs["update_security_marks"] + def close(self): + return self.grpc_channel.close() + __all__ = ("SecurityCenterGrpcAsyncIOTransport",) diff --git a/google/cloud/securitycenter_v1beta1/types/organization_settings.py b/google/cloud/securitycenter_v1beta1/types/organization_settings.py index d76fe161..5cb641cd 100644 --- a/google/cloud/securitycenter_v1beta1/types/organization_settings.py +++ b/google/cloud/securitycenter_v1beta1/types/organization_settings.py @@ -43,6 +43,7 @@ class OrganizationSettings(proto.Message): class AssetDiscoveryConfig(proto.Message): r"""The configuration used for Asset Discovery runs. + Attributes: project_ids (Sequence[str]): The project ids to use for filtering asset diff --git a/google/cloud/securitycenter_v1beta1/types/run_asset_discovery_response.py b/google/cloud/securitycenter_v1beta1/types/run_asset_discovery_response.py index 3e78a7ad..3e51bf94 100644 --- a/google/cloud/securitycenter_v1beta1/types/run_asset_discovery_response.py +++ b/google/cloud/securitycenter_v1beta1/types/run_asset_discovery_response.py @@ -26,6 +26,7 @@ class RunAssetDiscoveryResponse(proto.Message): r"""Response of asset discovery run + Attributes: state (google.cloud.securitycenter_v1beta1.types.RunAssetDiscoveryResponse.State): The state of an asset discovery run. diff --git a/google/cloud/securitycenter_v1beta1/types/securitycenter_service.py b/google/cloud/securitycenter_v1beta1/types/securitycenter_service.py index d942e4bc..e9d56251 100644 --- a/google/cloud/securitycenter_v1beta1/types/securitycenter_service.py +++ b/google/cloud/securitycenter_v1beta1/types/securitycenter_service.py @@ -60,6 +60,7 @@ class CreateFindingRequest(proto.Message): r"""Request message for creating a finding. + Attributes: parent (str): Required. Resource name of the new finding's parent. Its @@ -84,6 +85,7 @@ class CreateFindingRequest(proto.Message): class CreateSourceRequest(proto.Message): r"""Request message for creating a source. + Attributes: parent (str): Required. Resource name of the new source's parent. Its @@ -100,6 +102,7 @@ class CreateSourceRequest(proto.Message): class GetOrganizationSettingsRequest(proto.Message): r"""Request message for getting organization settings. + Attributes: name (str): Required. Name of the organization to get organization @@ -112,6 +115,7 @@ class GetOrganizationSettingsRequest(proto.Message): class GetSourceRequest(proto.Message): r"""Request message for getting a source. + Attributes: name (str): Required. Relative resource name of the source. Its format @@ -123,6 +127,7 @@ class GetSourceRequest(proto.Message): class GroupAssetsRequest(proto.Message): r"""Request message for grouping by assets. + Attributes: parent (str): Required. Name of the organization to groupBy. Its format is @@ -231,6 +236,7 @@ class GroupAssetsRequest(proto.Message): class GroupAssetsResponse(proto.Message): r"""Response message for grouping by assets. + Attributes: group_by_results (Sequence[google.cloud.securitycenter_v1beta1.types.GroupResult]): Group results. There exists an element for @@ -258,6 +264,7 @@ def raw_page(self): class GroupFindingsRequest(proto.Message): r"""Request message for grouping by findings. + Attributes: parent (str): Required. Name of the source to groupBy. Its format is @@ -332,6 +339,7 @@ class GroupFindingsRequest(proto.Message): class GroupFindingsResponse(proto.Message): r"""Response message for group by findings. + Attributes: group_by_results (Sequence[google.cloud.securitycenter_v1beta1.types.GroupResult]): Group results. There exists an element for @@ -378,6 +386,7 @@ class GroupResult(proto.Message): class ListSourcesRequest(proto.Message): r"""Request message for listing sources. + Attributes: parent (str): Required. Resource name of the parent of sources to list. @@ -400,6 +409,7 @@ class ListSourcesRequest(proto.Message): class ListSourcesResponse(proto.Message): r"""Response message for listing sources. + Attributes: sources (Sequence[google.cloud.securitycenter_v1beta1.types.Source]): Sources belonging to the requested parent. @@ -418,6 +428,7 @@ def raw_page(self): class ListAssetsRequest(proto.Message): r"""Request message for listing assets. + Attributes: parent (str): Required. Name of the organization assets should belong to. @@ -527,6 +538,7 @@ class ListAssetsRequest(proto.Message): class ListAssetsResponse(proto.Message): r"""Response message for listing assets. + Attributes: list_assets_results (Sequence[google.cloud.securitycenter_v1beta1.types.ListAssetsResponse.ListAssetsResult]): Assets matching the list request. @@ -542,6 +554,7 @@ class ListAssetsResponse(proto.Message): class ListAssetsResult(proto.Message): r"""Result containing the Asset and its State. + Attributes: asset (google.cloud.securitycenter_v1beta1.types.Asset): Asset matching the search request. @@ -581,6 +594,7 @@ def raw_page(self): class ListFindingsRequest(proto.Message): r"""Request message for listing findings. + Attributes: parent (str): Required. Name of the source the findings belong to. Its @@ -660,6 +674,7 @@ class ListFindingsRequest(proto.Message): class ListFindingsResponse(proto.Message): r"""Response message for listing findings. + Attributes: findings (Sequence[google.cloud.securitycenter_v1beta1.types.Finding]): Findings matching the list request. @@ -687,6 +702,7 @@ def raw_page(self): class SetFindingStateRequest(proto.Message): r"""Request message for updating a finding's state. + Attributes: name (str): Required. The relative resource name of the finding. See: @@ -720,6 +736,7 @@ class RunAssetDiscoveryRequest(proto.Message): class UpdateFindingRequest(proto.Message): r"""Request message for updating or creating a finding. + Attributes: finding (google.cloud.securitycenter_v1beta1.types.Finding): Required. The finding resource to update or create if it @@ -743,6 +760,7 @@ class UpdateFindingRequest(proto.Message): class UpdateOrganizationSettingsRequest(proto.Message): r"""Request message for updating an organization's settings. + Attributes: organization_settings (google.cloud.securitycenter_v1beta1.types.OrganizationSettings): Required. The organization settings resource @@ -762,6 +780,7 @@ class UpdateOrganizationSettingsRequest(proto.Message): class UpdateSourceRequest(proto.Message): r"""Request message for updating a source. + Attributes: source (google.cloud.securitycenter_v1beta1.types.Source): Required. The source resource to update. @@ -778,6 +797,7 @@ class UpdateSourceRequest(proto.Message): class UpdateSecurityMarksRequest(proto.Message): r"""Request message for updating a SecurityMarks resource. + Attributes: security_marks (google.cloud.securitycenter_v1beta1.types.SecurityMarks): Required. The security marks resource to diff --git a/google/cloud/securitycenter_v1p1beta1/services/security_center/async_client.py b/google/cloud/securitycenter_v1p1beta1/services/security_center/async_client.py index 6aa30766..f906a8b7 100644 --- a/google/cloud/securitycenter_v1p1beta1/services/security_center/async_client.py +++ b/google/cloud/securitycenter_v1p1beta1/services/security_center/async_client.py @@ -2381,6 +2381,12 @@ async def update_security_marks( # 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/securitycenter_v1p1beta1/services/security_center/client.py b/google/cloud/securitycenter_v1p1beta1/services/security_center/client.py index 82f32c35..cd4b4be0 100644 --- a/google/cloud/securitycenter_v1p1beta1/services/security_center/client.py +++ b/google/cloud/securitycenter_v1p1beta1/services/security_center/client.py @@ -456,10 +456,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_source( @@ -2562,6 +2559,19 @@ def update_security_marks( # 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/securitycenter_v1p1beta1/services/security_center/transports/base.py b/google/cloud/securitycenter_v1p1beta1/services/security_center/transports/base.py index 72a996c8..2e0ba33f 100644 --- a/google/cloud/securitycenter_v1p1beta1/services/security_center/transports/base.py +++ b/google/cloud/securitycenter_v1p1beta1/services/security_center/transports/base.py @@ -387,6 +387,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/securitycenter_v1p1beta1/services/security_center/transports/grpc.py b/google/cloud/securitycenter_v1p1beta1/services/security_center/transports/grpc.py index 809d4093..fcec5523 100644 --- a/google/cloud/securitycenter_v1p1beta1/services/security_center/transports/grpc.py +++ b/google/cloud/securitycenter_v1p1beta1/services/security_center/transports/grpc.py @@ -923,5 +923,8 @@ def update_security_marks( ) return self._stubs["update_security_marks"] + def close(self): + self.grpc_channel.close() + __all__ = ("SecurityCenterGrpcTransport",) diff --git a/google/cloud/securitycenter_v1p1beta1/services/security_center/transports/grpc_asyncio.py b/google/cloud/securitycenter_v1p1beta1/services/security_center/transports/grpc_asyncio.py index 6ca1d810..8ffec364 100644 --- a/google/cloud/securitycenter_v1p1beta1/services/security_center/transports/grpc_asyncio.py +++ b/google/cloud/securitycenter_v1p1beta1/services/security_center/transports/grpc_asyncio.py @@ -940,5 +940,8 @@ def update_security_marks( ) return self._stubs["update_security_marks"] + def close(self): + return self.grpc_channel.close() + __all__ = ("SecurityCenterGrpcAsyncIOTransport",) diff --git a/google/cloud/securitycenter_v1p1beta1/types/notification_message.py b/google/cloud/securitycenter_v1p1beta1/types/notification_message.py index 484cd101..ca4d470e 100644 --- a/google/cloud/securitycenter_v1p1beta1/types/notification_message.py +++ b/google/cloud/securitycenter_v1p1beta1/types/notification_message.py @@ -26,6 +26,7 @@ class NotificationMessage(proto.Message): r"""Security Command Center's Notification + Attributes: notification_config_name (str): Name of the notification config that diff --git a/google/cloud/securitycenter_v1p1beta1/types/organization_settings.py b/google/cloud/securitycenter_v1p1beta1/types/organization_settings.py index 604b9c09..212ce085 100644 --- a/google/cloud/securitycenter_v1p1beta1/types/organization_settings.py +++ b/google/cloud/securitycenter_v1p1beta1/types/organization_settings.py @@ -43,6 +43,7 @@ class OrganizationSettings(proto.Message): class AssetDiscoveryConfig(proto.Message): r"""The configuration used for Asset Discovery runs. + Attributes: project_ids (Sequence[str]): The project ids to use for filtering asset diff --git a/google/cloud/securitycenter_v1p1beta1/types/resource.py b/google/cloud/securitycenter_v1p1beta1/types/resource.py index ab3ea8fa..8cc75dfb 100644 --- a/google/cloud/securitycenter_v1p1beta1/types/resource.py +++ b/google/cloud/securitycenter_v1p1beta1/types/resource.py @@ -25,6 +25,7 @@ class Resource(proto.Message): r"""Information related to the Google Cloud resource. + Attributes: name (str): The full resource name of the resource. See: diff --git a/google/cloud/securitycenter_v1p1beta1/types/run_asset_discovery_response.py b/google/cloud/securitycenter_v1p1beta1/types/run_asset_discovery_response.py index 8fb57687..90301258 100644 --- a/google/cloud/securitycenter_v1p1beta1/types/run_asset_discovery_response.py +++ b/google/cloud/securitycenter_v1p1beta1/types/run_asset_discovery_response.py @@ -26,6 +26,7 @@ class RunAssetDiscoveryResponse(proto.Message): r"""Response of asset discovery run + Attributes: state (google.cloud.securitycenter_v1p1beta1.types.RunAssetDiscoveryResponse.State): The state of an asset discovery run. diff --git a/google/cloud/securitycenter_v1p1beta1/types/securitycenter_service.py b/google/cloud/securitycenter_v1p1beta1/types/securitycenter_service.py index 12533a09..7fd1f639 100644 --- a/google/cloud/securitycenter_v1p1beta1/types/securitycenter_service.py +++ b/google/cloud/securitycenter_v1p1beta1/types/securitycenter_service.py @@ -70,6 +70,7 @@ class CreateFindingRequest(proto.Message): r"""Request message for creating a finding. + Attributes: parent (str): Required. Resource name of the new finding's parent. Its @@ -91,6 +92,7 @@ class CreateFindingRequest(proto.Message): class CreateNotificationConfigRequest(proto.Message): r"""Request message for creating a notification config. + Attributes: parent (str): Required. Resource name of the new notification config's @@ -117,6 +119,7 @@ class CreateNotificationConfigRequest(proto.Message): class CreateSourceRequest(proto.Message): r"""Request message for creating a source. + Attributes: parent (str): Required. Resource name of the new source's parent. Its @@ -133,6 +136,7 @@ class CreateSourceRequest(proto.Message): class DeleteNotificationConfigRequest(proto.Message): r"""Request message for deleting a notification config. + Attributes: name (str): Required. Name of the notification config to delete. Its @@ -145,6 +149,7 @@ class DeleteNotificationConfigRequest(proto.Message): class GetNotificationConfigRequest(proto.Message): r"""Request message for getting a notification config. + Attributes: name (str): Required. Name of the notification config to get. Its format @@ -157,6 +162,7 @@ class GetNotificationConfigRequest(proto.Message): class GetOrganizationSettingsRequest(proto.Message): r"""Request message for getting organization settings. + Attributes: name (str): Required. Name of the organization to get organization @@ -169,6 +175,7 @@ class GetOrganizationSettingsRequest(proto.Message): class GetSourceRequest(proto.Message): r"""Request message for getting a source. + Attributes: name (str): Required. Relative resource name of the source. Its format @@ -180,6 +187,7 @@ class GetSourceRequest(proto.Message): class GroupAssetsRequest(proto.Message): r"""Request message for grouping by assets. + Attributes: parent (str): Required. Name of the organization to groupBy. Its format is @@ -350,6 +358,7 @@ class GroupAssetsRequest(proto.Message): class GroupAssetsResponse(proto.Message): r"""Response message for grouping by assets. + Attributes: group_by_results (Sequence[google.cloud.securitycenter_v1p1beta1.types.GroupResult]): Group results. There exists an element for @@ -381,6 +390,7 @@ def raw_page(self): class GroupFindingsRequest(proto.Message): r"""Request message for grouping by findings. + Attributes: parent (str): Required. Name of the source to groupBy. Its format is @@ -540,6 +550,7 @@ class GroupFindingsRequest(proto.Message): class GroupFindingsResponse(proto.Message): r"""Response message for group by findings. + Attributes: group_by_results (Sequence[google.cloud.securitycenter_v1p1beta1.types.GroupResult]): Group results. There exists an element for @@ -590,6 +601,7 @@ class GroupResult(proto.Message): class ListNotificationConfigsRequest(proto.Message): r"""Request message for listing notification configs. + Attributes: parent (str): Required. Name of the organization to list notification @@ -612,6 +624,7 @@ class ListNotificationConfigsRequest(proto.Message): class ListNotificationConfigsResponse(proto.Message): r"""Response message for listing notification configs. + Attributes: notification_configs (Sequence[google.cloud.securitycenter_v1p1beta1.types.NotificationConfig]): Notification configs belonging to the @@ -633,6 +646,7 @@ def raw_page(self): class ListSourcesRequest(proto.Message): r"""Request message for listing sources. + Attributes: parent (str): Required. Resource name of the parent of sources to list. @@ -656,6 +670,7 @@ class ListSourcesRequest(proto.Message): class ListSourcesResponse(proto.Message): r"""Response message for listing sources. + Attributes: sources (Sequence[google.cloud.securitycenter_v1p1beta1.types.Source]): Sources belonging to the requested parent. @@ -674,6 +689,7 @@ def raw_page(self): class ListAssetsRequest(proto.Message): r"""Request message for listing assets. + Attributes: parent (str): Required. Name of the organization assets should belong to. @@ -846,6 +862,7 @@ class ListAssetsRequest(proto.Message): class ListAssetsResponse(proto.Message): r"""Response message for listing assets. + Attributes: list_assets_results (Sequence[google.cloud.securitycenter_v1p1beta1.types.ListAssetsResponse.ListAssetsResult]): Assets matching the list request. @@ -861,6 +878,7 @@ class ListAssetsResponse(proto.Message): class ListAssetsResult(proto.Message): r"""Result containing the Asset and its State. + Attributes: asset (google.cloud.securitycenter_v1p1beta1.types.Asset): Asset matching the search request. @@ -903,6 +921,7 @@ def raw_page(self): class ListFindingsRequest(proto.Message): r"""Request message for listing findings. + Attributes: parent (str): Required. Name of the source the findings belong to. Its @@ -1060,6 +1079,7 @@ class ListFindingsRequest(proto.Message): class ListFindingsResponse(proto.Message): r"""Response message for listing findings. + Attributes: list_findings_results (Sequence[google.cloud.securitycenter_v1p1beta1.types.ListFindingsResponse.ListFindingsResult]): Findings matching the list request. @@ -1075,6 +1095,7 @@ class ListFindingsResponse(proto.Message): class ListFindingsResult(proto.Message): r"""Result containing the Finding and its StateChange. + Attributes: finding (google.cloud.securitycenter_v1p1beta1.types.Finding): Finding matching the search request. @@ -1162,6 +1183,7 @@ def raw_page(self): class SetFindingStateRequest(proto.Message): r"""Request message for updating a finding's state. + Attributes: name (str): Required. The relative resource name of the finding. See: @@ -1195,6 +1217,7 @@ class RunAssetDiscoveryRequest(proto.Message): class UpdateFindingRequest(proto.Message): r"""Request message for updating or creating a finding. + Attributes: finding (google.cloud.securitycenter_v1p1beta1.types.Finding): Required. The finding resource to update or create if it @@ -1222,6 +1245,7 @@ class UpdateFindingRequest(proto.Message): class UpdateNotificationConfigRequest(proto.Message): r"""Request message for updating a notification config. + Attributes: notification_config (google.cloud.securitycenter_v1p1beta1.types.NotificationConfig): Required. The notification config to update. @@ -1241,6 +1265,7 @@ class UpdateNotificationConfigRequest(proto.Message): class UpdateOrganizationSettingsRequest(proto.Message): r"""Request message for updating an organization's settings. + Attributes: organization_settings (google.cloud.securitycenter_v1p1beta1.types.OrganizationSettings): Required. The organization settings resource @@ -1262,6 +1287,7 @@ class UpdateOrganizationSettingsRequest(proto.Message): class UpdateSourceRequest(proto.Message): r"""Request message for updating a source. + Attributes: source (google.cloud.securitycenter_v1p1beta1.types.Source): Required. The source resource to update. @@ -1279,6 +1305,7 @@ class UpdateSourceRequest(proto.Message): class UpdateSecurityMarksRequest(proto.Message): r"""Request message for updating a SecurityMarks resource. + Attributes: security_marks (google.cloud.securitycenter_v1p1beta1.types.SecurityMarks): Required. The security marks resource to diff --git a/tests/unit/gapic/securitycenter_v1/test_security_center.py b/tests/unit/gapic/securitycenter_v1/test_security_center.py index edbd7bd1..6834f803 100644 --- a/tests/unit/gapic/securitycenter_v1/test_security_center.py +++ b/tests/unit/gapic/securitycenter_v1/test_security_center.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.securitycenter_v1.services.security_center import ( @@ -6731,6 +6732,9 @@ def test_security_center_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): @@ -7374,3 +7378,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 = SecurityCenterAsyncClient( + 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 = SecurityCenterClient( + 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 = SecurityCenterClient( + 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/securitycenter_v1beta1/test_security_center.py b/tests/unit/gapic/securitycenter_v1beta1/test_security_center.py index 4506c320..c9da493d 100644 --- a/tests/unit/gapic/securitycenter_v1beta1/test_security_center.py +++ b/tests/unit/gapic/securitycenter_v1beta1/test_security_center.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.securitycenter_v1beta1.services.security_center import ( @@ -5229,6 +5230,9 @@ def test_security_center_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): @@ -5828,3 +5832,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 = SecurityCenterAsyncClient( + 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 = SecurityCenterClient( + 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 = SecurityCenterClient( + 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/securitycenter_v1p1beta1/test_security_center.py b/tests/unit/gapic/securitycenter_v1p1beta1/test_security_center.py index e729675f..7e3c717f 100644 --- a/tests/unit/gapic/securitycenter_v1p1beta1/test_security_center.py +++ b/tests/unit/gapic/securitycenter_v1p1beta1/test_security_center.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.securitycenter_v1p1beta1.services.security_center import ( @@ -6908,6 +6909,9 @@ def test_security_center_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): @@ -7551,3 +7555,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 = SecurityCenterAsyncClient( + 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 = SecurityCenterClient( + 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 = SecurityCenterClient( + 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()