-
Notifications
You must be signed in to change notification settings - Fork 6.6k
chore(secretmanager): Add samples for deleting secret annotations and updating annotation and label args #13511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
durgesh-ninave-crest
wants to merge
8
commits into
GoogleCloudPlatform:main
Choose a base branch
from
durgesh-ninave-crest:secretmanager-label-and-annotation-samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−8
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0943419
feat(secretmanager): Added samples for labels and annotations
durgesh-ninave-crest 7510de8
fix(secretmanager): Update arguments labels and annotations
durgesh-ninave-crest 61486ed
fix(secretmanager): address gemini review comments
durgesh-ninave-crest a129270
fix(secretmanager): update samples and testcases
durgesh-ninave-crest ab08321
Merge branch 'main' into secretmanager-label-and-annotation-samples
durgesh-ninave-crest 3251152
chore(secretmanager): added assertions in test cases
durgesh-ninave-crest 0aac15f
Merge branch 'main' into secretmanager-label-and-annotation-samples
durgesh-ninave-crest 0703bef
chore(secretmanager): update test assertions
durgesh-ninave-crest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2025 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
|
||
# [START secretmanager_delete_secret_annotation] | ||
import argparse | ||
|
||
# Import the Secret Manager client library. | ||
from google.cloud import secretmanager | ||
|
||
|
||
def delete_secret_annotation( | ||
project_id: str, secret_id: str, annotation_key: str | ||
) -> secretmanager.Secret: | ||
""" | ||
Delete a annotation on an existing secret. | ||
""" | ||
|
||
# Create the Secret Manager client. | ||
client = secretmanager.SecretManagerServiceClient() | ||
|
||
# Build the resource name of the secret. | ||
name = client.secret_path(project_id, secret_id) | ||
|
||
# Get the secret. | ||
response = client.get_secret(request={"name": name}) | ||
|
||
annotations = response.annotations | ||
|
||
# Delete the annotation | ||
annotations.pop(annotation_key, None) | ||
|
||
# Update the secret. | ||
secret = {"name": name, "annotations": annotations} | ||
update_mask = {"paths": ["annotations"]} | ||
response = client.update_secret( | ||
request={"secret": secret, "update_mask": update_mask} | ||
) | ||
|
||
# Print the new secret name. | ||
print(f"Updated secret: {response.name}") | ||
|
||
return response | ||
|
||
|
||
# [END secretmanager_delete_secret_annotation] | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
parser.add_argument("project_id", help="id of the GCP project") | ||
parser.add_argument("secret_id", help="id of the secret to act on") | ||
parser.add_argument("annotation_key", help="key of the annotation to be deleted") | ||
args = parser.parse_args() | ||
|
||
delete_secret_annotation(args.project_id, args.secret_id, args.annotation_key) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
secretmanager/snippets/regional_samples/delete_regional_secret_annotation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2025 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
|
||
# [START secretmanager_delete_regional_secret_annotation] | ||
import argparse | ||
|
||
# Import the Secret Manager client library. | ||
from google.cloud import secretmanager_v1 | ||
|
||
|
||
def delete_regional_secret_annotation( | ||
project_id: str, location_id: str, secret_id: str, annotation_key: str | ||
) -> secretmanager_v1.Secret: | ||
""" | ||
Delete a annotation on an existing secret. | ||
""" | ||
|
||
# Endpoint to call the regional Secret Manager API. | ||
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com" | ||
|
||
# Create the Secret Manager client. | ||
client = secretmanager_v1.SecretManagerServiceClient( | ||
client_options={"api_endpoint": api_endpoint}, | ||
) | ||
|
||
# Build the resource name of the parent secret. | ||
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}" | ||
durgesh-ninave-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Get the secret. | ||
response = client.get_secret(request={"name": name}) | ||
|
||
annotations = response.annotations | ||
|
||
# Delete the annotation | ||
annotations.pop(annotation_key, None) | ||
|
||
# Update the secret. | ||
secret = {"name": name, "annotations": annotations} | ||
update_mask = {"paths": ["annotations"]} | ||
response = client.update_secret( | ||
request={"secret": secret, "update_mask": update_mask} | ||
) | ||
|
||
# Print the new secret name. | ||
print(f"Updated secret: {response.name}") | ||
|
||
return response | ||
|
||
|
||
# [END secretmanager_delete_regional_secret_annotation] | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
parser.add_argument("project_id", help="id of the GCP project") | ||
parser.add_argument( | ||
"location_id", help="id of the location where secret is to be created" | ||
) | ||
parser.add_argument("secret_id", help="id of the secret to act on") | ||
parser.add_argument("annotation_key", help="key of the annotation to be deleted") | ||
args = parser.parse_args() | ||
|
||
delete_regional_secret_annotation( | ||
args.project_id, args.location_id, args.secret_id, args.annotation_key | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.