Skip to content

Commit be25489

Browse files
authored
chore(secretmanager): add code samples for secretmanager integration … (#3140)
### Changes - Add code samples for SecretManager integration with Tags.
1 parent 1f60484 commit be25489

File tree

5 files changed

+398
-0
lines changed

5 files changed

+398
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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.ResourceManager.V3;
18+
using Google.Cloud.SecretManager.V1;
19+
using System;
20+
using Xunit;
21+
22+
23+
[Collection(nameof(RegionalSecretManagerFixture))]
24+
public class CreateRegionalSecretWithTagsTests
25+
{
26+
private readonly RegionalSecretManagerFixture _fixture;
27+
private readonly CreateRegionalSecretWithTagsSample _sample;
28+
private readonly TagKeysClient _tagKeysClient;
29+
private readonly TagValuesClient _tagValuesClient;
30+
private string _tagKeyName;
31+
private string _tagValueName;
32+
33+
public CreateRegionalSecretWithTagsTests(RegionalSecretManagerFixture fixture)
34+
{
35+
_fixture = fixture;
36+
_sample = new CreateRegionalSecretWithTagsSample();
37+
_tagKeysClient = TagKeysClient.Create();
38+
_tagValuesClient = TagValuesClient.Create();
39+
}
40+
41+
private void CreateTagKeyAndValue(string projectId, string tagKeyShortName, string tagValueShortName)
42+
{
43+
var createKeyRequest = new CreateTagKeyRequest
44+
{
45+
TagKey = new TagKey
46+
{
47+
Parent = $"projects/{projectId}",
48+
ShortName = tagKeyShortName,
49+
}
50+
};
51+
52+
try
53+
{
54+
var createKeyOperation = _tagKeysClient.CreateTagKey(createKeyRequest);
55+
TagKey tagKey = createKeyOperation.PollUntilCompleted().Result;
56+
_tagKeyName = tagKey.Name;
57+
}
58+
catch (Exception e)
59+
{
60+
throw new Exception($"Error creating tag key: {e.Message}");
61+
}
62+
63+
var createValueRequest = new CreateTagValueRequest
64+
{
65+
TagValue = new TagValue
66+
{
67+
Parent = _tagKeyName,
68+
ShortName = tagValueShortName,
69+
}
70+
};
71+
72+
try
73+
{
74+
var createValueOperation = _tagValuesClient.CreateTagValue(createValueRequest);
75+
TagValue tagValue = createValueOperation.PollUntilCompleted().Result;
76+
_tagValueName = tagValue.Name;
77+
}
78+
catch (Exception e)
79+
{
80+
throw new Exception($"Error creating tag value: {e.Message}");
81+
}
82+
}
83+
84+
private void CleanTagKeyandValue()
85+
{
86+
if (!string.IsNullOrEmpty(_tagValueName))
87+
{
88+
try
89+
{
90+
DeleteTagValueRequest deleteValueRequest = new DeleteTagValueRequest { Name = _tagValueName };
91+
_tagValuesClient.DeleteTagValue(deleteValueRequest).PollUntilCompleted();
92+
}
93+
catch (Exception e)
94+
{
95+
Console.WriteLine("Deleting the tag value failed!");
96+
throw new Exception($"Error deleting tag value: {e.Message}");
97+
}
98+
}
99+
else
100+
{
101+
Console.WriteLine("TagValue.name was not set, skipping deletion.");
102+
}
103+
104+
if (!string.IsNullOrEmpty(_tagKeyName))
105+
{
106+
try
107+
{
108+
DeleteTagKeyRequest deleteKeyRequest = new DeleteTagKeyRequest { Name = _tagKeyName };
109+
_tagKeysClient.DeleteTagKey(deleteKeyRequest).PollUntilCompleted();
110+
}
111+
catch (Exception e)
112+
{
113+
Console.Error.WriteLine("Deleting the tag key failed!");
114+
throw new Exception($"Error deleting tag key: {e.Message}");
115+
}
116+
}
117+
else
118+
{
119+
Console.WriteLine("TagKey.name was not set, skipping deletion.");
120+
}
121+
}
122+
123+
[Fact]
124+
public void CreatesRegionalSecretsWithTags()
125+
{
126+
// Get the SecretName from the set ProjectId & LocationId.
127+
SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId());
128+
129+
// Define tag key and value short names
130+
string tagKeyShortName = $"test-key-{_fixture.RandomId()}";
131+
string tagValueShortName = $"test-value-{_fixture.RandomId()}";
132+
133+
CreateTagKeyAndValue(_fixture.ProjectId, tagKeyShortName, tagValueShortName);
134+
135+
// Run the code sample.
136+
Secret result = _sample.CreateRegionalSecretWithTags(
137+
projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId, tagKeyName: _tagKeyName, tagValueName: _tagValueName);
138+
139+
// Assert that the secretId is equal to the expected value.
140+
Assert.Equal(result.SecretName.SecretId, secretName.SecretId);
141+
142+
_fixture.DeleteSecret(secretName);
143+
CleanTagKeyandValue();
144+
}
145+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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.ResourceManager.V3;
18+
using Google.Cloud.SecretManager.V1;
19+
using System;
20+
using Xunit;
21+
22+
[Collection(nameof(SecretManagerFixture))]
23+
public class CreateSecretWithTagsTests
24+
{
25+
private readonly SecretManagerFixture _fixture;
26+
private readonly CreateSecretWithTagsSample _sample;
27+
private readonly TagKeysClient _tagKeysClient;
28+
private readonly TagValuesClient _tagValuesClient;
29+
private string _tagKeyName;
30+
private string _tagValueName;
31+
32+
public CreateSecretWithTagsTests(SecretManagerFixture fixture)
33+
{
34+
_fixture = fixture;
35+
_sample = new CreateSecretWithTagsSample();
36+
_tagKeysClient = TagKeysClient.Create();
37+
_tagValuesClient = TagValuesClient.Create();
38+
}
39+
40+
private void CreateTagKeyAndValue(string projectId, string tagKeyShortName, string tagValueShortName)
41+
{
42+
var createKeyRequest = new CreateTagKeyRequest
43+
{
44+
TagKey = new TagKey
45+
{
46+
Parent = $"projects/{projectId}",
47+
ShortName = tagKeyShortName,
48+
}
49+
};
50+
51+
try
52+
{
53+
var createKeyOperation = _tagKeysClient.CreateTagKey(createKeyRequest);
54+
TagKey tagKey = createKeyOperation.PollUntilCompleted().Result;
55+
_tagKeyName = tagKey.Name;
56+
}
57+
catch (Exception e)
58+
{
59+
throw new Exception($"Error creating tag key: {e.Message}");
60+
}
61+
62+
var createValueRequest = new CreateTagValueRequest
63+
{
64+
TagValue = new TagValue
65+
{
66+
Parent = _tagKeyName,
67+
ShortName = tagValueShortName,
68+
}
69+
};
70+
71+
try
72+
{
73+
var createValueOperation = _tagValuesClient.CreateTagValue(createValueRequest);
74+
TagValue tagValue = createValueOperation.PollUntilCompleted().Result;
75+
_tagValueName = tagValue.Name;
76+
}
77+
catch (Exception e)
78+
{
79+
throw new Exception($"Error creating tag value: {e.Message}");
80+
}
81+
}
82+
83+
private void CleanTagKeyandValue()
84+
{
85+
if (!string.IsNullOrEmpty(_tagValueName))
86+
{
87+
try
88+
{
89+
DeleteTagValueRequest deleteValueRequest = new DeleteTagValueRequest { Name = _tagValueName };
90+
_tagValuesClient.DeleteTagValue(deleteValueRequest).PollUntilCompleted();
91+
}
92+
catch (Exception e)
93+
{
94+
Console.WriteLine("Deleting the tag value failed!");
95+
throw new Exception($"Error deleting tag value: {e.Message}");
96+
}
97+
}
98+
else
99+
{
100+
Console.WriteLine("TagValue.name was not set, skipping deletion.");
101+
}
102+
103+
if (!string.IsNullOrEmpty(_tagKeyName))
104+
{
105+
try
106+
{
107+
DeleteTagKeyRequest deleteKeyRequest = new DeleteTagKeyRequest { Name = _tagKeyName };
108+
_tagKeysClient.DeleteTagKey(deleteKeyRequest).PollUntilCompleted();
109+
}
110+
catch (Exception e)
111+
{
112+
Console.Error.WriteLine("Deleting the tag key failed!");
113+
throw new Exception($"Error deleting tag key: {e.Message}");
114+
}
115+
}
116+
else
117+
{
118+
Console.WriteLine("TagKey.name was not set, skipping deletion.");
119+
}
120+
}
121+
122+
[Fact]
123+
public void CreatesSecretsWithTags()
124+
{
125+
// Get the SecretName to create Secret.
126+
SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId());
127+
128+
// Define tag key and value short names
129+
string tagKeyShortName = $"test-key-{_fixture.RandomId()}";
130+
string tagValueShortName = $"test-value-{_fixture.RandomId()}";
131+
132+
CreateTagKeyAndValue(_fixture.ProjectId, tagKeyShortName, tagValueShortName);
133+
134+
// Create the secret with the specificed fields.
135+
Secret result = _sample.CreateSecretWithTags(
136+
projectId: secretName.ProjectId, secretId: secretName.SecretId, tagKeyName: _tagKeyName, tagValueName: _tagValueName);
137+
138+
// Assert that the secretId is same as expected.
139+
Assert.Equal(result.SecretName.SecretId, secretName.SecretId);
140+
141+
_fixture.DeleteSecret(secretName);
142+
CleanTagKeyandValue();
143+
}
144+
}

secretmanager/api/SecretManager.Samples.Tests/SecretManager.Samples.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="Google.Cloud.ResourceManager.V3" Version="2.5.0" />
89
<PackageReference Include="JUnitTestLogger" Version="1.1.0" />
910
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
1011
<PackageReference Include="xunit" Version="2.9.3" />
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_tags]
18+
19+
using Google.Api.Gax.ResourceNames;
20+
using Google.Cloud.SecretManager.V1;
21+
using System.Collections.Generic;
22+
23+
public class CreateRegionalSecretWithTagsSample
24+
{
25+
public Secret CreateRegionalSecretWithTags(
26+
string projectId = "my-project",
27+
string locationId = "my-location",
28+
string secretId = "my-secret",
29+
string tagKeyName = "tagKey/value",
30+
string tagValueName = "tagValue/value"
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+
Tags =
46+
{
47+
{ tagKeyName, tagValueName }
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_tags]

0 commit comments

Comments
 (0)