Skip to content

Commit 6a7535d

Browse files
docs(samples): add sample to update secret with alias (#307)
Update code samples for Access By alias Launch Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-secret-manager/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> 🦕
1 parent 0822602 commit 6a7535d

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

secretmanager/snippets/snippets_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from list_secrets_with_filter import list_secrets_with_filter
4242
from quickstart import quickstart
4343
from update_secret import update_secret
44+
from update_secret_with_alias import update_secret_with_alias
4445
from update_secret_with_etag import update_secret_with_etag
4546

4647

@@ -290,3 +291,9 @@ def test_update_secret_with_etag(secret):
290291
project_id, secret_id, etag = secret
291292
secret = update_secret_with_etag(project_id, secret_id, etag)
292293
assert secret.labels["secretmanager"] == "rocks"
294+
295+
296+
def test_update_secret_with_alias(secret_version):
297+
project_id, secret_id, version_id, _ = secret_version
298+
secret = update_secret_with_alias(project_id, secret_id)
299+
assert secret.version_aliases["test"] == 1
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 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+
import argparse
17+
18+
19+
# [START secretmanager_update_secret_with_alias]
20+
def update_secret_with_alias(project_id, secret_id):
21+
"""
22+
Update the metadata about an existing secret.
23+
"""
24+
25+
# Import the Secret Manager client library.
26+
from google.cloud import secretmanager
27+
28+
# Create the Secret Manager client.
29+
client = secretmanager.SecretManagerServiceClient()
30+
31+
# Build the resource name of the secret.
32+
name = client.secret_path(project_id, secret_id)
33+
34+
# Update the secret.
35+
secret = {"name": name, "version_aliases": {"test": 1}}
36+
update_mask = {"paths": ["version_aliases"]}
37+
response = client.update_secret(
38+
request={"secret": secret, "update_mask": update_mask}
39+
)
40+
41+
# Print the new secret name.
42+
print("Updated secret: {}".format(response.name))
43+
# [END secretmanager_update_secret_with_alias]
44+
45+
return response
46+
47+
48+
if __name__ == "__main__":
49+
parser = argparse.ArgumentParser(
50+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
51+
)
52+
parser.add_argument("project_id", help="id of the GCP project")
53+
parser.add_argument("--secret-id", required=True)
54+
args = parser.parse_args()
55+
56+
update_secret_with_alias(args.project_id, args.secret_id)

0 commit comments

Comments
 (0)