-
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
base: main
Are you sure you want to change the base?
Changes from all commits
0943419
7510de8
61486ed
a129270
ab08321
3251152
0aac15f
0703bef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) |
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 | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
from regional_samples import create_regional_secret_with_labels | ||
from regional_samples import create_regional_secret_with_tags | ||
from regional_samples import delete_regional_secret | ||
from regional_samples import delete_regional_secret_annotation | ||
from regional_samples import delete_regional_secret_label | ||
from regional_samples import delete_regional_secret_with_etag | ||
from regional_samples import destroy_regional_secret_version | ||
|
@@ -469,6 +470,20 @@ def test_create_regional_secret_with_label( | |
assert secret_id in secret.name | ||
|
||
|
||
def test_delete_regional_secret_annotation( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add some asserts in the tests There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I followed the pattern of the other delete test cases, which is why it was missing. I've added the necessary assertion to the tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assert statement doesn't verify the functionality of the sample, we should assert to verify the operation performed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've updated the assertion to reflect the intended functionality of the sample. |
||
project_id: str, | ||
location_id: str, | ||
regional_secret: Tuple[str, str], | ||
annotation_key: str, | ||
) -> None: | ||
secret_id, _ = regional_secret | ||
secret = delete_regional_secret_annotation.delete_regional_secret_annotation( | ||
project_id, location_id, secret_id, annotation_key | ||
) | ||
assert secret_id in secret.name | ||
assert annotation_key not in secret.annotations | ||
|
||
|
||
def test_delete_regional_secret_labels( | ||
regional_client: secretmanager_v1.SecretManagerServiceClient, | ||
project_id: str, | ||
|
Uh oh!
There was an error while loading. Please reload this page.