Skip to content

Commit 776643d

Browse files
committed
chore(secretmanager): Add Regional Code Samples for Delayed Destroy
1 parent 6c5c651 commit 776643d

10 files changed

+430
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
* https://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+
using Google.Cloud.SecretManager.V1;
18+
using Google.Protobuf.WellKnownTypes;
19+
20+
using System;
21+
using Xunit;
22+
23+
24+
[Collection(nameof(RegionalSecretManagerFixture))]
25+
public class CreateRegionalSecretWithDelayedDestroyTests
26+
{
27+
private readonly RegionalSecretManagerFixture _fixture;
28+
private readonly CreateRegionalSecretWithDelayedDestroySample _sample;
29+
30+
public CreateRegionalSecretWithDelayedDestroyTests(RegionalSecretManagerFixture fixture)
31+
{
32+
_fixture = fixture;
33+
_sample = new CreateRegionalSecretWithDelayedDestroySample();
34+
}
35+
36+
[Fact]
37+
public void CreatesRegionalSecretsWithDelayedDestroy()
38+
{
39+
// Get the SecretName from the set ProjectId & LocationId.
40+
SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId());
41+
42+
// Set the timeToLive Value.
43+
int timeToLive = 86400;
44+
45+
// Run the sample.
46+
Secret result = _sample.CreateRegionalSecretWithDelayedDestroy(
47+
projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId, timeToLive: timeToLive);
48+
49+
// Assert that the secret was created with corect configurations
50+
Assert.Equal(result.SecretName.SecretId, secretName.SecretId);
51+
Assert.Equal(result.VersionDestroyTtl, Duration.FromTimeSpan(TimeSpan.FromSeconds(timeToLive)));
52+
53+
// Clean the created secret.
54+
_fixture.DeleteSecret(secretName);
55+
}
56+
}

secretmanager/api/SecretManager.Samples.Tests/DestroyRegionalSecretVersionTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,29 @@ public void DestroysRegionalSecretVersions()
5151
// Clean the created secret.
5252
_fixture.DeleteSecret(secret.SecretName);
5353
}
54+
55+
[Fact]
56+
public void DisablesSecretVersionWhenDelayedDestroyEnabled()
57+
{
58+
// Create the secret and add secret version.
59+
Secret secret = _fixture.CreateSecretWithDelayedDestroy();
60+
61+
// Add the secret version to the created secret.
62+
SecretVersion version = _fixture.AddSecretVersion(secret);
63+
SecretVersionName secretVersionName = version.SecretVersionName;
64+
65+
// Run the sample.
66+
SecretVersion secretVersion = _sample.DestroyRegionalSecretVersion(
67+
projectId: secretVersionName.ProjectId,
68+
locationId: secretVersionName.LocationId,
69+
secretId: secretVersionName.SecretId,
70+
secretVersionId: secretVersionName.SecretVersionId
71+
);
72+
73+
// Assert that the Secret gets disabled.
74+
Assert.Equal(SecretVersion.Types.State.Disabled, secretVersion.State);
75+
76+
// Clean up the created resource
77+
_fixture.DeleteSecret(secret.SecretName);
78+
}
5479
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
* https://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+
using Google.Cloud.SecretManager.V1;
18+
using Xunit;
19+
20+
[Collection(nameof(RegionalSecretManagerFixture))]
21+
public class DisableRegionalSecretDelayedDestroyTests
22+
{
23+
private readonly RegionalSecretManagerFixture _fixture;
24+
private readonly DisableRegionalSecretDelayedDestroySample _sample;
25+
26+
public DisableRegionalSecretDelayedDestroyTests(RegionalSecretManagerFixture fixture)
27+
{
28+
_fixture = fixture;
29+
_sample = new DisableRegionalSecretDelayedDestroySample();
30+
}
31+
32+
[Fact]
33+
public void DisableRegionalsSecretDelayedDestroy()
34+
{
35+
// Create the secret and add secret version.
36+
Secret secret = _fixture.CreateSecretWithDelayedDestroy();
37+
38+
// Run the code.
39+
Secret disabledSecret = _sample.DisableRegionalSecretDelayedDestroy(
40+
projectId: secret.SecretName.ProjectId, locationId: secret.SecretName.LocationId, secretId: secret.SecretName.SecretId);
41+
42+
// Assert that the secret was created with the correct configurations.
43+
Assert.Null(disabledSecret.VersionDestroyTtl);
44+
45+
// Clean the created secret.
46+
_fixture.DeleteSecret(secret.SecretName);
47+
}
48+
}

secretmanager/api/SecretManager.Samples.Tests/DisableRegionalSecretVersionTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,32 @@ public void DisablesRegionalSecretVersions()
5151
// Clean the created secret.
5252
_fixture.DeleteSecret(secret.SecretName);
5353
}
54+
55+
[Fact]
56+
public void DisablesSecretVersionScheduledForDestruction()
57+
{
58+
// Create the secret and add secret version.
59+
Secret secret = _fixture.CreateSecretWithDelayedDestroy();
60+
61+
// Add the secret version to the created secret.
62+
SecretVersion version = _fixture.AddSecretVersion(secret);
63+
SecretVersionName secretVersionName = version.SecretVersionName;
64+
65+
// Destroy the created secret.
66+
SecretVersion destroyedVersion = _fixture.DestroySecretVersion(version);
67+
68+
// Run the code sample.
69+
SecretVersion secretVersion = _sample.DisableRegionalSecretVersion(
70+
projectId: secretVersionName.ProjectId,
71+
locationId: secretVersionName.LocationId,
72+
secretId: secretVersionName.SecretId,
73+
secretVersionId: secretVersionName.SecretVersionId
74+
);
75+
76+
// Assert that the Secret gets disabled.
77+
Assert.Equal(SecretVersion.Types.State.Disabled, secretVersion.State);
78+
79+
// Clean up the created resource
80+
_fixture.DeleteSecret(secret.SecretName);
81+
}
5482
}

secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretVersionTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,32 @@ public void EnablesRegionalSecretVersions()
5151
// Clean the created secret.
5252
_fixture.DeleteSecret(secret.SecretName);
5353
}
54+
55+
[Fact]
56+
public void EnablesSecretVersionScheduledForDestruction()
57+
{
58+
// Create the secret and add secret version.
59+
Secret secret = _fixture.CreateSecretWithDelayedDestroy();
60+
61+
// Add the secret version to the created secret.
62+
SecretVersion version = _fixture.AddSecretVersion(secret);
63+
SecretVersionName secretVersionName = version.SecretVersionName;
64+
65+
// Destroy the created secret.
66+
SecretVersion destroyedVersion = _fixture.DestroySecretVersion(version);
67+
68+
// Run the code sample.
69+
SecretVersion secretVersion = _sample.EnableRegionalSecretVersion(
70+
projectId: secretVersionName.ProjectId,
71+
locationId: secretVersionName.LocationId,
72+
secretId: secretVersionName.SecretId,
73+
secretVersionId: secretVersionName.SecretVersionId
74+
);
75+
76+
// Assert that the Secret gets disabled.
77+
Assert.Equal(SecretVersion.Types.State.Enabled, secretVersion.State);
78+
79+
// Clean up the created resource
80+
_fixture.DeleteSecret(secret.SecretName);
81+
}
5482
}

secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Google.Api.Gax.ResourceNames;
1616
using Google.Cloud.SecretManager.V1;
1717
using Google.Protobuf;
18+
using Google.Protobuf.WellKnownTypes;
1819
using System;
1920
using System.Text;
2021
using Xunit;
@@ -90,6 +91,20 @@ public Secret CreateSecret(string secretId)
9091
return Client.CreateSecret(locationName, secretId, secret);
9192
}
9293

94+
public Secret CreateSecretWithDelayedDestroy()
95+
{
96+
LocationName locationName = new LocationName(ProjectId, LocationId);
97+
98+
Secret secret = new Secret
99+
{
100+
VersionDestroyTtl = new Duration
101+
{
102+
Seconds = 24 * 60 * 60,
103+
}
104+
};
105+
return Client.CreateSecret(locationName, RandomId(), secret);
106+
}
107+
93108
public SecretVersion AddSecretVersion(Secret secret)
94109
{
95110
SecretPayload payload = new SecretPayload
@@ -117,4 +132,9 @@ public SecretVersion DisableSecretVersion(SecretVersion version)
117132
{
118133
return Client.DisableSecretVersion(version.SecretVersionName);
119134
}
135+
136+
public SecretVersion DestroySecretVersion(SecretVersion version)
137+
{
138+
return Client.DestroySecretVersion(version.SecretVersionName);
139+
}
120140
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 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+
* https://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+
using Google.Cloud.SecretManager.V1;
18+
using Google.Protobuf.WellKnownTypes;
19+
using System;
20+
using Xunit;
21+
22+
[Collection(nameof(RegionalSecretManagerFixture))]
23+
public class UpdateRegionalSecretWithDelayedDestroyTests
24+
{
25+
private readonly RegionalSecretManagerFixture _fixture;
26+
private readonly UpdateRegionalSecretWithDelayedDestroySample _sample;
27+
28+
public UpdateRegionalSecretWithDelayedDestroyTests(RegionalSecretManagerFixture fixture)
29+
{
30+
_fixture = fixture;
31+
_sample = new UpdateRegionalSecretWithDelayedDestroySample();
32+
}
33+
34+
[Fact]
35+
public void UpdatesRegionalSecretsWithDelayedDestroy()
36+
{
37+
// Create the secret and add secret version.
38+
Secret secret = _fixture.CreateSecretWithDelayedDestroy();
39+
40+
// Set the updated time to live value.
41+
int updatedTimeToLive = 172800;
42+
43+
// Run the sample.
44+
Secret result = _sample.UpdateRegionalSecretWithDelayedDestroy(
45+
projectId: secret.SecretName.ProjectId,
46+
locationId: secret.SecretName.LocationId,
47+
secretId: secret.SecretName.SecretId,
48+
updatedTimeToLive: updatedTimeToLive
49+
);
50+
51+
// Assert that the secret was updated with the correct configurations.
52+
Assert.Equal(result.SecretName.SecretId, secret.SecretName.SecretId);
53+
Assert.Equal(result.VersionDestroyTtl, Duration.FromTimeSpan(TimeSpan.FromSeconds(updatedTimeToLive)));
54+
55+
// Clean the created secret.
56+
_fixture.DeleteSecret(secret.SecretName);
57+
}
58+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
* https://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+
// [START secretmanager_create_regional_secret_with_delayed_destroy]
18+
19+
using Google.Api.Gax.ResourceNames;
20+
using Google.Cloud.SecretManager.V1;
21+
using Google.Protobuf.WellKnownTypes;
22+
using System.Collections.Generic;
23+
24+
public class CreateRegionalSecretWithDelayedDestroySample
25+
{
26+
public Secret CreateRegionalSecretWithDelayedDestroy(
27+
string projectId = "my-project",
28+
string locationId = "my-location",
29+
string secretId = "my-secret",
30+
int timeToLive = 86400
31+
)
32+
{
33+
// Create the Regional Secret Manager Client.
34+
SecretManagerServiceClient client = new SecretManagerServiceClientBuilder
35+
{
36+
Endpoint = $"secretmanager.{locationId}.rep.googleapis.com"
37+
}.Build();
38+
39+
// Build the parent resource name.
40+
LocationName location = new LocationName(projectId, locationId);
41+
42+
// Build the secret.
43+
Secret secret = new Secret
44+
{
45+
VersionDestroyTtl = new Duration
46+
{
47+
Seconds = timeToLive,
48+
}
49+
};
50+
51+
// Call the API.
52+
Secret createdSecret = client.CreateSecret(location, secretId, secret);
53+
return createdSecret;
54+
}
55+
}
56+
// [END secretmanager_create_regional_secret_with_delayed_destroy]

0 commit comments

Comments
 (0)