Skip to content

Commit

Permalink
docs(samples): Added sample for creating Secret with UserManaged repl…
Browse files Browse the repository at this point in the history
…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 <partheniou@google.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 24, 2022
1 parent d7b904f commit de82074
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
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)
7 changes: 7 additions & 0 deletions secretmanager/snippets/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from add_secret_version import add_secret_version
from consume_event_notification import consume_event_notification
from create_secret import create_secret
from create_secret_with_user_managed_replication import create_ummr_secret
from delete_secret import delete_secret
from delete_secret_with_etag import delete_secret_with_etag
from destroy_secret_version import destroy_secret_version
Expand Down Expand Up @@ -145,6 +146,12 @@ def test_create_secret(client, project_id, secret_id):
assert secret_id in secret.name


def test_create_secret_with_user_managed_replication(client, project_id, secret_id):
locations = ["us-east1", "us-east4", "us-west1"]
secret = create_ummr_secret(project_id, secret_id, locations)
assert secret_id in secret.name


def test_delete_secret(client, secret):
project_id, secret_id, _ = secret
delete_secret(project_id, secret_id)
Expand Down

0 comments on commit de82074

Please sign in to comment.