Skip to content

Commit 04304c8

Browse files
OremGLGiennaetelpirion
authored
chore(secretmanager): add optional argument ttl to createSecret (#4894)
* chore(secretmanager): add optional argument ttl to createSecret and testing it with/without ttl * chore(secretmanager): add optional argument ttl to createSecret and testing it with/without ttl * chore(secretmanager): add function/test for secret with ttl * chore(secretmanager): add function/test for secret with ttl --------- Co-authored-by: Jennifer Davis <[email protected]> Co-authored-by: Eric Schmidt <[email protected]>
1 parent 6e73cbe commit 04304c8

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

secretmanager/create_secret_ttl.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package secretmanager
16+
17+
// [START secretmanager_create_secret_with_ttl]
18+
import (
19+
"context"
20+
"fmt"
21+
"io"
22+
"time"
23+
24+
secretmanager "cloud.google.com/go/secretmanager/apiv1"
25+
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
26+
"google.golang.org/protobuf/types/known/durationpb"
27+
)
28+
29+
// createSecretWithTTL creates a new secret with the given name and ttl.
30+
func createSecretWithTTL(w io.Writer, parent, id string, d time.Duration) error {
31+
// parent := "projects/my-project"
32+
// id := "my-secret"
33+
34+
expiration := &secretmanagerpb.Secret_Ttl{Ttl: durationpb.New(d)}
35+
36+
// Create the client.
37+
ctx := context.Background()
38+
client, err := secretmanager.NewClient(ctx)
39+
if err != nil {
40+
return fmt.Errorf("failed to create secretmanager client: %w", err)
41+
}
42+
defer client.Close()
43+
44+
// Build the request.
45+
req := &secretmanagerpb.CreateSecretRequest{
46+
Parent: parent,
47+
SecretId: id,
48+
Secret: &secretmanagerpb.Secret{
49+
Replication: &secretmanagerpb.Replication{
50+
Replication: &secretmanagerpb.Replication_Automatic_{
51+
Automatic: &secretmanagerpb.Replication_Automatic{},
52+
},
53+
},
54+
Expiration: expiration,
55+
},
56+
}
57+
58+
// Call the API.
59+
result, err := client.CreateSecret(ctx, req)
60+
if err != nil {
61+
return fmt.Errorf("failed to create secret: %w", err)
62+
}
63+
fmt.Fprintf(w, "Created secret with ttl: %s\n", result.Name)
64+
return nil
65+
}
66+
67+
// [END secretmanager_create_secret_with_ttl]

secretmanager/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
google.golang.org/api v0.195.0
1010
google.golang.org/genproto v0.0.0-20240827150818-7e3bb234dfed
1111
google.golang.org/grpc v1.66.0
12+
google.golang.org/protobuf v1.34.2
1213
)
1314

1415
require (
@@ -41,5 +42,4 @@ require (
4142
golang.org/x/time v0.6.0 // indirect
4243
google.golang.org/genproto/googleapis/api v0.0.0-20240827150818-7e3bb234dfed // indirect
4344
google.golang.org/genproto/googleapis/rpc v0.0.0-20240827150818-7e3bb234dfed // indirect
44-
google.golang.org/protobuf v1.34.2 // indirect
4545
)

secretmanager/secretmanager_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"reflect"
2323
"strings"
24+
"time"
2425

2526
"testing"
2627

@@ -321,6 +322,26 @@ func TestCreateSecret(t *testing.T) {
321322
}
322323
}
323324

325+
func TestCreateSecretWithTTL(t *testing.T) {
326+
tc := testutil.SystemTest(t)
327+
328+
secretID := "createSecretTTL"
329+
330+
parent := fmt.Sprintf("projects/%s", tc.ProjectID)
331+
332+
duration := time.Second * 70
333+
334+
var b bytes.Buffer
335+
if err := createSecretWithTTL(&b, parent, secretID, duration); err != nil {
336+
t.Fatal(err)
337+
}
338+
defer testCleanupSecret(t, fmt.Sprintf("projects/%s/secrets/%s", tc.ProjectID, secretID))
339+
340+
if got, want := b.String(), "Created secret with ttl:"; !strings.Contains(got, want) {
341+
t.Errorf("createSecretWithTTL: expected %q to contain %q", got, want)
342+
}
343+
}
344+
324345
func TestCreateSecretWithLabels(t *testing.T) {
325346
tc := testutil.SystemTest(t)
326347

0 commit comments

Comments
 (0)