-
Notifications
You must be signed in to change notification settings - Fork 1.8k
chore(secretmanager): Added samples for delayed destroy #5351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
durgesh-ninave-crest
wants to merge
6
commits into
GoogleCloudPlatform:main
Choose a base branch
from
durgesh-ninave-crest:secretmanager-delayed-destroy-samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
822fb33
chore(secretmanager): Added samples for delayed destroy
durgesh-ninave-crest f690b50
chore(secretmanager): address gemini review comments
durgesh-ninave-crest 8c23be2
Merge branch 'main' into secretmanager-delayed-destroy-samples
durgesh-ninave-crest bf4b35b
chore(secretmanager): Updated test assertions
durgesh-ninave-crest 0e4067c
Merge branch 'main' into secretmanager-delayed-destroy-samples
durgesh-ninave-crest 04e3539
Merge branch 'main' into secretmanager-delayed-destroy-samples
durgesh-ninave-crest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package secretmanager | ||
|
||
// [START secretmanager_create_secret_with_delayed_destroy] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
secretmanager "cloud.google.com/go/secretmanager/apiv1" | ||
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb" | ||
"google.golang.org/protobuf/types/known/durationpb" | ||
) | ||
|
||
// createSecretWithDelayedDestroy creates a new secret with the given name | ||
// and version destroy ttl. A secret is a logical wrapper around a collection | ||
// of secret versions. Secret versions hold the actual secret material. | ||
func createSecretWithDelayedDestroy(w io.Writer, parent, secretId string, versionDestroyTtl int) error { | ||
// parent := "projects/my-project" | ||
// secretId := "my-secret" | ||
// versionDestroyTtl := 86400 | ||
|
||
// Create the client. | ||
ctx := context.Background() | ||
client, err := secretmanager.NewClient(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to create secretmanager client: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
// Build the request. | ||
req := &secretmanagerpb.CreateSecretRequest{ | ||
Parent: parent, | ||
SecretId: secretId, | ||
Secret: &secretmanagerpb.Secret{ | ||
Replication: &secretmanagerpb.Replication{ | ||
Replication: &secretmanagerpb.Replication_Automatic_{ | ||
Automatic: &secretmanagerpb.Replication_Automatic{}, | ||
}, | ||
}, | ||
VersionDestroyTtl: durationpb.New(time.Duration(versionDestroyTtl) * time.Second), | ||
}, | ||
} | ||
|
||
// Call the API. | ||
result, err := client.CreateSecret(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("failed to create secret: %w", err) | ||
} | ||
fmt.Fprintf(w, "Created secret with version destroy ttl: %s\n", result.Name) | ||
return nil | ||
} | ||
|
||
// [END secretmanager_create_secret_with_delayed_destroy] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package secretmanager | ||
|
||
// [START secretmanager_disable_secret_delayed_destroy] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
secretmanager "cloud.google.com/go/secretmanager/apiv1" | ||
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb" | ||
"google.golang.org/genproto/protobuf/field_mask" | ||
) | ||
|
||
// disableSecretDelayedDestroy creates a new secret with the given name | ||
// and version destroy ttl. A secret is a logical wrapper around a collection | ||
// of secret versions. Secret versions hold the actual secret material. | ||
durgesh-ninave-crest marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
func disableSecretDelayedDestroy(w io.Writer, name string) error { | ||
// name := "projects/my-project/secrets/my-secret" | ||
|
||
// Create the client. | ||
ctx := context.Background() | ||
client, err := secretmanager.NewClient(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to create secretmanager client: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
// Build the request. | ||
req := &secretmanagerpb.UpdateSecretRequest{ | ||
Secret: &secretmanagerpb.Secret{ | ||
Name: name, | ||
}, | ||
UpdateMask: &field_mask.FieldMask{ | ||
Paths: []string{"version_destroy_ttl"}, | ||
}, | ||
} | ||
|
||
// Call the API. | ||
result, err := client.UpdateSecret(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("failed to update secret: %w", err) | ||
} | ||
fmt.Fprintf(w, "Updated secret: %s\n", result.Name) | ||
return nil | ||
} | ||
|
||
// [END secretmanager_disable_secret_delayed_destroy] |
68 changes: 68 additions & 0 deletions
68
secretmanager/regional_samples/create_regional_secret_with_delayed_destroy.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package regional_secretmanager | ||
|
||
// [START secretmanager_create_regional_secret_with_delayed_destroy] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
secretmanager "cloud.google.com/go/secretmanager/apiv1" | ||
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb" | ||
"google.golang.org/api/option" | ||
"google.golang.org/protobuf/types/known/durationpb" | ||
) | ||
|
||
// createRegionalSecretWithDelayedDestroy creates a new secret with the given name | ||
// and version destroy ttl. A secret is a logical wrapper around a collection | ||
// of secret versions. Secret versions hold the actual secret material. | ||
func createRegionalSecretWithDelayedDestroy(w io.Writer, projectId, locationId, secretId string, versionDestroyTtl int) error { | ||
durgesh-ninave-crest marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// projectId := "my-project" | ||
// locationId := "us-central1" | ||
// secretId := "my-secret" | ||
// versionDestroyTtl := 86400 | ||
|
||
// Create the client. | ||
ctx := context.Background() | ||
endpoint := fmt.Sprintf("secretmanager.%s.rep.googleapis.com:443", locationId) | ||
client, err := secretmanager.NewClient(ctx, option.WithEndpoint(endpoint)) | ||
if err != nil { | ||
return fmt.Errorf("failed to create secretmanager client: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
parent := fmt.Sprintf("projects/%s/locations/%s", projectId, locationId) | ||
|
||
// Build the request. | ||
req := &secretmanagerpb.CreateSecretRequest{ | ||
Parent: parent, | ||
SecretId: secretId, | ||
Secret: &secretmanagerpb.Secret{ | ||
VersionDestroyTtl: durationpb.New(time.Duration(versionDestroyTtl) * time.Second), | ||
}, | ||
} | ||
|
||
// Call the API. | ||
result, err := client.CreateSecret(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("failed to create secret: %w", err) | ||
} | ||
fmt.Fprintf(w, "Created secret with version destroy ttl: %s\n", result.Name) | ||
return nil | ||
} | ||
|
||
// [END secretmanager_create_regional_secret_with_delayed_destroy] |
67 changes: 67 additions & 0 deletions
67
secretmanager/regional_samples/disable_regional_secret_delayed_destroy.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package regional_secretmanager | ||
|
||
// [START secretmanager_disable_regional_secret_delayed_destroy] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
secretmanager "cloud.google.com/go/secretmanager/apiv1" | ||
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb" | ||
"google.golang.org/api/option" | ||
"google.golang.org/genproto/protobuf/field_mask" | ||
) | ||
|
||
// disableRegionalSecretDelayedDestroy creates a new secret with the given name | ||
// and version destroy ttl. A secret is a logical wrapper around a collection | ||
// of secret versions. Secret versions hold the actual secret material. | ||
durgesh-ninave-crest marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
func disableRegionalSecretDelayedDestroy(w io.Writer, projectId, locationId, secretId string) error { | ||
durgesh-ninave-crest marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// projectId := "my-project" | ||
// locationId := "us-central1" | ||
// secretId := "my-secret" | ||
|
||
// Create the client. | ||
ctx := context.Background() | ||
endpoint := fmt.Sprintf("secretmanager.%s.rep.googleapis.com:443", locationId) | ||
client, err := secretmanager.NewClient(ctx, option.WithEndpoint(endpoint)) | ||
if err != nil { | ||
return fmt.Errorf("failed to create secretmanager client: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
name := fmt.Sprintf("projects/%s/locations/%s/secrets/%s", projectId, locationId, secretId) | ||
|
||
// Build the request. | ||
req := &secretmanagerpb.UpdateSecretRequest{ | ||
Secret: &secretmanagerpb.Secret{ | ||
Name: name, | ||
}, | ||
UpdateMask: &field_mask.FieldMask{ | ||
Paths: []string{"version_destroy_ttl"}, | ||
}, | ||
} | ||
|
||
// Call the API. | ||
result, err := client.UpdateSecret(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("failed to update secret: %w", err) | ||
} | ||
fmt.Fprintf(w, "Updated secret: %s\n", result.Name) | ||
return nil | ||
} | ||
|
||
// [END secretmanager_disable_regional_secret_delayed_destroy] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.