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

docs(samples): Added sample for creating Secret with UserManaged replication #328

Merged
merged 9 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions samples/snippets/create_secret_with_user_managed_replication.py
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 samples/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