Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit c5fe7ff

Browse files
kapishpspartheagcf-owl-bot[bot]
authored
docs(samples): Added sample for creating Secret with UserManaged replication (#328)
* added snippet and test * updated copyright * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * pr comment changes Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 74a6c2a commit c5fe7ff

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
command line application and sample code for creating a new secret with
17+
user managed replication.
18+
"""
19+
20+
import argparse
21+
22+
23+
def create_ummr_secret(project_id, secret_id, locations):
24+
"""
25+
Create a new secret with the given name. A secret is a logical wrapper
26+
around a collection of secret versions. Secret versions hold the actual
27+
secret material.
28+
"""
29+
30+
# Import the Secret Manager client library.
31+
from google.cloud import secretmanager
32+
33+
# Create the Secret Manager client.
34+
client = secretmanager.SecretManagerServiceClient()
35+
36+
# Build the resource name of the parent project.
37+
parent = f"projects/{project_id}"
38+
39+
# Create the secret.
40+
response = client.create_secret(
41+
request={
42+
"parent": parent,
43+
"secret_id": secret_id,
44+
"secret": {
45+
"replication": {
46+
"user_managed": {"replicas": [{"location": x} for x in locations]}
47+
}
48+
},
49+
}
50+
)
51+
52+
# Print the new secret name.
53+
print("Created secret: {}".format(response.name))
54+
55+
return response
56+
57+
58+
if __name__ == "__main__":
59+
parser = argparse.ArgumentParser(
60+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
61+
)
62+
parser.add_argument("project_id", help="id of the GCP project")
63+
parser.add_argument("secret_id", help="id of the secret to create")
64+
parser.add_argument(
65+
"--locations", nargs="+", help="list of locations for secret replication"
66+
)
67+
args = parser.parse_args()
68+
69+
create_ummr_secret(args.project_id, args.secret_id, args.locations)

samples/snippets/snippets_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from add_secret_version import add_secret_version
2424
from consume_event_notification import consume_event_notification
2525
from create_secret import create_secret
26+
from create_secret_with_user_managed_replication import create_ummr_secret
2627
from delete_secret import delete_secret
2728
from delete_secret_with_etag import delete_secret_with_etag
2829
from destroy_secret_version import destroy_secret_version
@@ -145,6 +146,12 @@ def test_create_secret(client, project_id, secret_id):
145146
assert secret_id in secret.name
146147

147148

149+
def test_create_secret_with_user_managed_replication(client, project_id, secret_id):
150+
locations = ["us-east1", "us-east4", "us-west1"]
151+
secret = create_ummr_secret(project_id, secret_id, locations)
152+
assert secret_id in secret.name
153+
154+
148155
def test_delete_secret(client, secret):
149156
project_id, secret_id, _ = secret
150157
delete_secret(project_id, secret_id)

0 commit comments

Comments
 (0)