-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): Added sample for creating Secret with UserManaged repl…
…ication (#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>
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
secretmanager/snippets/create_secret_with_user_managed_replication.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 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 | ||
""" | ||
command line application and sample code for creating a new secret with | ||
user managed replication. | ||
""" | ||
|
||
import argparse | ||
|
||
|
||
def create_ummr_secret(project_id, secret_id, locations): | ||
""" | ||
Create a new secret with the given name. A secret is a logical wrapper | ||
around a collection of secret versions. Secret versions hold the actual | ||
secret material. | ||
""" | ||
|
||
# Import the Secret Manager client library. | ||
from google.cloud import secretmanager | ||
|
||
# Create the Secret Manager client. | ||
client = secretmanager.SecretManagerServiceClient() | ||
|
||
# Build the resource name of the parent project. | ||
parent = f"projects/{project_id}" | ||
|
||
# Create the secret. | ||
response = client.create_secret( | ||
request={ | ||
"parent": parent, | ||
"secret_id": secret_id, | ||
"secret": { | ||
"replication": { | ||
"user_managed": {"replicas": [{"location": x} for x in locations]} | ||
} | ||
}, | ||
} | ||
) | ||
|
||
# Print the new secret name. | ||
print("Created secret: {}".format(response.name)) | ||
|
||
return response | ||
|
||
|
||
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 create") | ||
parser.add_argument( | ||
"--locations", nargs="+", help="list of locations for secret replication" | ||
) | ||
args = parser.parse_args() | ||
|
||
create_ummr_secret(args.project_id, args.secret_id, args.locations) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters