Skip to content

Commit 97d9b23

Browse files
chore(secretmanager): Add samples for deleting secret annotations and updating annotation and label args (#13511)
* feat(secretmanager): Added samples for labels and annotations * fix(secretmanager): Update arguments labels and annotations * fix(secretmanager): address gemini review comments * fix(secretmanager): update samples and testcases * chore(secretmanager): added assertions in test cases * chore(secretmanager): update test assertions
1 parent ccba8af commit 97d9b23

12 files changed

+180
-8
lines changed

secretmanager/snippets/create_secret_with_annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ def create_secret_with_annotations(
7474
)
7575
args = parser.parse_args()
7676

77-
annotations = {args.annotation_key, args.annotation_value}
77+
annotations = {args.annotation_key: args.annotation_value}
7878
create_secret_with_annotations(args.project_id, args.secret_id, annotations)

secretmanager/snippets/create_secret_with_labels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,5 @@ def create_secret_with_labels(
7070
parser.add_argument("label_value", help="value of the label you want to add")
7171
args = parser.parse_args()
7272

73-
labels = {args.label_key, args.label_value}
73+
labels = {args.label_key: args.label_value}
7474
create_secret_with_labels(args.project_id, args.secret_id, labels)

secretmanager/snippets/create_update_secret_label.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ def create_update_secret_label(
6969
parser.add_argument("label_value", help="value of the label to be added/updated")
7070
args = parser.parse_args()
7171

72-
labels = {args.label_key, args.label_value}
72+
labels = {args.label_key: args.label_value}
7373
create_update_secret_label(args.project_id, args.secret_id, labels)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2025 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+
# http://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+
16+
# [START secretmanager_delete_secret_annotation]
17+
import argparse
18+
19+
# Import the Secret Manager client library.
20+
from google.cloud import secretmanager
21+
22+
23+
def delete_secret_annotation(
24+
project_id: str, secret_id: str, annotation_key: str
25+
) -> secretmanager.Secret:
26+
"""
27+
Delete a annotation on an existing secret.
28+
"""
29+
30+
# Create the Secret Manager client.
31+
client = secretmanager.SecretManagerServiceClient()
32+
33+
# Build the resource name of the secret.
34+
name = client.secret_path(project_id, secret_id)
35+
36+
# Get the secret.
37+
response = client.get_secret(request={"name": name})
38+
39+
annotations = response.annotations
40+
41+
# Delete the annotation
42+
annotations.pop(annotation_key, None)
43+
44+
# Update the secret.
45+
secret = {"name": name, "annotations": annotations}
46+
update_mask = {"paths": ["annotations"]}
47+
response = client.update_secret(
48+
request={"secret": secret, "update_mask": update_mask}
49+
)
50+
51+
# Print the new secret name.
52+
print(f"Updated secret: {response.name}")
53+
54+
return response
55+
56+
57+
# [END secretmanager_delete_secret_annotation]
58+
59+
if __name__ == "__main__":
60+
parser = argparse.ArgumentParser(
61+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
62+
)
63+
parser.add_argument("project_id", help="id of the GCP project")
64+
parser.add_argument("secret_id", help="id of the secret to act on")
65+
parser.add_argument("annotation_key", help="key of the annotation to be deleted")
66+
args = parser.parse_args()
67+
68+
delete_secret_annotation(args.project_id, args.secret_id, args.annotation_key)

secretmanager/snippets/edit_secret_annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ def edit_secret_annotations(
7373
)
7474
args = parser.parse_args()
7575

76-
annotations = {args.annotation_key, args.annotation_value}
76+
annotations = {args.annotation_key: args.annotation_value}
7777
edit_secret_annotations(args.project_id, args.secret_id, annotations)

secretmanager/snippets/regional_samples/create_regional_secret_with_annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def create_regional_secret_with_annotations(
8080
)
8181
args = parser.parse_args()
8282

83-
annotations = {args.annotation_key, args.annotation_value}
83+
annotations = {args.annotation_key: args.annotation_value}
8484
create_regional_secret_with_annotations(
8585
args.project_id, args.location_id, args.secret_id, annotations
8686
)

secretmanager/snippets/regional_samples/create_regional_secret_with_labels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def create_regional_secret_with_labels(
7979
parser.add_argument("label_value", help="value of the label you want to add")
8080
args = parser.parse_args()
8181

82-
labels = {args.label_key, args.label_value}
82+
labels = {args.label_key: args.label_value}
8383
create_regional_secret_with_labels(
8484
args.project_id, args.location_id, args.secret_id, labels
8585
)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2025 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+
# http://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+
16+
# [START secretmanager_delete_regional_secret_annotation]
17+
import argparse
18+
19+
# Import the Secret Manager client library.
20+
from google.cloud import secretmanager_v1
21+
22+
23+
def delete_regional_secret_annotation(
24+
project_id: str, location_id: str, secret_id: str, annotation_key: str
25+
) -> secretmanager_v1.Secret:
26+
"""
27+
Delete a annotation on an existing secret.
28+
"""
29+
30+
# Endpoint to call the regional Secret Manager API.
31+
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com"
32+
33+
# Create the Secret Manager client.
34+
client = secretmanager_v1.SecretManagerServiceClient(
35+
client_options={"api_endpoint": api_endpoint},
36+
)
37+
38+
# Build the resource name of the parent secret.
39+
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}"
40+
41+
# Get the secret.
42+
response = client.get_secret(request={"name": name})
43+
44+
annotations = response.annotations
45+
46+
# Delete the annotation
47+
annotations.pop(annotation_key, None)
48+
49+
# Update the secret.
50+
secret = {"name": name, "annotations": annotations}
51+
update_mask = {"paths": ["annotations"]}
52+
response = client.update_secret(
53+
request={"secret": secret, "update_mask": update_mask}
54+
)
55+
56+
# Print the new secret name.
57+
print(f"Updated secret: {response.name}")
58+
59+
return response
60+
61+
62+
# [END secretmanager_delete_regional_secret_annotation]
63+
64+
if __name__ == "__main__":
65+
parser = argparse.ArgumentParser(
66+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
67+
)
68+
parser.add_argument("project_id", help="id of the GCP project")
69+
parser.add_argument(
70+
"location_id", help="id of the location where secret is to be created"
71+
)
72+
parser.add_argument("secret_id", help="id of the secret to act on")
73+
parser.add_argument("annotation_key", help="key of the annotation to be deleted")
74+
args = parser.parse_args()
75+
76+
delete_regional_secret_annotation(
77+
args.project_id, args.location_id, args.secret_id, args.annotation_key
78+
)

secretmanager/snippets/regional_samples/edit_regional_secret_annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def edit_regional_secret_annotations(
8181
)
8282
args = parser.parse_args()
8383

84-
annotations = {args.annotation_key, args.annotation_value}
84+
annotations = {args.annotation_key: args.annotation_value}
8585
edit_regional_secret_annotations(
8686
args.project_id, args.location_id, args.secret_id, annotations
8787
)

secretmanager/snippets/regional_samples/edit_regional_secret_label.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def edit_regional_secret_label(
7777
parser.add_argument("label_value", help="value of the label to be added/updated")
7878
args = parser.parse_args()
7979

80-
labels = {args.label_key, args.label_value}
80+
labels = {args.label_key: args.label_value}
8181
edit_regional_secret_label(
8282
args.project_id, args.location_id, args.secret_id, labels
8383
)

0 commit comments

Comments
 (0)