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

Commit 740af33

Browse files
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: googleapis/googleapis@787f8c9 Source-Link: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent c1b9595 commit 740af33

31 files changed

Lines changed: 339 additions & 12 deletions

google/cloud/securitycenter_v1/services/security_center/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,6 +2284,12 @@ async def update_security_marks(
22842284
# Done; return the response.
22852285
return response
22862286

2287+
async def __aenter__(self):
2288+
return self
2289+
2290+
async def __aexit__(self, exc_type, exc, tb):
2291+
await self.transport.close()
2292+
22872293

22882294
try:
22892295
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/securitycenter_v1/services/security_center/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,7 @@ def __init__(
455455
client_cert_source_for_mtls=client_cert_source_func,
456456
quota_project_id=client_options.quota_project_id,
457457
client_info=client_info,
458-
always_use_jwt_access=(
459-
Transport == type(self).get_transport_class("grpc")
460-
or Transport == type(self).get_transport_class("grpc_asyncio")
461-
),
458+
always_use_jwt_access=True,
462459
)
463460

464461
def create_source(
@@ -2467,6 +2464,19 @@ def update_security_marks(
24672464
# Done; return the response.
24682465
return response
24692466

2467+
def __enter__(self):
2468+
return self
2469+
2470+
def __exit__(self, type, value, traceback):
2471+
"""Releases underlying transport's resources.
2472+
2473+
.. warning::
2474+
ONLY use as a context manager if the transport is NOT shared
2475+
with other clients! Exiting the with block will CLOSE the transport
2476+
and may cause errors in other clients!
2477+
"""
2478+
self.transport.close()
2479+
24702480

24712481
try:
24722482
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/securitycenter_v1/services/security_center/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,15 @@ def _prep_wrapped_messages(self, client_info):
385385
),
386386
}
387387

388+
def close(self):
389+
"""Closes resources associated with the transport.
390+
391+
.. warning::
392+
Only call this method if the transport is NOT shared
393+
with other clients - this may cause errors in other clients!
394+
"""
395+
raise NotImplementedError()
396+
388397
@property
389398
def operations_client(self) -> operations_v1.OperationsClient:
390399
"""Return the client designed to process long-running operations."""

google/cloud/securitycenter_v1/services/security_center/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,5 +920,8 @@ def update_security_marks(
920920
)
921921
return self._stubs["update_security_marks"]
922922

923+
def close(self):
924+
self.grpc_channel.close()
925+
923926

924927
__all__ = ("SecurityCenterGrpcTransport",)

google/cloud/securitycenter_v1/services/security_center/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,5 +937,8 @@ def update_security_marks(
937937
)
938938
return self._stubs["update_security_marks"]
939939

940+
def close(self):
941+
return self.grpc_channel.close()
942+
940943

941944
__all__ = ("SecurityCenterGrpcAsyncIOTransport",)

google/cloud/securitycenter_v1/types/notification_message.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
class NotificationMessage(proto.Message):
2828
r"""Cloud SCC's Notification
29+
2930
Attributes:
3031
notification_config_name (str):
3132
Name of the notification config that

google/cloud/securitycenter_v1/types/organization_settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class OrganizationSettings(proto.Message):
4343

4444
class AssetDiscoveryConfig(proto.Message):
4545
r"""The configuration used for Asset Discovery runs.
46+
4647
Attributes:
4748
project_ids (Sequence[str]):
4849
The project ids to use for filtering asset

google/cloud/securitycenter_v1/types/resource.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
class Resource(proto.Message):
2727
r"""Information related to the Google Cloud resource.
28+
2829
Attributes:
2930
name (str):
3031
The full resource name of the resource. See:

google/cloud/securitycenter_v1/types/run_asset_discovery_response.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
class RunAssetDiscoveryResponse(proto.Message):
2727
r"""Response of asset discovery run
28+
2829
Attributes:
2930
state (google.cloud.securitycenter_v1.types.RunAssetDiscoveryResponse.State):
3031
The state of an asset discovery run.

google/cloud/securitycenter_v1/types/securitycenter_service.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868

6969
class CreateFindingRequest(proto.Message):
7070
r"""Request message for creating a finding.
71+
7172
Attributes:
7273
parent (str):
7374
Required. Resource name of the new finding's parent. Its
@@ -92,6 +93,7 @@ class CreateFindingRequest(proto.Message):
9293

9394
class CreateNotificationConfigRequest(proto.Message):
9495
r"""Request message for creating a notification config.
96+
9597
Attributes:
9698
parent (str):
9799
Required. Resource name of the new notification config's
@@ -118,6 +120,7 @@ class CreateNotificationConfigRequest(proto.Message):
118120

119121
class CreateSourceRequest(proto.Message):
120122
r"""Request message for creating a source.
123+
121124
Attributes:
122125
parent (str):
123126
Required. Resource name of the new source's parent. Its
@@ -134,6 +137,7 @@ class CreateSourceRequest(proto.Message):
134137

135138
class DeleteNotificationConfigRequest(proto.Message):
136139
r"""Request message for deleting a notification config.
140+
137141
Attributes:
138142
name (str):
139143
Required. Name of the notification config to delete. Its
@@ -146,6 +150,7 @@ class DeleteNotificationConfigRequest(proto.Message):
146150

147151
class GetNotificationConfigRequest(proto.Message):
148152
r"""Request message for getting a notification config.
153+
149154
Attributes:
150155
name (str):
151156
Required. Name of the notification config to get. Its format
@@ -158,6 +163,7 @@ class GetNotificationConfigRequest(proto.Message):
158163

159164
class GetOrganizationSettingsRequest(proto.Message):
160165
r"""Request message for getting organization settings.
166+
161167
Attributes:
162168
name (str):
163169
Required. Name of the organization to get organization
@@ -170,6 +176,7 @@ class GetOrganizationSettingsRequest(proto.Message):
170176

171177
class GetSourceRequest(proto.Message):
172178
r"""Request message for getting a source.
179+
173180
Attributes:
174181
name (str):
175182
Required. Relative resource name of the source. Its format
@@ -181,6 +188,7 @@ class GetSourceRequest(proto.Message):
181188

182189
class GroupAssetsRequest(proto.Message):
183190
r"""Request message for grouping by assets.
191+
184192
Attributes:
185193
parent (str):
186194
Required. Name of the organization to groupBy. Its format is
@@ -351,6 +359,7 @@ class GroupAssetsRequest(proto.Message):
351359

352360
class GroupAssetsResponse(proto.Message):
353361
r"""Response message for grouping by assets.
362+
354363
Attributes:
355364
group_by_results (Sequence[google.cloud.securitycenter_v1.types.GroupResult]):
356365
Group results. There exists an element for
@@ -382,6 +391,7 @@ def raw_page(self):
382391

383392
class GroupFindingsRequest(proto.Message):
384393
r"""Request message for grouping by findings.
394+
385395
Attributes:
386396
parent (str):
387397
Required. Name of the source to groupBy. Its format is
@@ -553,6 +563,7 @@ class GroupFindingsRequest(proto.Message):
553563

554564
class GroupFindingsResponse(proto.Message):
555565
r"""Response message for group by findings.
566+
556567
Attributes:
557568
group_by_results (Sequence[google.cloud.securitycenter_v1.types.GroupResult]):
558569
Group results. There exists an element for
@@ -603,6 +614,7 @@ class GroupResult(proto.Message):
603614

604615
class ListNotificationConfigsRequest(proto.Message):
605616
r"""Request message for listing notification configs.
617+
606618
Attributes:
607619
parent (str):
608620
Required. Name of the organization to list notification
@@ -625,6 +637,7 @@ class ListNotificationConfigsRequest(proto.Message):
625637

626638
class ListNotificationConfigsResponse(proto.Message):
627639
r"""Response message for listing notification configs.
640+
628641
Attributes:
629642
notification_configs (Sequence[google.cloud.securitycenter_v1.types.NotificationConfig]):
630643
Notification configs belonging to the
@@ -646,6 +659,7 @@ def raw_page(self):
646659

647660
class ListSourcesRequest(proto.Message):
648661
r"""Request message for listing sources.
662+
649663
Attributes:
650664
parent (str):
651665
Required. Resource name of the parent of sources to list.
@@ -669,6 +683,7 @@ class ListSourcesRequest(proto.Message):
669683

670684
class ListSourcesResponse(proto.Message):
671685
r"""Response message for listing sources.
686+
672687
Attributes:
673688
sources (Sequence[google.cloud.securitycenter_v1.types.Source]):
674689
Sources belonging to the requested parent.
@@ -687,6 +702,7 @@ def raw_page(self):
687702

688703
class ListAssetsRequest(proto.Message):
689704
r"""Request message for listing assets.
705+
690706
Attributes:
691707
parent (str):
692708
Required. Name of the organization assets should belong to.
@@ -859,6 +875,7 @@ class ListAssetsRequest(proto.Message):
859875

860876
class ListAssetsResponse(proto.Message):
861877
r"""Response message for listing assets.
878+
862879
Attributes:
863880
list_assets_results (Sequence[google.cloud.securitycenter_v1.types.ListAssetsResponse.ListAssetsResult]):
864881
Assets matching the list request.
@@ -874,6 +891,7 @@ class ListAssetsResponse(proto.Message):
874891

875892
class ListAssetsResult(proto.Message):
876893
r"""Result containing the Asset and its State.
894+
877895
Attributes:
878896
asset (google.cloud.securitycenter_v1.types.Asset):
879897
Asset matching the search request.
@@ -916,6 +934,7 @@ def raw_page(self):
916934

917935
class ListFindingsRequest(proto.Message):
918936
r"""Request message for listing findings.
937+
919938
Attributes:
920939
parent (str):
921940
Required. Name of the source the findings belong to. Its
@@ -1088,6 +1107,7 @@ class ListFindingsRequest(proto.Message):
10881107

10891108
class ListFindingsResponse(proto.Message):
10901109
r"""Response message for listing findings.
1110+
10911111
Attributes:
10921112
list_findings_results (Sequence[google.cloud.securitycenter_v1.types.ListFindingsResponse.ListFindingsResult]):
10931113
Findings matching the list request.
@@ -1103,6 +1123,7 @@ class ListFindingsResponse(proto.Message):
11031123

11041124
class ListFindingsResult(proto.Message):
11051125
r"""Result containing the Finding and its StateChange.
1126+
11061127
Attributes:
11071128
finding (google.cloud.securitycenter_v1.types.Finding):
11081129
Finding matching the search request.
@@ -1190,6 +1211,7 @@ def raw_page(self):
11901211

11911212
class SetFindingStateRequest(proto.Message):
11921213
r"""Request message for updating a finding's state.
1214+
11931215
Attributes:
11941216
name (str):
11951217
Required. The relative resource name of the finding. See:
@@ -1223,6 +1245,7 @@ class RunAssetDiscoveryRequest(proto.Message):
12231245

12241246
class UpdateFindingRequest(proto.Message):
12251247
r"""Request message for updating or creating a finding.
1248+
12261249
Attributes:
12271250
finding (google.cloud.securitycenter_v1.types.Finding):
12281251
Required. The finding resource to update or create if it
@@ -1250,6 +1273,7 @@ class UpdateFindingRequest(proto.Message):
12501273

12511274
class UpdateNotificationConfigRequest(proto.Message):
12521275
r"""Request message for updating a notification config.
1276+
12531277
Attributes:
12541278
notification_config (google.cloud.securitycenter_v1.types.NotificationConfig):
12551279
Required. The notification config to update.
@@ -1269,6 +1293,7 @@ class UpdateNotificationConfigRequest(proto.Message):
12691293

12701294
class UpdateOrganizationSettingsRequest(proto.Message):
12711295
r"""Request message for updating an organization's settings.
1296+
12721297
Attributes:
12731298
organization_settings (google.cloud.securitycenter_v1.types.OrganizationSettings):
12741299
Required. The organization settings resource
@@ -1290,6 +1315,7 @@ class UpdateOrganizationSettingsRequest(proto.Message):
12901315

12911316
class UpdateSourceRequest(proto.Message):
12921317
r"""Request message for updating a source.
1318+
12931319
Attributes:
12941320
source (google.cloud.securitycenter_v1.types.Source):
12951321
Required. The source resource to update.
@@ -1307,6 +1333,7 @@ class UpdateSourceRequest(proto.Message):
13071333

13081334
class UpdateSecurityMarksRequest(proto.Message):
13091335
r"""Request message for updating a SecurityMarks resource.
1336+
13101337
Attributes:
13111338
security_marks (google.cloud.securitycenter_v1.types.SecurityMarks):
13121339
Required. The security marks resource to

0 commit comments

Comments
 (0)