Skip to content

Commit 900f7fe

Browse files
chore(secretmanager): Added samples for delayed destroy (#10136)
* chore(secretmanager): Added samples for delayed destroy * chore(secretmanager): Update code sample * chore(secretmanager): Update test assertions
1 parent 3f8043a commit 900f7fe

File tree

8 files changed

+495
-1
lines changed

8 files changed

+495
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager;
18+
19+
// [START secretmanager_create_secret_with_delayed_destroy]
20+
21+
import com.google.cloud.secretmanager.v1.ProjectName;
22+
import com.google.cloud.secretmanager.v1.Replication;
23+
import com.google.cloud.secretmanager.v1.Secret;
24+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
25+
import com.google.protobuf.Duration;
26+
import java.io.IOException;
27+
28+
public class CreateSecretWithDelayedDestroy {
29+
30+
public static void createSecretWithDelayedDestroy() throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "your-project-id";
33+
String secretId = "your-secret-id";
34+
Integer versionDestroyTtl = 86400;
35+
createSecretWithDelayedDestroy(projectId, secretId, versionDestroyTtl);
36+
}
37+
38+
// Create secret with version destroy TTL.
39+
public static Secret createSecretWithDelayedDestroy(
40+
String projectId,
41+
String secretId,
42+
Integer versionDestroyTtl)
43+
throws IOException {
44+
// Initialize the client that will be used to send requests.
45+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
46+
// Build the parent name from the project.
47+
ProjectName projectName = ProjectName.of(projectId);
48+
49+
// Build the secret to create.
50+
Secret secret =
51+
Secret.newBuilder()
52+
.setReplication(
53+
Replication.newBuilder()
54+
.setAutomatic(Replication.Automatic.newBuilder().build())
55+
.build())
56+
.setVersionDestroyTtl(Duration.newBuilder().setSeconds(versionDestroyTtl))
57+
.build();
58+
59+
// Create the secret.
60+
Secret createdSecret = client.createSecret(projectName, secretId, secret);
61+
System.out.printf("Created secret with version destroy ttl %s\n", createdSecret.getName());
62+
63+
return createdSecret;
64+
}
65+
}
66+
}
67+
// [END secretmanager_create_secret_with_delayed_destroy]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager;
18+
19+
// [START secretmanager_disable_secret_delayed_destroy]
20+
21+
import com.google.cloud.secretmanager.v1.Secret;
22+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
23+
import com.google.cloud.secretmanager.v1.SecretName;
24+
import com.google.protobuf.FieldMask;
25+
import com.google.protobuf.util.FieldMaskUtil;
26+
import java.io.IOException;
27+
28+
public class DisableSecretDelayedDestroy {
29+
30+
public static void disableSecretDelayedDestroy() throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "your-project-id";
33+
String secretId = "your-secret-id";
34+
disableSecretDelayedDestroy(projectId, secretId);
35+
}
36+
37+
// Disables delayed destroy for a secret.
38+
public static Secret disableSecretDelayedDestroy(
39+
String projectId,
40+
String secretId)
41+
throws IOException {
42+
// Initialize the client that will be used to send requests.
43+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
44+
// Build the parent name from the project and secret.
45+
SecretName secretName = SecretName.of(projectId, secretId);
46+
47+
// Build the secret to update.
48+
Secret secret =
49+
Secret.newBuilder()
50+
.setName(secretName.toString())
51+
.build();
52+
53+
// Build the field mask.
54+
FieldMask fieldMask = FieldMaskUtil.fromString("version_destroy_ttl");
55+
56+
// Update the secret.
57+
Secret updatedSecret = client.updateSecret(secret, fieldMask);
58+
System.out.printf("Updated secret %s\n", updatedSecret.getName());
59+
60+
return updatedSecret;
61+
}
62+
}
63+
}
64+
// [END secretmanager_disable_secret_delayed_destroy]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager;
18+
19+
// [START secretmanager_update_secret_with_delayed_destroy]
20+
21+
import com.google.cloud.secretmanager.v1.Secret;
22+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
23+
import com.google.cloud.secretmanager.v1.SecretName;
24+
import com.google.protobuf.Duration;
25+
import com.google.protobuf.FieldMask;
26+
import com.google.protobuf.util.FieldMaskUtil;
27+
import java.io.IOException;
28+
29+
public class UpdateSecretWithDelayedDestroy {
30+
31+
public static void updateSecretWithDelayedDestroy() throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "your-project-id";
34+
String secretId = "your-secret-id";
35+
Integer versionDestroyTtl = 86400;
36+
updateSecretWithDelayedDestroy(projectId, secretId, versionDestroyTtl);
37+
}
38+
39+
// Update secret with version destroy TTL.
40+
public static Secret updateSecretWithDelayedDestroy(
41+
String projectId,
42+
String secretId,
43+
Integer versionDestroyTtl)
44+
throws IOException {
45+
// Initialize the client that will be used to send requests.
46+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
47+
// Build the parent name from the project and secret.
48+
SecretName secretName = SecretName.of(projectId, secretId);
49+
50+
// Build the secret to update.
51+
Secret secret =
52+
Secret.newBuilder()
53+
.setName(secretName.toString())
54+
.setVersionDestroyTtl(Duration.newBuilder().setSeconds(versionDestroyTtl))
55+
.build();
56+
57+
// Build the field mask.
58+
FieldMask fieldMask = FieldMaskUtil.fromString("version_destroy_ttl");
59+
60+
// Update the secret.
61+
Secret updatedSecret = client.updateSecret(secret, fieldMask);
62+
System.out.printf("Updated secret %s\n", updatedSecret.getName());
63+
64+
return updatedSecret;
65+
}
66+
}
67+
}
68+
// [END secretmanager_update_secret_with_delayed_destroy]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager.regionalsamples;
18+
19+
// [START secretmanager_create_regional_secret_with_delayed_destroy]
20+
21+
import com.google.cloud.secretmanager.v1.LocationName;
22+
import com.google.cloud.secretmanager.v1.Secret;
23+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
24+
import com.google.cloud.secretmanager.v1.SecretManagerServiceSettings;
25+
import com.google.protobuf.Duration;
26+
import java.io.IOException;
27+
28+
public class CreateRegionalSecretWithDelayedDestroy {
29+
30+
public static void createRegionalSecretWithDelayedDestroy() throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "your-project-id";
33+
String locationId = "your-location-id";
34+
String secretId = "your-secret-id";
35+
Integer versionDestroyTtl = 86400;
36+
createRegionalSecretWithDelayedDestroy(projectId, locationId, secretId, versionDestroyTtl);
37+
}
38+
39+
// Create secret with version destroy TTL.
40+
public static Secret createRegionalSecretWithDelayedDestroy(
41+
String projectId,
42+
String locationId,
43+
String secretId,
44+
Integer versionDestroyTtl)
45+
throws IOException {
46+
// Endpoint to call the regional secret manager sever
47+
String apiEndpoint = String.format("secretmanager.%s.rep.googleapis.com:443", locationId);
48+
SecretManagerServiceSettings secretManagerServiceSettings =
49+
SecretManagerServiceSettings.newBuilder().setEndpoint(apiEndpoint).build();
50+
51+
// Initialize the client that will be used to send requests.
52+
try (SecretManagerServiceClient client =
53+
SecretManagerServiceClient.create(secretManagerServiceSettings)) {
54+
// Build the parent name from the project.
55+
LocationName locationName = LocationName.of(projectId, locationId);
56+
57+
// Build the secret to create.
58+
Secret secret =
59+
Secret.newBuilder()
60+
.setVersionDestroyTtl(Duration.newBuilder().setSeconds(versionDestroyTtl))
61+
.build();
62+
63+
// Create the secret.
64+
Secret createdSecret = client.createSecret(locationName, secretId, secret);
65+
System.out.printf("Created secret with version destroy ttl %s\n", createdSecret.getName());
66+
67+
return createdSecret;
68+
}
69+
}
70+
}
71+
// [END secretmanager_create_regional_secret_with_delayed_destroy]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager.regionalsamples;
18+
19+
// [START secretmanager_disable_regional_secret_delayed_destroy]
20+
21+
import com.google.cloud.secretmanager.v1.Secret;
22+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
23+
import com.google.cloud.secretmanager.v1.SecretManagerServiceSettings;
24+
import com.google.cloud.secretmanager.v1.SecretName;
25+
import com.google.protobuf.FieldMask;
26+
import com.google.protobuf.util.FieldMaskUtil;
27+
import java.io.IOException;
28+
29+
public class DisableRegionalSecretDelayedDestroy {
30+
31+
public static void disableRegionalSecretDelayedDestroy() throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "your-project-id";
34+
String locationId = "your-location-id";
35+
String secretId = "your-secret-id";
36+
disableRegionalSecretDelayedDestroy(projectId, locationId, secretId);
37+
}
38+
39+
// Disables the secret's delayed destroy.
40+
public static Secret disableRegionalSecretDelayedDestroy(
41+
String projectId,
42+
String locationId,
43+
String secretId)
44+
throws IOException {
45+
// Endpoint to call the regional secret manager sever
46+
String apiEndpoint = String.format("secretmanager.%s.rep.googleapis.com:443", locationId);
47+
SecretManagerServiceSettings secretManagerServiceSettings =
48+
SecretManagerServiceSettings.newBuilder().setEndpoint(apiEndpoint).build();
49+
50+
// Initialize the client that will be used to send requests.
51+
try (SecretManagerServiceClient client =
52+
SecretManagerServiceClient.create(secretManagerServiceSettings)) {
53+
// Build the parent name from the project and secret.
54+
SecretName secretName =
55+
SecretName.ofProjectLocationSecretName(projectId, locationId, secretId);
56+
57+
// Build the secret to update.
58+
Secret secret =
59+
Secret.newBuilder()
60+
.setName(secretName.toString())
61+
.build();
62+
63+
// Build the field mask.
64+
FieldMask fieldMask = FieldMaskUtil.fromString("version_destroy_ttl");
65+
66+
// Update the secret.
67+
Secret updatedSecret = client.updateSecret(secret, fieldMask);
68+
System.out.printf("Updated secret %s\n", updatedSecret.getName());
69+
70+
return updatedSecret;
71+
}
72+
}
73+
}
74+
// [END secretmanager_disable_regional_secret_delayed_destroy]

0 commit comments

Comments
 (0)