Skip to content

Commit aa9e5b1

Browse files
feat(modelarmor): Add samples for updating templates (#3084)
Added Model Armor Snippets: - Update Template - Update Template with labels - Update Template with metadata
1 parent 055b7cd commit aa9e5b1

22 files changed

+500
-54
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
using Google.Cloud.ModelArmor.V1;
1718
using System;
1819
using System.Collections.Generic;
19-
using Google.Cloud.ModelArmor.V1;
2020
using Xunit;
2121
using Xunit.Abstractions;
2222

@@ -43,12 +43,7 @@ public void CreateTemplateWithLabels()
4343
_fixture.RegisterTemplateForCleanup(templateName);
4444
string templateId = templateName.TemplateId;
4545

46-
// Run the sample.
47-
Template createdTemplate = _sample.CreateTemplateWithLabels(
48-
projectId: projectId,
49-
locationId: locationId,
50-
templateId: templateId
51-
);
46+
Template createdTemplate = _sample.CreateTemplateWithLabels(projectId, locationId, templateId);
5247

5348
// Verify the template was created successfully.
5449
Assert.NotNull(createdTemplate);

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
using Google.Cloud.ModelArmor.V1;
1718
using System;
1819
using System.Linq;
19-
using Google.Cloud.ModelArmor.V1;
2020
using Xunit;
2121
using Xunit.Abstractions;
2222

@@ -43,14 +43,8 @@ public void CreateTemplateWithMetadata()
4343
_fixture.RegisterTemplateForCleanup(templateName);
4444
string templateId = templateName.TemplateId;
4545

46-
// Run the sample.
47-
Template createdTemplate = _sample.CreateTemplateWithMetadata(
48-
projectId: projectId,
49-
locationId: locationId,
50-
templateId: templateId
51-
);
46+
Template createdTemplate = _sample.CreateTemplateWithMetadata(projectId, locationId, templateId);
5247

53-
// Verify the expected template metadata.
5448
Assert.NotNull(createdTemplate.TemplateMetadata);
5549
Assert.True(createdTemplate.TemplateMetadata.LogTemplateOperations);
5650
Assert.True(createdTemplate.TemplateMetadata.LogSanitizeOperations);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
using System.Collections.Generic;
1817
using Google.Cloud.ModelArmor.V1;
18+
using System.Collections.Generic;
1919
using Xunit;
2020

2121
public class ListTemplatesTests : IClassFixture<ModelArmorFixture>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
using System;
18-
using System.Collections.Generic;
1917
using Google.Api.Gax.ResourceNames;
2018
using Google.Cloud.Dlp.V2;
2119
using Google.Cloud.ModelArmor.V1;
20+
using System;
21+
using System.Collections.Generic;
2222
using Xunit;
2323

2424
[CollectionDefinition(nameof(ModelArmorFixture))]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
using System.IO;
1817
using Google.Cloud.ModelArmor.V1;
1918
using ModelArmor.Samples;
19+
using System.IO;
2020
using Xunit;
2121
using Xunit.Abstractions;
2222

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 System;
19+
using System.Collections.Generic;
20+
using Xunit;
21+
using Xunit.Abstractions;
22+
23+
namespace ModelArmor.Samples.Tests
24+
{
25+
public class UpdateTemplateTests : IClassFixture<ModelArmorFixture>
26+
{
27+
private readonly ModelArmorFixture _fixture;
28+
private readonly CreateTemplateSample _create_template_sample;
29+
private readonly UpdateTemplateSample _update_template_sample;
30+
31+
public UpdateTemplateTests(ModelArmorFixture fixture, ITestOutputHelper output)
32+
{
33+
_fixture = fixture;
34+
_create_template_sample = new CreateTemplateSample();
35+
_update_template_sample = new UpdateTemplateSample();
36+
}
37+
38+
[Fact]
39+
public void UpdateTemplateTest()
40+
{
41+
// Create a template.
42+
TemplateName templateName = _fixture.CreateTemplateName();
43+
_fixture.RegisterTemplateForCleanup(templateName);
44+
45+
Template originalTemplate = _create_template_sample.CreateTemplate(
46+
projectId: _fixture.ProjectId,
47+
locationId: _fixture.LocationId,
48+
templateId: templateName.TemplateId);
49+
50+
string templateId = templateName.TemplateId;
51+
52+
// Call the sample to modify the RAI filter of above created template.
53+
Template updatedTemplate = _update_template_sample.UpdateTemplate(
54+
projectId: _fixture.ProjectId,
55+
locationId: _fixture.LocationId,
56+
templateId: templateId);
57+
58+
// Assertions.
59+
var raiFilters = updatedTemplate.FilterConfig.RaiSettings.RaiFilters;
60+
Assert.Contains(
61+
raiFilters,
62+
f =>
63+
f.FilterType == RaiFilterType.Dangerous
64+
&& f.ConfidenceLevel == DetectionConfidenceLevel.High
65+
);
66+
67+
Assert.Contains(
68+
raiFilters,
69+
f =>
70+
f.FilterType == RaiFilterType.HateSpeech
71+
&& f.ConfidenceLevel == DetectionConfidenceLevel.MediumAndAbove
72+
);
73+
74+
Assert.Contains(
75+
raiFilters,
76+
f =>
77+
f.FilterType == RaiFilterType.Harassment
78+
&& f.ConfidenceLevel == DetectionConfidenceLevel.MediumAndAbove
79+
);
80+
81+
Assert.Contains(
82+
raiFilters,
83+
f =>
84+
f.FilterType == RaiFilterType.SexuallyExplicit
85+
&& f.ConfidenceLevel == DetectionConfidenceLevel.MediumAndAbove
86+
);
87+
}
88+
}
89+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
using Xunit.Abstractions;
20+
21+
namespace ModelArmor.Samples.Tests
22+
{
23+
public class UpdateTemplateWithLabelsTests : IClassFixture<ModelArmorFixture>
24+
{
25+
private readonly ModelArmorFixture _fixture;
26+
private readonly CreateTemplateSample _create_template_sample;
27+
private readonly GetTemplateSample _get_template_sample;
28+
private readonly UpdateTemplateWithLabelsSample _update_template_sample;
29+
30+
public UpdateTemplateWithLabelsTests(ModelArmorFixture fixture, ITestOutputHelper output)
31+
{
32+
_fixture = fixture;
33+
_create_template_sample = new CreateTemplateSample();
34+
_get_template_sample = new GetTemplateSample();
35+
_update_template_sample = new UpdateTemplateWithLabelsSample();
36+
}
37+
38+
[Fact]
39+
public void UpdateTemplateWithLabelsTest()
40+
{
41+
// Create a template.
42+
TemplateName templateName = _fixture.CreateTemplateName();
43+
_fixture.RegisterTemplateForCleanup(templateName);
44+
45+
Template originalTemplate = _create_template_sample.CreateTemplate(
46+
projectId: _fixture.ProjectId,
47+
locationId: _fixture.LocationId,
48+
templateId: templateName.TemplateId);
49+
50+
string templateId = templateName.TemplateId;
51+
52+
Template updatedTemplate = _update_template_sample.UpdateTemplateWithLabels(
53+
projectId: _fixture.ProjectId,
54+
locationId: _fixture.LocationId,
55+
templateId: templateId);
56+
57+
Assert.NotNull(updatedTemplate);
58+
Assert.Equal(originalTemplate.Name, updatedTemplate.Name);
59+
60+
// Get template details for assertion of labels.
61+
Template getUpdatedTemplate = _get_template_sample.GetTemplate(
62+
projectId: _fixture.ProjectId,
63+
locationId: _fixture.LocationId,
64+
templateId: templateName.TemplateId);
65+
66+
Assert.NotNull(getUpdatedTemplate.Labels);
67+
Assert.Contains(
68+
getUpdatedTemplate.Labels,
69+
l => l.Key == "updated_key1" && l.Value == "updated_value1"
70+
);
71+
Assert.Contains(
72+
getUpdatedTemplate.Labels,
73+
l => l.Key == "updated_key2" && l.Value == "updated_value2"
74+
);
75+
}
76+
}
77+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
using Xunit.Abstractions;
20+
21+
namespace ModelArmor.Samples.Tests
22+
{
23+
public class UpdateTemplateWithMetadataTests : IClassFixture<ModelArmorFixture>
24+
{
25+
private readonly ModelArmorFixture _fixture;
26+
private readonly CreateTemplateSample _create_template_sample;
27+
private readonly UpdateTemplateWithMetadataSample _update_template_sample;
28+
29+
public UpdateTemplateWithMetadataTests(ModelArmorFixture fixture, ITestOutputHelper output)
30+
{
31+
_fixture = fixture;
32+
_create_template_sample = new CreateTemplateSample();
33+
_update_template_sample = new UpdateTemplateWithMetadataSample();
34+
}
35+
36+
[Fact]
37+
public void UpdateTemplateWithMetadataTest()
38+
{
39+
// Create a template.
40+
TemplateName templateName = _fixture.CreateTemplateName();
41+
_fixture.RegisterTemplateForCleanup(templateName);
42+
43+
Template originalTemplate = _create_template_sample.CreateTemplate(
44+
projectId: _fixture.ProjectId,
45+
locationId: _fixture.LocationId,
46+
templateId: templateName.TemplateId);
47+
48+
string templateId = templateName.TemplateId;
49+
50+
Template updatedTemplate = _update_template_sample.UpdateTemplateWithMetadata(
51+
projectId: _fixture.ProjectId,
52+
locationId: _fixture.LocationId,
53+
templateId: templateId);
54+
55+
Assert.NotNull(updatedTemplate);
56+
Assert.Equal(originalTemplate.Name, updatedTemplate.Name);
57+
Assert.NotNull(updatedTemplate.TemplateMetadata);
58+
Assert.True(updatedTemplate.TemplateMetadata.LogTemplateOperations);
59+
Assert.True(updatedTemplate.TemplateMetadata.LogSanitizeOperations);
60+
}
61+
}
62+
}

modelarmor/api/ModelArmor.Samples/CreateTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616

1717
// [START modelarmor_create_template]
18-
using System.Collections.Generic;
1918
using Google.Api.Gax.ResourceNames;
2019
using Google.Cloud.ModelArmor.V1;
20+
using System.Collections.Generic;
2121

2222
public class CreateTemplateSample
2323
{

modelarmor/api/ModelArmor.Samples/CreateTemplateWithAdvancedSdp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616

1717
// [START modelarmor_create_template_with_advanced_sdp]
18-
using System;
1918
using Google.Api.Gax.ResourceNames;
2019
using Google.Cloud.ModelArmor.V1;
20+
using System;
2121

2222
public class CreateTemplateWithAdvancedSdpSample
2323
{

0 commit comments

Comments
 (0)