Skip to content

Commit 662f8d5

Browse files
feat:added samples for labels, regional SM (#12687)
* feat:added samples for labels, regional SM * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: updated function description * fix: updated function description * fix: resolved comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 1dc6ff0 commit 662f8d5

File tree

7 files changed

+394
-3
lines changed

7 files changed

+394
-3
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
# 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+
command line application and sample code for creating a new secret with
17+
labels.
18+
"""
19+
20+
# [START secretmanager_create_regional_secret_with_label]
21+
import argparse
22+
import typing
23+
24+
# Import the Secret Manager client library.
25+
from google.cloud import secretmanager_v1
26+
27+
28+
def create_regional_secret_with_labels(
29+
project_id: str,
30+
location_id: str,
31+
secret_id: str,
32+
labels: typing.Dict[str, str],
33+
ttl: typing.Optional[str] = None,
34+
) -> secretmanager_v1.Secret:
35+
"""
36+
Create a new secret with the given name. A secret is a logical wrapper
37+
around a collection of secret versions. Secret versions hold the actual
38+
secret material.
39+
"""
40+
41+
# Endpoint to call the regional Secret Manager API.
42+
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com"
43+
44+
# Create the Secret Manager client.
45+
client = secretmanager_v1.SecretManagerServiceClient(
46+
client_options={"api_endpoint": api_endpoint},
47+
)
48+
49+
# Build the resource name of the parent secret.
50+
parent = f"projects/{project_id}/locations/{location_id}"
51+
52+
# Create the secret.
53+
response = client.create_secret(
54+
request={
55+
"parent": parent,
56+
"secret_id": secret_id,
57+
"secret": {"ttl": ttl, "labels": labels},
58+
}
59+
)
60+
61+
# Print the new secret name.
62+
print(f"Created secret: {response.name}")
63+
64+
return response
65+
66+
67+
# [END secretmanager_create_regional_secret_with_label]
68+
69+
if __name__ == "__main__":
70+
parser = argparse.ArgumentParser(
71+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
72+
)
73+
parser.add_argument("project_id", help="id of the GCP project")
74+
parser.add_argument(
75+
"location_id", help="id of the location where secret is to be created"
76+
)
77+
parser.add_argument("secret_id", help="id of the secret to create")
78+
parser.add_argument("label_key", help="key of the label you want to add")
79+
parser.add_argument("label_value", help="value of the label you want to add")
80+
args = parser.parse_args()
81+
82+
labels = {args.label_key, args.label_value}
83+
create_regional_secret_with_labels(
84+
args.project_id, args.location_id, args.secret_id, labels
85+
)
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 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+
# 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_label]
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_label(
24+
project_id: str, location_id: str, secret_id: str, label_key: str
25+
) -> secretmanager_v1.UpdateSecretRequest:
26+
"""
27+
Delete a label 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+
labels = response.labels
45+
46+
# Delete the label
47+
labels.pop(label_key, None)
48+
49+
# Update the secret.
50+
secret = {"name": name, "labels": labels}
51+
update_mask = {"paths": ["labels"]}
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_label]
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("label_key", help="key of the label to be deleted")
74+
args = parser.parse_args()
75+
76+
delete_regional_secret_label(
77+
args.project_id, args.location_id, args.secret_id, args.label_key
78+
)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
# 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_edit_regional_secret_label]
17+
18+
import argparse
19+
from typing import Dict
20+
21+
# Import the Secret Manager client library.
22+
from google.cloud import secretmanager_v1
23+
24+
25+
def edit_regional_secret_label(
26+
project_id: str, location_id: str, secret_id: str, new_labels: Dict[str, str]
27+
) -> secretmanager_v1.UpdateSecretRequest:
28+
"""
29+
Create or update a label on an existing secret.
30+
"""
31+
32+
# Endpoint to call the regional Secret Manager API.
33+
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com"
34+
35+
# Create the Secret Manager client.
36+
client = secretmanager_v1.SecretManagerServiceClient(
37+
client_options={"api_endpoint": api_endpoint},
38+
)
39+
40+
# Build the resource name of the parent secret.
41+
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}"
42+
43+
# Get the secret.
44+
response = client.get_secret(request={"name": name})
45+
46+
labels = response.labels
47+
48+
# Update the labels
49+
for label_key in new_labels:
50+
labels[label_key] = new_labels[label_key]
51+
52+
# Update the secret.
53+
secret = {"name": name, "labels": labels}
54+
update_mask = {"paths": ["labels"]}
55+
response = client.update_secret(
56+
request={"secret": secret, "update_mask": update_mask}
57+
)
58+
59+
# Print the new secret name.
60+
print(f"Updated secret: {response.name}")
61+
62+
return response
63+
64+
65+
# [END secretmanager_edit_regional_secret_label]
66+
67+
if __name__ == "__main__":
68+
parser = argparse.ArgumentParser(
69+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
70+
)
71+
parser.add_argument("project_id", help="id of the GCP project")
72+
parser.add_argument(
73+
"location_id", help="id of the location where secret is to be created"
74+
)
75+
parser.add_argument("secret_id", help="id of the secret to act on")
76+
parser.add_argument("label_key", help="key of the label to be added/updated")
77+
parser.add_argument("label_value", help="value of the label to be added/updated")
78+
args = parser.parse_args()
79+
80+
labels = {args.label_key, args.label_value}
81+
edit_regional_secret_label(
82+
args.project_id, args.location_id, args.secret_id, labels
83+
)

secretmanager/snippets/regional_samples/get_regional_secret.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def get_regional_secret(
3131
the secret container, not any secret material.
3232
"""
3333

34-
# Endpoint to call the regional secret manager sever
34+
# Endpoint to call the regional Secret Manager API
3535
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com"
3636

3737
# Create the Secret Manager client.

secretmanager/snippets/regional_samples/snippets_test.py

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
from regional_samples import access_regional_secret_version
2424
from regional_samples import add_regional_secret_version
2525
from regional_samples import create_regional_secret
26+
from regional_samples import create_regional_secret_with_labels
2627
from regional_samples import delete_regional_secret
28+
from regional_samples import delete_regional_secret_label
2729
from regional_samples import delete_regional_secret_with_etag
2830
from regional_samples import destroy_regional_secret_version
2931
from regional_samples import destroy_regional_secret_version_with_etag
3032
from regional_samples import disable_regional_secret_version
3133
from regional_samples import disable_regional_secret_version_with_etag
34+
from regional_samples import edit_regional_secret_label
3235
from regional_samples import enable_regional_secret_version
3336
from regional_samples import enable_regional_secret_version_with_etag
3437
from regional_samples import get_regional_secret
@@ -42,13 +45,24 @@
4245
from regional_samples import regional_quickstart
4346
from regional_samples import update_regional_secret
4447
from regional_samples import update_regional_secret_with_etag
48+
from regional_samples import view_regional_secret_labels
4549

4650

4751
@pytest.fixture()
4852
def location_id() -> str:
4953
return "us-east5"
5054

5155

56+
@pytest.fixture()
57+
def label_key() -> str:
58+
return "googlecloud"
59+
60+
61+
@pytest.fixture()
62+
def label_value() -> str:
63+
return "rocks"
64+
65+
5266
@pytest.fixture()
5367
def regional_client(location_id: str) -> secretmanager_v1.SecretManagerServiceClient:
5468
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com"
@@ -148,6 +162,8 @@ def regional_secret(
148162
project_id: str,
149163
location_id: str,
150164
secret_id: str,
165+
label_key: str,
166+
label_value: str,
151167
ttl: str,
152168
) -> Iterator[Tuple[str, str]]:
153169
print(f"creating secret {secret_id}")
@@ -159,7 +175,10 @@ def regional_secret(
159175
request={
160176
"parent": parent,
161177
"secret_id": secret_id,
162-
"secret": {"ttl": ttl},
178+
"secret": {
179+
"ttl": ttl,
180+
"labels": {label_key: label_value},
181+
},
163182
},
164183
)
165184

@@ -207,6 +226,40 @@ def test_create_regional_secret(
207226
assert secret_id in secret.name
208227

209228

229+
def test_create_regional_secret_with_label(
230+
regional_client: secretmanager_v1.SecretManagerServiceClient,
231+
project_id: str,
232+
location_id: str,
233+
secret_id: str,
234+
label_key: str,
235+
label_value: str,
236+
ttl: str,
237+
) -> None:
238+
labels = {label_key: label_value}
239+
secret = create_regional_secret_with_labels.create_regional_secret_with_labels(
240+
project_id, location_id, secret_id, labels, ttl
241+
)
242+
assert secret_id in secret.name
243+
244+
245+
def test_delete_regional_secret_labels(
246+
regional_client: secretmanager_v1.SecretManagerServiceClient,
247+
project_id: str,
248+
location_id: str,
249+
regional_secret: Tuple[str, str],
250+
label_key: str,
251+
) -> None:
252+
secret_id, _ = regional_secret
253+
delete_regional_secret_label.delete_regional_secret_label(
254+
project_id, location_id, secret_id, label_key
255+
)
256+
with pytest.raises(exceptions.NotFound):
257+
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}/versions/latest"
258+
retry_client_access_regional_secret_version(
259+
regional_client, request={"name": name}
260+
)
261+
262+
210263
def test_delete_regional_secret_with_etag(
211264
regional_client: secretmanager_v1.SecretManagerServiceClient,
212265
regional_secret: Tuple[str, str],
@@ -457,6 +510,18 @@ def test_get_regional_secret(
457510
assert secret_id in snippet_regional_secret.name
458511

459512

513+
def test_edit_regional_secret_label(
514+
project_id: str, location_id: str, regional_secret: Tuple[str, str], label_key: str
515+
) -> None:
516+
secret_id, _ = regional_secret
517+
updated_label_value = "updatedvalue"
518+
labels = {label_key: updated_label_value}
519+
updated_secret = edit_regional_secret_label.edit_regional_secret_label(
520+
project_id, location_id, secret_id, labels
521+
)
522+
assert updated_secret.labels[label_key] == updated_label_value
523+
524+
460525
def test_update_regional_secret_with_etag(
461526
regional_secret: Tuple[str, str],
462527
project_id: str,
@@ -481,3 +546,19 @@ def test_update_regional_secret(
481546
project_id, location_id, secret_id
482547
)
483548
assert updated_regional_secret.labels["secretmanager"] == "rocks"
549+
550+
551+
def test_view_regional_secret_labels(
552+
capsys: pytest.LogCaptureFixture,
553+
project_id: str,
554+
location_id: str,
555+
regional_secret: Tuple[str, str],
556+
label_key: str,
557+
) -> None:
558+
secret_id, _ = regional_secret
559+
view_regional_secret_labels.view_regional_secret_labels(
560+
project_id, location_id, secret_id
561+
)
562+
563+
out, _ = capsys.readouterr()
564+
assert label_key in out

0 commit comments

Comments
 (0)