secretmanager: update quickstart to match other languages
@@ -23,7 +23,6 @@ import (
"log"
secretmanager "cloud.google.com/go/secretmanager/apiv1beta1"
- "google.golang.org/api/iterator"
secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1beta1"
)
@@ -35,28 +34,60 @@ func main() {
ctx := context.Background()
client, err := secretmanager.NewClient(ctx)
if err != nil {
- log.Fatal(err)
+ log.Fatalf("failed to setup client: %v", err)
}
- // List all secrets in the given project
- req := &secretmanagerpb.ListSecretsRequest{
+ // Create the request to create the secret.
+ createSecretReq := &secretmanagerpb.CreateSecretRequest{
Parent: fmt.Sprintf("projects/%s", projectID),
+ SecretId: "my-secret",
+ Secret: &secretmanagerpb.Secret{
+ Replication: &secretmanagerpb.Replication{
+ Replication: &secretmanagerpb.Replication_Automatic_{
+ Automatic: &secretmanagerpb.Replication_Automatic{},
+ },
+ },
+ },
}
- it := client.ListSecrets(ctx, req)
+ secret, err := client.CreateSecret(ctx, createSecretReq)
+ if err != nil {
+ log.Fatalf("failed to create secret: %v", err)
+ }
- for {
- resp, err := it.Next()
- if err == iterator.Done {
- break
+ // Declare the payload to store.
+ payload := []byte("my super secret data")
+
+ // Build the request.
+ addSecretVersionReq := &secretmanagerpb.AddSecretVersionRequest{
+ Parent: secret.Name,
+ Payload: &secretmanagerpb.SecretPayload{
+ Data: payload,
+ },
}
+ // Call the API.
+ version, err := client.AddSecretVersion(ctx, addSecretVersionReq)
if err != nil {
- log.Fatalf("failed to list secrets: %v", err)
+ log.Fatalf("failed to add secret version: %v", err)
+ }
+
+ // Build the request.
+ accessRequest := &secretmanagerpb.AccessSecretVersionRequest{
+ Name: version.Name,
}
- fmt.Printf("Secret: %q\n", resp.Name)
+ // Call the API.
+ result, err := client.AccessSecretVersion(ctx, accessRequest)
+ if err != nil {
+ log.Fatalf("failed to access secret version: %v", err)
}
+
+ // Print the secret payload.
+ //
+ // WARNING: Do not print the secret in a production environment - this
+ // snippet is showing how to access the secret material.
+ log.Printf("Plaintext: %s", result.Payload.Data)
}
// [END secretmanager_quickstart]
