Skip to content

Commit efcf6f1

Browse files
feat(modelarmor): Added snippets for Create Template with labels and metadata (#3082)
Added Model Armor Snippets: 1. Create Template 2. Create Template with labels 3. Create Template with metadata
1 parent 1498eb3 commit efcf6f1

File tree

7 files changed

+424
-3
lines changed

7 files changed

+424
-3
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 CreateTemplateTests : IClassFixture<ModelArmorFixture>
24+
{
25+
private readonly ModelArmorFixture _fixture;
26+
private readonly CreateTemplateSample _sample;
27+
28+
public CreateTemplateTests(ModelArmorFixture fixture, ITestOutputHelper output)
29+
{
30+
_fixture = fixture;
31+
_sample = new CreateTemplateSample();
32+
}
33+
34+
[Fact]
35+
public void Runs()
36+
{
37+
TemplateName templateName = _fixture.CreateTemplateName();
38+
_fixture.RegisterTemplateForCleanup(templateName);
39+
string templateId = templateName.TemplateId;
40+
41+
// Run the sample.
42+
Template template = _sample.CreateTemplate(
43+
projectId: _fixture.ProjectId,
44+
locationId: _fixture.LocationId,
45+
templateId: templateId
46+
);
47+
48+
// Verify the template was created successfully.
49+
Assert.NotNull(template);
50+
Assert.Contains(templateId, template.Name);
51+
52+
// Verify the template has the expected filter configuration.
53+
Assert.NotNull(template.FilterConfig);
54+
Assert.NotNull(template.FilterConfig.RaiSettings);
55+
var raiFilters = template.FilterConfig.RaiSettings.RaiFilters;
56+
Assert.Equal(4, raiFilters.Count);
57+
58+
// Assert each filter exists with the expected configuration.
59+
Assert.Contains(
60+
raiFilters,
61+
f =>
62+
f.FilterType == RaiFilterType.Dangerous
63+
&& f.ConfidenceLevel == DetectionConfidenceLevel.High
64+
);
65+
66+
Assert.Contains(
67+
raiFilters,
68+
f =>
69+
f.FilterType == RaiFilterType.HateSpeech
70+
&& f.ConfidenceLevel == DetectionConfidenceLevel.High
71+
);
72+
73+
Assert.Contains(
74+
raiFilters,
75+
f =>
76+
f.FilterType == RaiFilterType.SexuallyExplicit
77+
&& f.ConfidenceLevel == DetectionConfidenceLevel.LowAndAbove
78+
);
79+
80+
Assert.Contains(
81+
raiFilters,
82+
f =>
83+
f.FilterType == RaiFilterType.Harassment
84+
&& f.ConfidenceLevel == DetectionConfidenceLevel.MediumAndAbove
85+
);
86+
}
87+
}
88+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 System;
18+
using System.Collections.Generic;
19+
using Google.Cloud.ModelArmor.V1;
20+
using Xunit;
21+
using Xunit.Abstractions;
22+
23+
namespace ModelArmor.Samples.Tests
24+
{
25+
public class CreateTemplateWithLabelsTests : IClassFixture<ModelArmorFixture>
26+
{
27+
private readonly ModelArmorFixture _fixture;
28+
private readonly CreateTemplateWithLabelsSample _sample;
29+
30+
public CreateTemplateWithLabelsTests(ModelArmorFixture fixture, ITestOutputHelper output)
31+
{
32+
_fixture = fixture;
33+
_sample = new CreateTemplateWithLabelsSample();
34+
}
35+
36+
[Fact]
37+
public void CreateTemplateWithLabels()
38+
{
39+
string projectId = _fixture.ProjectId;
40+
string locationId = _fixture.LocationId;
41+
42+
TemplateName templateName = _fixture.CreateTemplateName();
43+
_fixture.RegisterTemplateForCleanup(templateName);
44+
string templateId = templateName.TemplateId;
45+
46+
// Run the sample.
47+
Template createdTemplate = _sample.CreateTemplateWithLabels(
48+
projectId: projectId,
49+
locationId: locationId,
50+
templateId: templateId
51+
);
52+
53+
// Verify the template was created successfully.
54+
Assert.NotNull(createdTemplate);
55+
Assert.Contains(templateId, createdTemplate.Name);
56+
57+
// Since labels is not a part of the Create Template API response,
58+
// we retrieve the template to verify labels.
59+
ModelArmorClientBuilder clientBuilder = new ModelArmorClientBuilder
60+
{
61+
Endpoint = $"modelarmor.{locationId}.rep.googleapis.com",
62+
};
63+
ModelArmorClient client = clientBuilder.Build();
64+
Template retrievedTemplate = client.GetTemplate(createdTemplate.Name);
65+
66+
// Verify the labels.
67+
Assert.NotNull(retrievedTemplate.Labels);
68+
Assert.Equal(2, retrievedTemplate.Labels.Count);
69+
Assert.Equal("value1", retrievedTemplate.Labels["key1"]);
70+
Assert.Equal("value2", retrievedTemplate.Labels["key2"]);
71+
}
72+
}
73+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 System;
18+
using System.Linq;
19+
using Google.Cloud.ModelArmor.V1;
20+
using Xunit;
21+
using Xunit.Abstractions;
22+
23+
namespace ModelArmor.Samples.Tests
24+
{
25+
public class CreateTemplateWithMetadataTests : IClassFixture<ModelArmorFixture>
26+
{
27+
private readonly ModelArmorFixture _fixture;
28+
private readonly CreateTemplateWithMetadataSample _sample;
29+
30+
public CreateTemplateWithMetadataTests(ModelArmorFixture fixture, ITestOutputHelper output)
31+
{
32+
_fixture = fixture;
33+
_sample = new CreateTemplateWithMetadataSample();
34+
}
35+
36+
[Fact]
37+
public void CreateTemplateWithMetadata()
38+
{
39+
string projectId = _fixture.ProjectId;
40+
string locationId = _fixture.LocationId;
41+
42+
TemplateName templateName = _fixture.CreateTemplateName();
43+
_fixture.RegisterTemplateForCleanup(templateName);
44+
string templateId = templateName.TemplateId;
45+
46+
// Run the sample.
47+
Template createdTemplate = _sample.CreateTemplateWithMetadata(
48+
projectId: projectId,
49+
locationId: locationId,
50+
templateId: templateId
51+
);
52+
53+
// Verify the expected template metadata.
54+
Assert.NotNull(createdTemplate.TemplateMetadata);
55+
Assert.True(createdTemplate.TemplateMetadata.LogTemplateOperations);
56+
Assert.True(createdTemplate.TemplateMetadata.LogSanitizeOperations);
57+
}
58+
}
59+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,5 @@ public void Runs()
4242
locationId: templateName.LocationId,
4343
templateId: templateName.TemplateId
4444
);
45-
46-
_fixture.RegisterTemplateForCleanup(templateName);
4745
}
4846
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 modelarmor_create_template_with_labels]
18+
using System;
19+
using System.Collections.Generic;
20+
using Google.Api.Gax.ResourceNames;
21+
using Google.Cloud.ModelArmor.V1;
22+
using Google.Protobuf.Collections;
23+
24+
namespace ModelArmor.Samples
25+
{
26+
public class CreateTemplateWithLabelsSample
27+
{
28+
public Template CreateTemplateWithLabels(
29+
string projectId = "my-project",
30+
string locationId = "us-central1",
31+
string templateId = "my-template"
32+
)
33+
{
34+
// Construct the API endpoint URL.
35+
ModelArmorClientBuilder clientBuilder = new ModelArmorClientBuilder
36+
{
37+
Endpoint = $"modelarmor.{locationId}.rep.googleapis.com",
38+
};
39+
40+
// Create the client.
41+
Google.Cloud.ModelArmor.V1.ModelArmorClient client = clientBuilder.Build();
42+
43+
// Build the parent resource name.
44+
LocationName parent = LocationName.FromProjectLocation(projectId, locationId);
45+
46+
// Build the Model Armor template with preferred filters.
47+
// For more details on filters, refer to:
48+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
49+
RaiFilterSettings raiFilterSettings = new RaiFilterSettings();
50+
raiFilterSettings.RaiFilters.Add(
51+
new RaiFilterSettings.Types.RaiFilter
52+
{
53+
FilterType = RaiFilterType.Dangerous,
54+
ConfidenceLevel = DetectionConfidenceLevel.High,
55+
}
56+
);
57+
raiFilterSettings.RaiFilters.Add(
58+
new RaiFilterSettings.Types.RaiFilter
59+
{
60+
FilterType = RaiFilterType.HateSpeech,
61+
ConfidenceLevel = DetectionConfidenceLevel.High,
62+
}
63+
);
64+
raiFilterSettings.RaiFilters.Add(
65+
new RaiFilterSettings.Types.RaiFilter
66+
{
67+
FilterType = RaiFilterType.SexuallyExplicit,
68+
ConfidenceLevel = DetectionConfidenceLevel.LowAndAbove,
69+
}
70+
);
71+
raiFilterSettings.RaiFilters.Add(
72+
new RaiFilterSettings.Types.RaiFilter
73+
{
74+
FilterType = RaiFilterType.Harassment,
75+
ConfidenceLevel = DetectionConfidenceLevel.MediumAndAbove,
76+
}
77+
);
78+
79+
FilterConfig modelArmorFilter = new FilterConfig { RaiSettings = raiFilterSettings };
80+
81+
Template template = new Template { FilterConfig = modelArmorFilter };
82+
83+
template.Labels.Add("key1", "value1");
84+
template.Labels.Add("key2", "value2");
85+
86+
CreateTemplateRequest request = new CreateTemplateRequest
87+
{
88+
ParentAsLocationName = parent,
89+
TemplateId = templateId,
90+
Template = template,
91+
};
92+
93+
Template createdTemplate = client.CreateTemplate(request);
94+
return createdTemplate;
95+
}
96+
}
97+
}
98+
// [END modelarmor_create_template_with_labels]

0 commit comments

Comments
 (0)