Skip to content

Instantly share code, notes, and snippets.

@henriquez
Last active May 1, 2024 21:58
Show Gist options
  • Save henriquez/2f91fcc9a8417a8e76c1edf54e1d8002 to your computer and use it in GitHub Desktop.
Save henriquez/2f91fcc9a8417a8e76c1edf54e1d8002 to your computer and use it in GitHub Desktop.
Secret Manager create secret function examples
# Create secret in secret manager: proposed generated samples
# These samples assume that detail about the arguments is provided on the same page below the sample. In the case of
# protobuf types - since they are not described in the SDK reference docs, we'd create new doc content describing how to
# convert from native types to protobuf types.
# EX 1: In this example we put the types as comments, see below for other options. In languages where type information is
# built into the request syntax, comments don't need to include type information, but may still need required/optional if its
# not part of the type system.
# Create Secret Request
response = client.create_secret(
request={
# REQUIRED: str
"parent": parent,
# REQUIRED: str
"secret_id": secret_id,
# REQUIRED: google.cloud.secretmanager_v1.types.Secret
"secret": {
# OPTIONAL: google.cloud.secretmanager_v1.types.Replication
"replication": {
# OPTIONAL: google.cloud.secretmanager_v1.types.Replication.Automatic
"automatic": {},
# OPTIONAL: and only if automatic is not specified: google.cloud.secretmanager_v1.types.Replication.UserManaged
"user_managed": {
# REQUIRED: with user_managed: MutableSequence[google.cloud.secretmanager_v1.types.Replication.UserManaged.Replication
"replicas": {
# REQUIRED: str
"location": location,
# OPTIONAL: google.cloud.secretmanager_v1.types.CustomerManagedEncryption
"customer_managed_encryption": {
# REQUIRED: The expected format is projects/*/locations/*/keyRings/*/cryptoKeys/*
"kms_key_name": kms_key_name
}
}
},
},
# OPTIONAL: MutableMapping[str, str]
"labels": {
# OPTIONAL: str: str
"keyname": "valuename".
},
# OPTIONAL: MutableSequence[google.cloud.secretmanager_v1.types.Topic]
"topics": [
# OPTIONAL: str
"topic-name"
],
# OPTIONAL: google.protobuf.timestamp_pb2.Timestamp
"expire_time": expire_time,
# OPTIONAL: google.protobuf.duration_pb2.Duration
"ttl": ttl,
.. and all the other properties ..
}
)
# Response
# instance of google.cloud.secretmanager_v1.types.resources.Secret
{
# REQUIRED: str
name: "name",
# REQUIRED: google.protobuf.timestamp_pb2.Timestamp
create_time: "create_time",
... list all the other properties, both optional and required that might occur in a response ...
}
# Errors
try:
...
except WhateverTheNameOfTheExceptionIs as e:
print(e.response)
# Error Syntax
{
'Message': 'str',
'Error': {
'Code': 'str',
'Message': 'str'
}
}
# EX 2: In this example, we only put type information into values. Object types are explained in the attributes/parameters
# section that appears below the sample similar to
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/secretsmanager/client/create_secret.html
# otherwise this sample would be the same as the one above, including responses and errors. In this case required/optional
# information would be put into the parameters section below the sample.
response = client.create_secret(
request={
"parent": 'string',
"secret_id": 'string',
"ttl": '{google.protobuf.duration_pb2.Duration}'
... all the other properties ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment