Skip to content

Commit

Permalink
docs: Create set_undefined_finding sample (GoogleCloudPlatform#12159)
Browse files Browse the repository at this point in the history
* docs: Update snippets_mute_config.py

Update mute_config snippet and add set_mute_undefined example.

* Update snippets_mute_config_test.py

* Update snippets_mute_config.py

* fix: test string match

---------

Co-authored-by: Tony Pujals <[email protected]>
  • Loading branch information
hannahtsai2024 and subfuzion authored Aug 16, 2024
1 parent de4ad98 commit b99d37e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
29 changes: 29 additions & 0 deletions securitycenter/snippets/snippets_mute_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,35 @@ def set_unmute_finding(finding_path: str) -> None:
# [END securitycenter_set_unmute]


# [START securitycenter_set_mute_undefined]
def set_undefined_finding(finding_path: str) -> None:
"""
Reset mute state of an individual finding.
Resetting a finding that isn't muted or unmuted has no effect.
Various mute states are: UNDEFINED/MUTE/UNMUTE.
Args:
finding_path: The relative resource name of the finding. See:
https://cloud.google.com/apis/design/resource_names#relative_resource_name
Use any one of the following formats:
- organizations/{organization_id}/sources/{source_id}/finding/{finding_id},
- folders/{folder_id}/sources/{source_id}/finding/{finding_id},
- projects/{project_id}/sources/{source_id}/finding/{finding_id}.
"""
from google.cloud import securitycenter

client = securitycenter.SecurityCenterClient()

request = securitycenter.SetMuteRequest()
request.name = finding_path
request.mute = securitycenter.Finding.Mute.UNDEFINED

finding = client.set_mute(request)
print(f"Reset mute value for the finding: {finding.mute.name}")


# [END securitycenter_set_mute_undefined]


# [START securitycenter_bulk_mute]
def bulk_mute_findings(parent_path: str, mute_rule: str) -> None:
"""
Expand Down
10 changes: 10 additions & 0 deletions securitycenter/snippets/snippets_mute_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ def test_set_unmute_finding(capsys: CaptureFixture, finding):
assert re.search("Mute value for the finding: UNMUTED", out)


@backoff.on_exception(
backoff.expo, (InternalServerError, ServiceUnavailable, NotFound), max_tries=3
)
def test_set_undefined_finding(capsys: CaptureFixture, finding):
finding_path = finding.get("finding1")
snippets_mute_config.set_undefined_finding(finding_path)
out, _ = capsys.readouterr()
assert re.search("Reset mute value for the finding: UNDEFINED", out)


@backoff.on_exception(
backoff.expo, (InternalServerError, ServiceUnavailable, NotFound), max_tries=3
)
Expand Down

0 comments on commit b99d37e

Please sign in to comment.