This repository was archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
docs(samples): Added sample for creating Secret with UserManaged replication #328
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
df804b5
added snippet and test
kapishps ac4f3b4
Merge branch 'main' into secrets-ummr-sample
kapishps e1ab81e
updated copyright
kapishps d0eb02a
Merge branch 'main' into secrets-ummr-sample
parthea a8697d3
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] c4bec04
Merge branch 'main' into secrets-ummr-sample
parthea 25dfdf1
pr comment changes
kapishps b61b5bd
Merge branch 'main' into secrets-ummr-sample
kapishps 9b14553
Merge branch 'main' into secrets-ummr-sample
kapishps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
samples/snippets/create_secret_with_user_managed_replication.py
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.