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
# Create Secret Request
response = client.create_secret(
request={
"parent": parent, # REQUIRED: str
"secret_id": secret_id, # REQUIRED: str
"secret": { # REQUIRED: google.cloud.secretmanager_v1.types.Secret
"replication": { # OPTIONAL: google.cloud.secretmanager_v1.types.Replication
"automatic": {}, # OPTIONAL: google.cloud.secretmanager_v1.types.Replication.Automatic
"user_managed": { # OPTIONAL: and only if automatic is not specified: google.cloud.secretmanager_v1.types.Replication.UserManaged
"replication": { # REQUIRED: with user_managed: MutableSequence[google.cloud.secretmanager_v1.types.Replication.UserManaged.Replica]
"location": location, # REQUIRED: str
"customer_managed_encryption": { # OPTIONAL: google.cloud.secretmanager_v1.types.CustomerManagedEncryption
"kms_key_name": kms_key_name # REQUIRED: The expected format is projects/*/locations/*/keyRings/*/cryptoKeys/*
}
}
},
},
"labels": { # OPTIONAL: MutableMapping[str, str]
"keyname": "valuename"
},
"topics": [
"topic-name"
], # OPTIONAL: MutableSequence[google.cloud.secretmanager_v1.types.Topic]
"expire_time": expire_time, # OPTIONAL: google.protobuf.timestamp_pb2.Timestamp
"ttl": ttl, # OPTIONAL: google.protobuf.duration_pb2.Duration
.. and all the other properties ..
},
}
)
# Response
# instance of google.cloud.secretmanager_v1.types.resources.Secret
{
name: "name", # REQUIRED: string
create_time: "create_time", # REQUIRED: google.protobuf.timestamp_pb2.Timestamp
... 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
response = client.create_secret(
request={
"parent": 'string',
"secret_id": 'string',
... all the other properties ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment