Skip to content

Commit f33ab50

Browse files
committed
add samples for using a regional endpoint with SCC v2 API
The SCC API can be accessed via Regional Endpoints to meet customer data residency requirements. This sample shows how to override the endpoint for an API client. See https://cloud.google.com/security-command-center/docs/data-residency-support.md#regional-urls for more information on data residency
1 parent 362c9e7 commit f33ab50

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

securitycenter/snippets/noxfile_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# A dictionary you want to inject into your test. Don't put any
3333
# secrets here. These values will override predefined values.
3434
"envs": {
35+
"DRZ_SA_ORGANIZATION": "172173830708",
3536
"GCLOUD_ORGANIZATION": "1081635000895",
3637
"GCLOUD_PROJECT": "project-a-id",
3738
"GCLOUD_PUBSUB_TOPIC": "projects/project-a-id/topics/notifications-sample-topic",
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2024 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# [START securitycenter_regional_endpoint_list_findings]
18+
def rep_list_finding(parent, endpoint) -> int:
19+
"""
20+
lists all findings for a parent
21+
Args:
22+
parent: Parent resource for which findings to be listed. Must be in one of the following formats:
23+
"organizations/{organization_id}/sources/{sources}/locations/{location}"
24+
"projects/{project_id}/sources/{sources}/locations/{location}"
25+
"folders/{folder_id}/sources/{sources}/locations/{location}"
26+
endpoint: Endpoint for this request. For example "securitycenter.googleapis.com", "securitycenter.me-central2.rep.googleapis.com"
27+
Returns:
28+
int: return the count of all findings for a source
29+
"""
30+
from google.cloud import securitycenter_v2 as securitycenter
31+
from google.api_core.client_options import ClientOptions
32+
# Override endpoint and create a client.
33+
options = ClientOptions(api_endpoint=endpoint)
34+
client = securitycenter.SecurityCenterClient(client_options=options)
35+
36+
finding_result_iterator = client.list_findings(request={"parent": parent})
37+
for count, finding_result in enumerate(finding_result_iterator):
38+
print(
39+
"{}: name: {} resource: {}".format(
40+
count, finding_result.finding.name, finding_result.finding.resource_name
41+
)
42+
)
43+
return count
44+
45+
# [END securitycenter_regional_endpoint_list_findings]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2024 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
import os
17+
import regional_endpoint_snippet
18+
import pytest
19+
20+
@pytest.fixture(scope="module")
21+
def parent():
22+
return f"{(os.environ["DRZ_SA_ORGANIZATION"])}/sources/-/locations/sa"
23+
24+
def endpoint():
25+
return "securitycenter.me-central2.rep.googleapis.com"
26+
27+
def test_rep_list_finding():
28+
count = regional_endpoint_snippet.rep_list_finding(parent, endpoint)
29+
assert count > 0

0 commit comments

Comments
 (0)