Skip to content

Commit 392008c

Browse files
feat(modelarmor): Added samples for viewing, listing and deleting templates (#3085)
1 parent 63e90c5 commit 392008c

File tree

8 files changed

+446
-0
lines changed

8 files changed

+446
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 Grpc.Core;
19+
using Xunit;
20+
21+
public class DeleteTemplateTests : IClassFixture<ModelArmorFixture>
22+
{
23+
private readonly ModelArmorFixture _fixture;
24+
private readonly CreateTemplateSample _create_template_sample;
25+
private readonly DeleteTemplateSample _delete_template_sample;
26+
27+
public DeleteTemplateTests(ModelArmorFixture fixture)
28+
{
29+
_fixture = fixture;
30+
_create_template_sample = new CreateTemplateSample();
31+
_delete_template_sample = new DeleteTemplateSample();
32+
}
33+
34+
[Fact]
35+
public void DeleteTemplateTest()
36+
{
37+
// Create a template for testing purpose.
38+
TemplateName templateName = _fixture.CreateTemplateName();
39+
Template createdTemplate = _create_template_sample.CreateTemplate(
40+
projectId: _fixture.ProjectId,
41+
locationId: _fixture.LocationId,
42+
templateId: templateName.TemplateId
43+
);
44+
45+
// Delete the template using the sample.
46+
_delete_template_sample.DeleteTemplate(
47+
projectId: _fixture.ProjectId,
48+
locationId: _fixture.LocationId,
49+
templateId: templateName.TemplateId
50+
);
51+
52+
// Try to get the template directly - should throw a "not found" exception.
53+
var exception = Assert.Throws<RpcException>(() =>
54+
{
55+
var getTemplateSample = new GetTemplateSample();
56+
getTemplateSample.GetTemplate(
57+
projectId: _fixture.ProjectId,
58+
locationId: _fixture.LocationId,
59+
templateId: templateName.TemplateId
60+
);
61+
});
62+
63+
Assert.Equal(StatusCode.NotFound, exception.StatusCode);
64+
}
65+
66+
[Fact]
67+
public void DeleteTemplateTest_NonExistentTemplate()
68+
{
69+
// Generate a random template ID that doesn't exist.
70+
string nonExistentTemplateId = $"non-existent-{_fixture.GenerateUniqueId()}";
71+
72+
// Attempt to delete a non-existent template should throw an exception.
73+
var exception = Assert.Throws<RpcException>(() =>
74+
_delete_template_sample.DeleteTemplate(
75+
projectId: _fixture.ProjectId,
76+
locationId: _fixture.LocationId,
77+
templateId: nonExistentTemplateId
78+
)
79+
);
80+
81+
// Verify the exception contains the expected error code.
82+
Assert.Equal(StatusCode.NotFound, exception.StatusCode);
83+
}
84+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 GetTemplateTests : IClassFixture<ModelArmorFixture>
21+
{
22+
private readonly ModelArmorFixture _fixture;
23+
private readonly CreateTemplateSample _create_template_sample;
24+
private readonly GetTemplateSample _get_template_sample;
25+
private readonly (RaiFilterType, DetectionConfidenceLevel)[] expectedFilters =
26+
[
27+
(RaiFilterType.Dangerous, DetectionConfidenceLevel.High),
28+
(RaiFilterType.HateSpeech, DetectionConfidenceLevel.High),
29+
(RaiFilterType.Harassment, DetectionConfidenceLevel.MediumAndAbove),
30+
(RaiFilterType.SexuallyExplicit, DetectionConfidenceLevel.LowAndAbove),
31+
];
32+
33+
public GetTemplateTests(ModelArmorFixture fixture)
34+
{
35+
_fixture = fixture;
36+
_create_template_sample = new CreateTemplateSample();
37+
_get_template_sample = new GetTemplateSample();
38+
}
39+
40+
[Fact]
41+
public void GetTemplateTest()
42+
{
43+
// Create a template for testing purpose.
44+
TemplateName templateName = _fixture.CreateTemplateName();
45+
_fixture.RegisterTemplateForCleanup(templateName);
46+
47+
Template createdTemplate = _create_template_sample.CreateTemplate(
48+
projectId: _fixture.ProjectId,
49+
locationId: _fixture.LocationId,
50+
templateId: templateName.TemplateId
51+
);
52+
53+
Template retrievedTemplate = _get_template_sample.GetTemplate(
54+
projectId: _fixture.ProjectId,
55+
locationId: _fixture.LocationId,
56+
templateId: templateName.TemplateId
57+
);
58+
59+
var retrievedRaiFilters = retrievedTemplate.FilterConfig.RaiSettings.RaiFilters;
60+
61+
foreach (var (type, confidence) in expectedFilters)
62+
{
63+
Assert.Contains(retrievedRaiFilters, f => f.FilterType == type);
64+
Assert.Contains(
65+
retrievedRaiFilters,
66+
f => f.FilterType == type && f.ConfidenceLevel == confidence
67+
);
68+
}
69+
}
70+
}
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+
using System.Collections.Generic;
18+
using System.Linq;
19+
using Google.Cloud.ModelArmor.V1;
20+
using Xunit;
21+
22+
public class ListTemplatesTests : IClassFixture<ModelArmorFixture>
23+
{
24+
private readonly ModelArmorFixture _fixture;
25+
private readonly CreateTemplateSample _create_template_sample;
26+
private readonly ListTemplatesSample _list_templates_sample;
27+
28+
public ListTemplatesTests(ModelArmorFixture fixture)
29+
{
30+
_fixture = fixture;
31+
_create_template_sample = new CreateTemplateSample();
32+
_list_templates_sample = new ListTemplatesSample();
33+
}
34+
35+
[Fact]
36+
public void ListTemplatesTest()
37+
{
38+
// Create a template for testing purpose.
39+
TemplateName templateName = _fixture.CreateTemplateName();
40+
_fixture.RegisterTemplateForCleanup(templateName);
41+
42+
Template createdTemplate = _create_template_sample.CreateTemplate(
43+
projectId: _fixture.ProjectId,
44+
locationId: _fixture.LocationId,
45+
templateId: templateName.TemplateId
46+
);
47+
48+
IEnumerable<Template> templates = _list_templates_sample.ListTemplates(
49+
projectId: _fixture.ProjectId,
50+
locationId: _fixture.LocationId
51+
);
52+
53+
Assert.NotNull(templates);
54+
Assert.Contains(templates, template => template.Name == templateName.ToString());
55+
}
56+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,7 @@ public void Runs()
4242
locationId: templateName.LocationId,
4343
templateId: templateName.TemplateId
4444
);
45+
46+
_fixture.RegisterTemplateForCleanup(templateName);
4547
}
4648
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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]
18+
using System.Collections.Generic;
19+
using Google.Api.Gax.ResourceNames;
20+
using Google.Cloud.ModelArmor.V1;
21+
22+
public class CreateTemplateSample
23+
{
24+
public Template CreateTemplate(
25+
string projectId = "my-project",
26+
string locationId = "us-central1",
27+
string templateId = "my-template"
28+
)
29+
{
30+
ModelArmorClient client = new ModelArmorClientBuilder
31+
{
32+
Endpoint = $"modelarmor.{locationId}.rep.googleapis.com",
33+
}.Build();
34+
35+
// Build the Model Armor template with your preferred filters.
36+
// For more details on filters, please refer to the following doc:
37+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
38+
39+
// Configure Responsible AI filter with multiple categories and their confidence
40+
// levels.
41+
RaiFilterSettings raiFilterSettings = new RaiFilterSettings();
42+
List<RaiFilterSettings.Types.RaiFilter> filters =
43+
new List<RaiFilterSettings.Types.RaiFilter>
44+
{
45+
new RaiFilterSettings.Types.RaiFilter
46+
{
47+
FilterType = RaiFilterType.Dangerous,
48+
ConfidenceLevel = DetectionConfidenceLevel.High,
49+
},
50+
new RaiFilterSettings.Types.RaiFilter
51+
{
52+
FilterType = RaiFilterType.HateSpeech,
53+
ConfidenceLevel = DetectionConfidenceLevel.High,
54+
},
55+
new RaiFilterSettings.Types.RaiFilter
56+
{
57+
FilterType = RaiFilterType.SexuallyExplicit,
58+
ConfidenceLevel = DetectionConfidenceLevel.LowAndAbove,
59+
},
60+
new RaiFilterSettings.Types.RaiFilter
61+
{
62+
FilterType = RaiFilterType.Harassment,
63+
ConfidenceLevel = DetectionConfidenceLevel.MediumAndAbove,
64+
},
65+
};
66+
67+
raiFilterSettings.RaiFilters.Add(filters);
68+
69+
Template template = new Template
70+
{
71+
FilterConfig = new FilterConfig { RaiSettings = raiFilterSettings },
72+
};
73+
74+
CreateTemplateRequest request = new CreateTemplateRequest
75+
{
76+
ParentAsLocationName = LocationName.FromProjectLocation(projectId, locationId),
77+
TemplateId = templateId,
78+
Template = template,
79+
};
80+
81+
Template createdTemplate = client.CreateTemplate(request);
82+
System.Console.WriteLine($"Created template: {createdTemplate.Name}");
83+
84+
return createdTemplate;
85+
}
86+
}
87+
// [END modelarmor_create_template]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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_delete_template]
18+
using System;
19+
using Google.Cloud.ModelArmor.V1;
20+
21+
public class DeleteTemplateSample
22+
{
23+
public void DeleteTemplate(
24+
string projectId = "my-project",
25+
string locationId = "us-central1",
26+
string templateId = "my-template"
27+
)
28+
{
29+
ModelArmorClient client = new ModelArmorClientBuilder
30+
{
31+
Endpoint = $"modelarmor.{locationId}.rep.googleapis.com",
32+
}.Build();
33+
34+
DeleteTemplateRequest request = new DeleteTemplateRequest
35+
{
36+
TemplateName = TemplateName.FromProjectLocationTemplate(
37+
projectId,
38+
locationId,
39+
templateId
40+
),
41+
};
42+
43+
client.DeleteTemplate(request);
44+
Console.WriteLine($"Deleted template: {templateId}");
45+
}
46+
}
47+
// [END modelarmor_delete_template]

0 commit comments

Comments
 (0)