Skip to content

Commit d9c2a6b

Browse files
authored
chore: Add SecretManager service regional code samples (#2880)
1 parent 0e2a948 commit d9c2a6b

File tree

57 files changed

+3085
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+3085
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 Xunit;
19+
20+
[Collection(nameof(RegionalSecretManagerFixture))]
21+
public class AccessRegionalSecretVersionTests
22+
{
23+
private readonly RegionalSecretManagerFixture _fixture;
24+
private readonly AccessRegionalSecretVersionSample _sample;
25+
26+
public AccessRegionalSecretVersionTests(RegionalSecretManagerFixture fixture)
27+
{
28+
_fixture = fixture;
29+
_sample = new AccessRegionalSecretVersionSample();
30+
}
31+
32+
[Fact]
33+
public void AccessRegionalSecretVersion()
34+
{
35+
// Get the secret version name.
36+
SecretVersionName secretVersionName = _fixture.SecretVersion.SecretVersionName;
37+
38+
// Run the code sample.
39+
string result = _sample.AccessRegionalSecretVersion(
40+
projectId: secretVersionName.ProjectId,
41+
locationId: secretVersionName.LocationId,
42+
secretId: secretVersionName.SecretId,
43+
secretVersionId: secretVersionName.SecretVersionId
44+
);
45+
46+
// Assert that the secret was created with the correct value.
47+
Assert.Equal("my super secret data", result);
48+
}
49+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 Xunit;
19+
20+
[Collection(nameof(RegionalSecretManagerFixture))]
21+
public class AddRegionalSecretVersionTests
22+
{
23+
private readonly RegionalSecretManagerFixture _fixture;
24+
private readonly AddRegionalSecretVersionSample _sample;
25+
26+
public AddRegionalSecretVersionTests(RegionalSecretManagerFixture fixture)
27+
{
28+
_fixture = fixture;
29+
_sample = new AddRegionalSecretVersionSample();
30+
}
31+
32+
[Fact]
33+
public void AddsRegionalSecretVersions()
34+
{
35+
string data = "my secret data";
36+
37+
// Create the secret and add secret version.
38+
Secret secret = _fixture.CreateSecret(_fixture.RandomId());
39+
SecretName secretName = secret.SecretName;
40+
SecretVersion secretVersion = _sample.AddRegionalSecretVersion(
41+
projectId: secretName.ProjectId,
42+
locationId: secretName.LocationId,
43+
secretId: secretName.SecretId,
44+
data: data
45+
);
46+
47+
// Access the secret version.
48+
AccessSecretVersionResponse result = _fixture.Client.AccessSecretVersion(secretVersion.SecretVersionName);
49+
50+
// Assert that the secret version was added with the correct data.
51+
Assert.Equal(data, result.Payload.Data.ToStringUtf8());
52+
53+
// Clean the created secret.
54+
_fixture.DeleteSecret(secret.SecretName);
55+
}
56+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 System;
19+
using Xunit;
20+
21+
[Collection(nameof(RegionalSecretManagerFixture))]
22+
public class CreateRegionalSecretTests
23+
{
24+
private readonly RegionalSecretManagerFixture _fixture;
25+
private readonly CreateRegionalSecretSample _sample;
26+
27+
public CreateRegionalSecretTests(RegionalSecretManagerFixture fixture)
28+
{
29+
_fixture = fixture;
30+
_sample = new CreateRegionalSecretSample();
31+
}
32+
33+
[Fact]
34+
public void CreatesRegionalSecrets()
35+
{
36+
// Get the SecretName from the set ProjectId & LocationId.
37+
SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId());
38+
39+
// Run the sample code.
40+
Secret result = _sample.CreateRegionalSecret(
41+
projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId);
42+
43+
// Assert that the secret was created with the correct secretId.
44+
Assert.Equal(result.SecretName.SecretId, secretName.SecretId);
45+
46+
// Clean the created secret.
47+
_fixture.DeleteSecret(secretName);
48+
}
49+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 System;
19+
using Xunit;
20+
21+
22+
[Collection(nameof(RegionalSecretManagerFixture))]
23+
public class CreateRegionalSecretWithAnnotationsTests
24+
{
25+
private readonly RegionalSecretManagerFixture _fixture;
26+
private readonly CreateRegionalSecretWithAnnotationsSample _sample;
27+
28+
public CreateRegionalSecretWithAnnotationsTests(RegionalSecretManagerFixture fixture)
29+
{
30+
_fixture = fixture;
31+
_sample = new CreateRegionalSecretWithAnnotationsSample();
32+
}
33+
34+
[Fact]
35+
public void CreatesRegionalSecretsWithAnnotations()
36+
{
37+
// Get the SecretName from the set ProjectId & LocationId.
38+
SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId());
39+
40+
// Get the annotation key & value from the fixture class.
41+
string annotationKey = _fixture.AnnotationKey;
42+
string annotationValue = _fixture.AnnotationValue;
43+
44+
// Run the code sample.
45+
Secret result = _sample.CreateRegionalSecretWithAnnotations(
46+
projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId, annotationKey: annotationKey, annotationValue: annotationValue);
47+
48+
// Assert that the secretId is same as expected.
49+
Assert.Equal(result.SecretName.SecretId, secretName.SecretId);
50+
51+
// Assert that the annotation values matches with the expected value.
52+
Assert.Equal(result.Annotations[annotationKey], annotationValue);
53+
54+
// Clean the created secret.
55+
_fixture.DeleteSecret(secretName);
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 System;
19+
using Xunit;
20+
21+
22+
[Collection(nameof(RegionalSecretManagerFixture))]
23+
public class CreateRegionalSecretWithLabelsTests
24+
{
25+
private readonly RegionalSecretManagerFixture _fixture;
26+
private readonly CreateRegionalSecretWithLabelsSample _sample;
27+
28+
public CreateRegionalSecretWithLabelsTests(RegionalSecretManagerFixture fixture)
29+
{
30+
_fixture = fixture;
31+
_sample = new CreateRegionalSecretWithLabelsSample();
32+
}
33+
34+
[Fact]
35+
public void CreatesRegionalSecretsWithLabels()
36+
{
37+
// Get the SecretName from the set ProjectId & LocationId.
38+
SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId());
39+
40+
// Get the label value and key and from fixture class.
41+
string labelKey = _fixture.AnnotationKey;
42+
string labelValue = _fixture.AnnotationValue;
43+
44+
// Run the code sample.
45+
Secret result = _sample.CreateRegionalSecretWithLabels(
46+
projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId, labelKey: labelKey, labelValue: labelValue);
47+
48+
// Assert that the secretId is equal to the expected value.
49+
Assert.Equal(result.SecretName.SecretId, secretName.SecretId);
50+
51+
// Assert that the label value matches with the expected value.
52+
Assert.Equal(result.Labels[labelKey], labelValue);
53+
54+
// Clean the created secret.
55+
_fixture.DeleteSecret(secretName);
56+
}
57+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 Xunit;
19+
20+
[Collection(nameof(RegionalSecretManagerFixture))]
21+
public class DeleteRegionalSecretTests
22+
{
23+
private readonly RegionalSecretManagerFixture _fixture;
24+
private readonly DeleteRegionalSecretSample _sample;
25+
26+
public DeleteRegionalSecretTests(RegionalSecretManagerFixture fixture)
27+
{
28+
_fixture = fixture;
29+
_sample = new DeleteRegionalSecretSample();
30+
}
31+
32+
[Fact]
33+
public void DeletesRegionalSecrets()
34+
{
35+
// Create the secret.
36+
Secret secret = _fixture.CreateSecret(_fixture.RandomId());
37+
38+
// Get the secretName from the created secret.
39+
SecretName secretName = secret.SecretName;
40+
41+
// Run the sample.
42+
_sample.DeleteRegionalSecret(
43+
projectId: secretName.ProjectId,
44+
locationId: secretName.LocationId,
45+
secretId: secretName.SecretId
46+
);
47+
48+
// Assert that the secret was deleted.
49+
Assert.Throws<Grpc.Core.RpcException>(() => _fixture.Client.GetSecret(secretName));
50+
}
51+
}
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 Xunit;
19+
20+
[Collection(nameof(RegionalSecretManagerFixture))]
21+
public class DeleteRegionalSecretWithEtagTests
22+
{
23+
private readonly RegionalSecretManagerFixture _fixture;
24+
private readonly DeleteRegionalSecretWithEtagSample _sample;
25+
26+
public DeleteRegionalSecretWithEtagTests(RegionalSecretManagerFixture fixture)
27+
{
28+
_fixture = fixture;
29+
_sample = new DeleteRegionalSecretWithEtagSample();
30+
}
31+
32+
[Fact]
33+
public void DeletesRegionalSecretsWithEtag()
34+
{
35+
// Create the secret.
36+
Secret secret = _fixture.CreateSecret(_fixture.RandomId());
37+
38+
// Get the secret name.
39+
SecretName secretName = secret.SecretName;
40+
41+
// Get the secret from the cloud.
42+
Secret secretFetched = _fixture.Client.GetSecret(secretName);
43+
44+
// Set the updated etag fetched.
45+
string updatedEtag = secretFetched.Etag;
46+
47+
// Run the sample to delete the secret with etag.
48+
_sample.DeleteRegionalSecretWithEtag(
49+
projectId: secretName.ProjectId,
50+
locationId: secretName.LocationId,
51+
secretId: secretName.SecretId,
52+
etag: updatedEtag
53+
);
54+
55+
// Assert that the secret was deleted and not found.
56+
Assert.Throws<Grpc.Core.RpcException>(() => _fixture.Client.GetSecret(secretName));
57+
}
58+
}

0 commit comments

Comments
 (0)