Skip to content

Commit 5e1b856

Browse files
feat(modelarmor): Added snippets to create template with basic and advanced SDP (#3083)
1 parent e890798 commit 5e1b856

File tree

8 files changed

+457
-68
lines changed

8 files changed

+457
-68
lines changed
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+
* 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.ModelArmor.V1;
18+
using Xunit;
19+
20+
public class CreateTemplateWithAdvancedSdpTests : IClassFixture<ModelArmorFixture>
21+
{
22+
private readonly ModelArmorFixture _fixture;
23+
private readonly CreateTemplateWithAdvancedSdpSample _sample;
24+
25+
public CreateTemplateWithAdvancedSdpTests(ModelArmorFixture fixture)
26+
{
27+
_fixture = fixture;
28+
_sample = new CreateTemplateWithAdvancedSdpSample();
29+
}
30+
31+
[Fact]
32+
public void CreateTemplateWithAdvancedSdpTest()
33+
{
34+
string projectId = _fixture.ProjectId;
35+
string locationId = _fixture.LocationId;
36+
37+
TemplateName templateName = _fixture.CreateTemplateName();
38+
_fixture.RegisterTemplateForCleanup(templateName);
39+
string templateId = templateName.TemplateId;
40+
41+
string inspectTemplateName = _fixture.CreateInspectTemplate();
42+
43+
string deidentifyTemplateName = _fixture.CreateDeidentifyTemplate();
44+
45+
// Run the sample.
46+
Template template = _sample.CreateTemplateWithAdvancedSdp(
47+
projectId: projectId,
48+
locationId: locationId,
49+
templateId: templateId,
50+
inspectTemplateName: inspectTemplateName,
51+
deidentifyTemplateName: deidentifyTemplateName
52+
);
53+
54+
// Verify that template was created successfully.
55+
Assert.NotNull(template);
56+
Assert.Contains(templateId, template.Name);
57+
58+
// Verify that created template has the expected filter configuration.
59+
Assert.NotNull(template.FilterConfig);
60+
Assert.NotNull(template.FilterConfig.SdpSettings);
61+
Assert.NotNull(template.FilterConfig.SdpSettings.AdvancedConfig);
62+
63+
// Verify the advanced SDP configuration of the created template.
64+
var advancedConfig = template.FilterConfig.SdpSettings.AdvancedConfig;
65+
Assert.Equal(inspectTemplateName, advancedConfig.InspectTemplate);
66+
Assert.Equal(deidentifyTemplateName, advancedConfig.DeidentifyTemplate);
67+
}
68+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.ModelArmor.V1;
18+
using Xunit;
19+
20+
public class CreateTemplateWithBasicSdpTests : IClassFixture<ModelArmorFixture>
21+
{
22+
private readonly ModelArmorFixture _fixture;
23+
private readonly CreateTemplateWithBasicSdpSample _sample;
24+
25+
public CreateTemplateWithBasicSdpTests(ModelArmorFixture fixture)
26+
{
27+
_fixture = fixture;
28+
_sample = new CreateTemplateWithBasicSdpSample();
29+
}
30+
31+
[Fact]
32+
public void CreateTemplateWithBasicSdpTest()
33+
{
34+
string projectId = _fixture.ProjectId;
35+
string locationId = _fixture.LocationId;
36+
37+
TemplateName templateName = _fixture.CreateTemplateName();
38+
_fixture.RegisterTemplateForCleanup(templateName);
39+
string templateId = templateName.TemplateId;
40+
41+
// Run the sample.
42+
Template template = _sample.CreateTemplateWithBasicSdp(
43+
projectId: projectId,
44+
locationId: locationId,
45+
templateId: templateId
46+
);
47+
48+
// Verify that template was created successfully.
49+
Assert.NotNull(template);
50+
Assert.Contains(templateId, template.Name);
51+
52+
// Verify that created template has the expected filter configuration.
53+
Assert.NotNull(template.FilterConfig);
54+
Assert.NotNull(template.FilterConfig.SdpSettings);
55+
Assert.NotNull(template.FilterConfig.SdpSettings.BasicConfig);
56+
Assert.Equal(
57+
SdpBasicConfig.Types.SdpBasicConfigEnforcement.Enabled,
58+
template.FilterConfig.SdpSettings.BasicConfig.FilterEnforcement
59+
);
60+
}
61+
}

modelarmor/api/ModelArmor.Samples.Tests/ModelArmor.Samples.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<IsPackable>false</IsPackable>
55
</PropertyGroup>
66
<ItemGroup>
7+
<PackageReference Include="Google.Cloud.Dlp.V2" Version="4.18.0" />
78
<PackageReference Include="Google.Cloud.ModelArmor.V1" Version="1.0.0-beta01" />
89
<PackageReference Include="JunitXml.TestLogger" Version="1.1.0" />
910
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />

modelarmor/api/ModelArmor.Samples.Tests/ModelArmorFixture.cs

Lines changed: 150 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,181 @@
1515
*/
1616

1717
using System;
18+
using System.Collections.Generic;
19+
using Google.Api.Gax.ResourceNames;
20+
using Google.Cloud.Dlp.V2;
1821
using Google.Cloud.ModelArmor.V1;
1922
using Xunit;
2023

2124
[CollectionDefinition(nameof(ModelArmorFixture))]
2225
public class ModelArmorFixture : IDisposable, ICollectionFixture<ModelArmorFixture>
2326
{
27+
// Environment variable names.
28+
private const string EnvProjectId = "GOOGLE_PROJECT_ID";
29+
private const string EnvLocation = "GOOGLE_CLOUD_LOCATION";
30+
2431
public ModelArmorClient Client { get; }
32+
public DlpServiceClient DlpClient { get; }
2533
public string ProjectId { get; }
2634
public string LocationId { get; }
27-
public TemplateName TemplateForQuickstartName { get; }
35+
private readonly List<TemplateName> _maTemplatesToCleanup = new List<TemplateName>();
36+
private readonly List<string> _dlpTemplatesToCleanup = new List<string>();
2837

2938
public ModelArmorFixture()
3039
{
31-
// Get the Google Cloud Project ID.
32-
ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
33-
if (string.IsNullOrEmpty(ProjectId))
40+
ProjectId = GetRequiredEnvVar(EnvProjectId);
41+
LocationId = Environment.GetEnvironmentVariable(EnvLocation) ?? "us-central1";
42+
43+
// Create the Model Armor client.
44+
ModelArmorClient Client = new ModelArmorClientBuilder
45+
{
46+
Endpoint = $"modelarmor.{LocationId}.rep.googleapis.com",
47+
}.Build();
48+
49+
// Create the DLP client.
50+
DlpClient = new DlpServiceClientBuilder { Endpoint = $"dlp.googleapis.com" }.Build();
51+
}
52+
53+
private string GetRequiredEnvVar(string name)
54+
{
55+
var value = Environment.GetEnvironmentVariable(name);
56+
if (string.IsNullOrEmpty(value))
3457
{
35-
throw new Exception("Missing GOOGLE_PROJECT_ID environment variable");
58+
throw new Exception($"Missing {name} environment variable");
3659
}
60+
return value;
61+
}
3762

38-
// Get location ID from environment variable or use default.
39-
LocationId = Environment.GetEnvironmentVariable("GOOGLE_CLOUD_LOCATION") ?? "us-central1";
63+
public string GenerateUniqueId()
64+
{
65+
return Guid.NewGuid().ToString("N").Substring(0, 8);
66+
}
4067

41-
// Create client.
42-
Client = ModelArmorClient.Create();
68+
// Creates a DLP Inspect Template and returns its name.
69+
public string CreateInspectTemplate(string displayName = "Test Inspect Template")
70+
{
71+
var parent = new LocationName(ProjectId, LocationId).ToString();
72+
var templateId = $"inspect-{GenerateUniqueId()}";
73+
var request = new CreateInspectTemplateRequest
74+
{
75+
Parent = parent,
76+
InspectTemplate = new InspectTemplate
77+
{
78+
DisplayName = displayName,
79+
InspectConfig = new InspectConfig
80+
{
81+
InfoTypes =
82+
{
83+
new InfoType { Name = "PERSON_NAME" },
84+
new InfoType { Name = "EMAIL_ADDRESS" },
85+
new InfoType { Name = "US_INDIVIDUAL_TAXPAYER_IDENTIFICATION_NUMBER" },
86+
},
87+
},
88+
},
89+
TemplateId = templateId,
90+
};
91+
var response = DlpClient.CreateInspectTemplate(request);
92+
RegisterDlpTemplateForCleanup(response.Name);
93+
return response.Name;
94+
}
4395

44-
// Create a template name for quickstart test.
45-
string templateId = $"test-csharp-{Guid.NewGuid().ToString("N").Substring(0, 8)}";
46-
TemplateForQuickstartName = TemplateName.FromProjectLocationTemplate(
47-
ProjectId,
48-
LocationId,
49-
templateId
50-
);
96+
// Creates a DLP Deidentify Template and returns its name.
97+
public string CreateDeidentifyTemplate(string displayName = "Test Deidentify Template")
98+
{
99+
var parent = new LocationName(ProjectId, LocationId).ToString();
100+
var templateId = $"deidentify-{GenerateUniqueId()}";
101+
var request = new CreateDeidentifyTemplateRequest
102+
{
103+
Parent = parent,
104+
DeidentifyTemplate = new DeidentifyTemplate
105+
{
106+
DisplayName = displayName,
107+
DeidentifyConfig = new DeidentifyConfig
108+
{
109+
InfoTypeTransformations = new InfoTypeTransformations
110+
{
111+
Transformations =
112+
{
113+
new InfoTypeTransformations.Types.InfoTypeTransformation
114+
{
115+
PrimitiveTransformation = new PrimitiveTransformation
116+
{
117+
ReplaceConfig = new ReplaceValueConfig
118+
{
119+
NewValue = new Value { StringValue = "[REDACTED]" },
120+
},
121+
},
122+
},
123+
},
124+
},
125+
},
126+
},
127+
TemplateId = templateId,
128+
};
129+
var response = DlpClient.CreateDeidentifyTemplate(request);
130+
RegisterDlpTemplateForCleanup(response.Name);
131+
return response.Name;
51132
}
52133

53134
public void Dispose()
54135
{
55136
// Clean up resources after tests.
56-
try
137+
foreach (var resourceName in _maTemplatesToCleanup)
57138
{
58-
Client.DeleteTemplate(
59-
new DeleteTemplateRequest { Name = TemplateForQuickstartName.ToString() }
60-
);
139+
try
140+
{
141+
Client.DeleteTemplate(new DeleteTemplateRequest { TemplateName = resourceName });
142+
}
143+
catch (Exception)
144+
{
145+
// Ignore errors during cleanup.
146+
}
61147
}
62-
catch (Exception)
148+
149+
// Clean up DLP templates.
150+
foreach (var dlpTemplateName in _dlpTemplatesToCleanup)
151+
{
152+
try
153+
{
154+
if (dlpTemplateName.Contains("inspectTemplates/"))
155+
{
156+
DlpClient.DeleteInspectTemplate(
157+
new DeleteInspectTemplateRequest { Name = dlpTemplateName }
158+
);
159+
}
160+
else if (dlpTemplateName.Contains("deidentifyTemplates/"))
161+
{
162+
DlpClient.DeleteDeidentifyTemplate(
163+
new DeleteDeidentifyTemplateRequest { Name = dlpTemplateName }
164+
);
165+
}
166+
}
167+
catch (Exception)
168+
{
169+
// Ignore errors during cleanup.
170+
}
171+
}
172+
}
173+
174+
public void RegisterTemplateForCleanup(TemplateName templateName)
175+
{
176+
if (templateName != null && !string.IsNullOrEmpty(templateName.ToString()))
177+
{
178+
_maTemplatesToCleanup.Add(templateName);
179+
}
180+
}
181+
182+
public void RegisterDlpTemplateForCleanup(string templateName)
183+
{
184+
if (!string.IsNullOrEmpty(templateName))
63185
{
64-
// Ignore errors during cleanup.
186+
_dlpTemplatesToCleanup.Add(templateName);
65187
}
66188
}
189+
190+
public TemplateName CreateTemplateName(string prefix = "test-dotnet")
191+
{
192+
string templateId = $"{prefix}-{GenerateUniqueId()}";
193+
return new TemplateName(ProjectId, LocationId, templateId);
194+
}
67195
}

modelarmor/api/ModelArmor.Samples.Tests/QuickStartTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616

1717
using Google.Cloud.ModelArmor.V1;
1818
using Xunit;
19-
using Xunit.Abstractions;
2019

2120
[Collection(nameof(ModelArmorFixture))]
2221
public class QuickstartTests
2322
{
2423
private readonly ModelArmorFixture _fixture;
2524
private readonly QuickstartSample _sample;
2625

27-
public QuickstartTests(ModelArmorFixture fixture, ITestOutputHelper output)
26+
public QuickstartTests(ModelArmorFixture fixture)
2827
{
2928
_fixture = fixture;
3029
_sample = new QuickstartSample();
@@ -34,7 +33,8 @@ public QuickstartTests(ModelArmorFixture fixture, ITestOutputHelper output)
3433
public void Runs()
3534
{
3635
// Get template name from fixture
37-
TemplateName templateName = _fixture.TemplateForQuickstartName;
36+
TemplateName templateName = _fixture.CreateTemplateName();
37+
_fixture.RegisterTemplateForCleanup(templateName);
3838

3939
// Run the quickstart sample
4040
_sample.Quickstart(

0 commit comments

Comments
 (0)