Skip to content

Commit a2f0b67

Browse files
pragya644telpirioniennae
authored
feat(secretmanager): create SM tags sample for global and regional (#5337)
* Updated go.mod and go.sum * create secret with tags * Update create_secret_with_tags.go Edits copyright year * Updated tag in go.mod and resolved comments * Added test for create secret with tags * Added tagkey and tagValue cleanup function * Added create_regional_secret_with_tags and test cases * go mod tidy * go lint issue * changling license year to 2025 --------- Co-authored-by: Eric Schmidt <[email protected]> Co-authored-by: Jennifer Davis <[email protected]>
1 parent 488f161 commit a2f0b67

File tree

6 files changed

+648
-129
lines changed

6 files changed

+648
-129
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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_tags]
18+
import (
19+
"context"
20+
"fmt"
21+
"io"
22+
23+
secretmanager "cloud.google.com/go/secretmanager/apiv1"
24+
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
25+
)
26+
27+
// createSecretWithTags creates a new secret with the given name and tags.
28+
func createSecretWithTags(w io.Writer, parent, id, tagKey, tagValue string) error {
29+
// parent := "projects/my-project"
30+
// id := "my-secret"
31+
32+
// Create the client.
33+
ctx := context.Background()
34+
client, err := secretmanager.NewClient(ctx)
35+
if err != nil {
36+
return fmt.Errorf("failed to create secretmanager client: %w", err)
37+
}
38+
defer client.Close()
39+
40+
// Build the request.
41+
req := &secretmanagerpb.CreateSecretRequest{
42+
Parent: parent,
43+
SecretId: id,
44+
Secret: &secretmanagerpb.Secret{
45+
Replication: &secretmanagerpb.Replication{
46+
Replication: &secretmanagerpb.Replication_Automatic_{
47+
Automatic: &secretmanagerpb.Replication_Automatic{},
48+
},
49+
},
50+
Tags: map[string]string{
51+
tagKey: tagValue,
52+
},
53+
},
54+
}
55+
56+
// Call the API.
57+
result, err := client.CreateSecret(ctx, req)
58+
if err != nil {
59+
return fmt.Errorf("failed to create secret: %w", err)
60+
}
61+
fmt.Fprintf(w, "Created secret with tags: %s\n", result.Name)
62+
return nil
63+
}
64+
65+
// [END secretmanager_create_secret_with_tags]

secretmanager/go.mod

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,60 @@ module github.com/GoogleCloudPlatform/golang-samples/secretmanager
33
go 1.23.0
44

55
require (
6-
cloud.google.com/go/secretmanager v1.14.3
6+
cloud.google.com/go/resourcemanager v1.10.6
7+
cloud.google.com/go/secretmanager v1.15.0
78
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20240724083556-7f760db013b7
89
github.com/gofrs/uuid v4.4.0+incompatible
9-
google.golang.org/api v0.217.0
10-
google.golang.org/genproto v0.0.0-20250115164207-1a7da9e5054f
11-
google.golang.org/grpc v1.69.4
12-
google.golang.org/protobuf v1.36.3
10+
google.golang.org/api v0.237.0
11+
google.golang.org/genproto v0.0.0-20250505200425-f936aa4a68b2
12+
google.golang.org/grpc v1.73.0
13+
google.golang.org/protobuf v1.36.6
1314
)
1415

1516
require (
16-
cel.dev/expr v0.19.1 // indirect
17-
cloud.google.com/go v0.118.0 // indirect
18-
cloud.google.com/go/auth v0.14.0 // indirect
19-
cloud.google.com/go/auth/oauth2adapt v0.2.7 // indirect
20-
cloud.google.com/go/compute/metadata v0.6.0 // indirect
21-
cloud.google.com/go/iam v1.3.1 // indirect
22-
cloud.google.com/go/monitoring v1.23.0 // indirect
23-
cloud.google.com/go/storage v1.50.0 // indirect
24-
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 // indirect
25-
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.49.0 // indirect
26-
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0 // indirect
17+
cel.dev/expr v0.23.0 // indirect
18+
cloud.google.com/go v0.121.2 // indirect
19+
cloud.google.com/go/auth v0.16.2 // indirect
20+
cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect
21+
cloud.google.com/go/compute/metadata v0.7.0 // indirect
22+
cloud.google.com/go/iam v1.5.2 // indirect
23+
cloud.google.com/go/longrunning v0.6.7 // indirect
24+
cloud.google.com/go/monitoring v1.24.2 // indirect
25+
cloud.google.com/go/storage v1.53.0 // indirect
26+
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.27.0 // indirect
27+
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.51.0 // indirect
28+
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.51.0 // indirect
2729
github.com/cespare/xxhash/v2 v2.3.0 // indirect
28-
github.com/cncf/xds/go v0.0.0-20250121191232-2f005788dc42 // indirect
29-
github.com/envoyproxy/go-control-plane/envoy v1.32.3 // indirect
30-
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
30+
github.com/cncf/xds/go v0.0.0-20250326154945-ae57f3c0d45f // indirect
31+
github.com/envoyproxy/go-control-plane/envoy v1.32.4 // indirect
32+
github.com/envoyproxy/protoc-gen-validate v1.2.1 // indirect
3133
github.com/felixge/httpsnoop v1.0.4 // indirect
34+
github.com/go-jose/go-jose/v4 v4.0.5 // indirect
3235
github.com/go-logr/logr v1.4.2 // indirect
3336
github.com/go-logr/stdr v1.2.2 // indirect
34-
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
3537
github.com/google/s2a-go v0.1.9 // indirect
3638
github.com/google/uuid v1.6.0 // indirect
37-
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
38-
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
39+
github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect
40+
github.com/googleapis/gax-go/v2 v2.14.2 // indirect
3941
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
42+
github.com/spiffe/go-spiffe/v2 v2.5.0 // indirect
43+
github.com/zeebo/errs v1.4.0 // indirect
4044
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
41-
go.opentelemetry.io/contrib/detectors/gcp v1.34.0 // indirect
42-
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0 // indirect
43-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 // indirect
44-
go.opentelemetry.io/otel v1.34.0 // indirect
45-
go.opentelemetry.io/otel/metric v1.34.0 // indirect
46-
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
47-
go.opentelemetry.io/otel/sdk/metric v1.34.0 // indirect
48-
go.opentelemetry.io/otel/trace v1.34.0 // indirect
49-
golang.org/x/crypto v0.32.0 // indirect
50-
golang.org/x/net v0.34.0 // indirect
51-
golang.org/x/oauth2 v0.25.0 // indirect
52-
golang.org/x/sync v0.10.0 // indirect
53-
golang.org/x/sys v0.29.0 // indirect
54-
golang.org/x/text v0.21.0 // indirect
55-
golang.org/x/time v0.9.0 // indirect
56-
google.golang.org/genproto/googleapis/api v0.0.0-20250115164207-1a7da9e5054f // indirect
57-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect
45+
go.opentelemetry.io/contrib/detectors/gcp v1.35.0 // indirect
46+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.61.0 // indirect
47+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.61.0 // indirect
48+
go.opentelemetry.io/otel v1.36.0 // indirect
49+
go.opentelemetry.io/otel/metric v1.36.0 // indirect
50+
go.opentelemetry.io/otel/sdk v1.36.0 // indirect
51+
go.opentelemetry.io/otel/sdk/metric v1.36.0 // indirect
52+
go.opentelemetry.io/otel/trace v1.36.0 // indirect
53+
golang.org/x/crypto v0.39.0 // indirect
54+
golang.org/x/net v0.41.0 // indirect
55+
golang.org/x/oauth2 v0.30.0 // indirect
56+
golang.org/x/sync v0.15.0 // indirect
57+
golang.org/x/sys v0.33.0 // indirect
58+
golang.org/x/text v0.26.0 // indirect
59+
golang.org/x/time v0.12.0 // indirect
60+
google.golang.org/genproto/googleapis/api v0.0.0-20250603155806-513f23925822 // indirect
61+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250603155806-513f23925822 // indirect
5862
)

0 commit comments

Comments
 (0)