Skip to content

Commit d4f19f2

Browse files
feat(modelarmor): Added snippets for create template with advance sdp filter (#5275)
* feat(modelarmor): Added samples for creating, listing, updating and deleting model armor templates * feat(modelarmor): Added samples for creating, listing, updating and deleting modelarmor templates * feat(modelarmor): resolve review comments * feat(modelarmor): added modelarmor code snippet to create template with advanced sdp * feat(modelarmor): resolve comments * feat(modelarmor): code refactored * feat(modelarmor): code refactor * feat(modelarmor): code refactor * feat(modelarmor): resolve conflicts * feat(modelarmor): resolve conflicts * feat(modelarmor): resolve conflicts * feat(modelarmor): fix tests --------- Co-authored-by: Eric Schmidt <[email protected]>
1 parent 8c4c402 commit d4f19f2

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Sample code for creating a new model armor template with advanced SDP settings enabled.
16+
17+
package modelarmor
18+
19+
// [START modelarmor_create_template_with_advanced_sdp]
20+
21+
import (
22+
"context"
23+
"fmt"
24+
"io"
25+
26+
modelarmor "cloud.google.com/go/modelarmor/apiv1"
27+
modelarmorpb "cloud.google.com/go/modelarmor/apiv1/modelarmorpb"
28+
"google.golang.org/api/option"
29+
)
30+
31+
// createModelArmorTemplateWithAdvancedSDP method creates a
32+
// new Model Armor template with advanced SDP settings,
33+
// including inspect and deidentify templates.
34+
//
35+
// w io.Writer: The writer to use for logging.
36+
// projectID string: The ID of the Google Cloud project.
37+
// locationID string: The ID of the Google Cloud location.
38+
// templateID string: The ID of the template to create.
39+
// inspectTemplate string: The ID of the inspect template to use.
40+
// deidentifyTemplate string: The ID of the deidentify template to use.
41+
func createModelArmorTemplateWithAdvancedSDP(w io.Writer, projectID, locationID, templateID, inspectTemplate, deidentifyTemplate string) error {
42+
ctx := context.Background()
43+
44+
// Create options for Model Armor client.
45+
opts := option.WithEndpoint(fmt.Sprintf("modelarmor.%s.rep.googleapis.com:443", locationID))
46+
// Create the Model Armor client.
47+
client, err := modelarmor.NewClient(ctx, opts)
48+
if err != nil {
49+
return fmt.Errorf("failed to create client for project %s, location %s: %w", projectID, locationID, err)
50+
}
51+
defer client.Close()
52+
53+
parent := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)
54+
55+
// Build the Model Armor template with your preferred filters.
56+
template := &modelarmorpb.Template{
57+
FilterConfig: &modelarmorpb.FilterConfig{
58+
RaiSettings: &modelarmorpb.RaiFilterSettings{
59+
RaiFilters: []*modelarmorpb.RaiFilterSettings_RaiFilter{
60+
{
61+
FilterType: modelarmorpb.RaiFilterType_DANGEROUS,
62+
ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_HIGH,
63+
},
64+
{
65+
FilterType: modelarmorpb.RaiFilterType_HARASSMENT,
66+
ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_MEDIUM_AND_ABOVE,
67+
},
68+
{
69+
FilterType: modelarmorpb.RaiFilterType_HATE_SPEECH,
70+
ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_HIGH,
71+
},
72+
{
73+
FilterType: modelarmorpb.RaiFilterType_SEXUALLY_EXPLICIT,
74+
ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_HIGH,
75+
},
76+
},
77+
},
78+
SdpSettings: &modelarmorpb.SdpFilterSettings{
79+
SdpConfiguration: &modelarmorpb.SdpFilterSettings_AdvancedConfig{
80+
AdvancedConfig: &modelarmorpb.SdpAdvancedConfig{
81+
InspectTemplate: inspectTemplate,
82+
DeidentifyTemplate: deidentifyTemplate,
83+
},
84+
},
85+
},
86+
},
87+
}
88+
89+
// Prepare the request for creating the template.
90+
req := &modelarmorpb.CreateTemplateRequest{
91+
Parent: parent,
92+
TemplateId: templateID,
93+
Template: template,
94+
}
95+
96+
// Create the template.
97+
response, err := client.CreateTemplate(ctx, req)
98+
if err != nil {
99+
return fmt.Errorf("failed to create template: %w", err)
100+
}
101+
102+
// Print the new template name using fmt.Fprint with the io.Writer.
103+
fmt.Fprintf(w, "Created Template with advanced SDP: %s\n", response.Name)
104+
105+
return err
106+
}
107+
108+
// [END modelarmor_create_template_with_advanced_sdp]

modelarmor/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/GoogleCloudPlatform/golang-samples/modelarmor
33
go 1.23.0
44

55
require (
6+
cloud.google.com/go/dlp v1.21.0
67
cloud.google.com/go/modelarmor v0.1.0
78
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20250404170905-0aca11152736
89
github.com/google/uuid v1.6.0

modelarmor/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ cloud.google.com/go/auth/oauth2adapt v0.2.7 h1:/Lc7xODdqcEw8IrZ9SvwnlLX6j9FHQM74
88
cloud.google.com/go/auth/oauth2adapt v0.2.7/go.mod h1:NTbTTzfvPl1Y3V1nPpOgl2w6d/FjO7NNUQaWSox6ZMc=
99
cloud.google.com/go/compute/metadata v0.6.0 h1:A6hENjEsCDtC1k8byVsgwvVcioamEHvZ4j01OwKxG9I=
1010
cloud.google.com/go/compute/metadata v0.6.0/go.mod h1:FjyFAW1MW0C203CEOMDTu3Dk1FlqW3Rga40jzHL4hfg=
11+
cloud.google.com/go/dlp v1.21.0 h1:9kz7+gaB/0gBZsDUnNT1asDihNZSrRFSeUTBcBdUAkk=
12+
cloud.google.com/go/dlp v1.21.0/go.mod h1:Y9HOVtPoArpL9sI1O33aN/vK9QRwDERU9PEJJfM8DvE=
1113
cloud.google.com/go/iam v1.4.0 h1:ZNfy/TYfn2uh/ukvhp783WhnbVluqf/tzOaqVUPlIPA=
1214
cloud.google.com/go/iam v1.4.0/go.mod h1:gMBgqPaERlriaOV0CUl//XUzDhSfXevn4OEUbg6VRs4=
1315
cloud.google.com/go/logging v1.13.0 h1:7j0HgAp0B94o1YRDqiqm26w4q1rDMH7XNRU34lJXHYc=

modelarmor/modelarmor_test.go

+103
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import (
2121
"os"
2222
"strings"
2323
"testing"
24+
"time"
2425

26+
dlp "cloud.google.com/go/dlp/apiv2"
27+
"cloud.google.com/go/dlp/apiv2/dlppb"
2528
modelarmor "cloud.google.com/go/modelarmor/apiv1"
2629
modelarmorpb "cloud.google.com/go/modelarmor/apiv1/modelarmorpb"
2730

@@ -78,6 +81,106 @@ func testCleanupTemplate(t *testing.T, templateName string) {
7881
}
7982
}
8083

84+
// testSDPTemplate creates DLP inspect and deidentify templates for use in tests.
85+
func testSDPTemplate(t *testing.T, projectID string, locationID string) (string, string) {
86+
inspectTemplateID := fmt.Sprintf("model-armor-inspect-template-%s", uuid.New().String())
87+
deidentifyTemplateID := fmt.Sprintf("model-armor-deidentify-template-%s", uuid.New().String())
88+
apiEndpoint := fmt.Sprintf("dlp.%s.rep.googleapis.com:443", locationID)
89+
parent := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)
90+
91+
infoTypes := []*dlppb.InfoType{
92+
{Name: "EMAIL_ADDRESS"},
93+
{Name: "PHONE_NUMBER"},
94+
{Name: "US_INDIVIDUAL_TAXPAYER_IDENTIFICATION_NUMBER"},
95+
}
96+
97+
ctx := context.Background()
98+
dlpClient, err := dlp.NewClient(ctx, option.WithEndpoint(apiEndpoint))
99+
if err != nil {
100+
t.Fatalf("Getting error while creating the client: %v", err)
101+
}
102+
defer dlpClient.Close()
103+
104+
inspectRequest := &dlppb.CreateInspectTemplateRequest{
105+
Parent: parent,
106+
TemplateId: inspectTemplateID,
107+
InspectTemplate: &dlppb.InspectTemplate{
108+
InspectConfig: &dlppb.InspectConfig{
109+
InfoTypes: infoTypes,
110+
},
111+
},
112+
}
113+
inspectResponse, err := dlpClient.CreateInspectTemplate(ctx, inspectRequest)
114+
if err != nil {
115+
t.Fatal(err)
116+
}
117+
118+
deidentifyRequest := &dlppb.CreateDeidentifyTemplateRequest{
119+
Parent: parent,
120+
TemplateId: deidentifyTemplateID,
121+
DeidentifyTemplate: &dlppb.DeidentifyTemplate{
122+
DeidentifyConfig: &dlppb.DeidentifyConfig{
123+
Transformation: &dlppb.DeidentifyConfig_InfoTypeTransformations{
124+
InfoTypeTransformations: &dlppb.InfoTypeTransformations{
125+
Transformations: []*dlppb.InfoTypeTransformations_InfoTypeTransformation{
126+
{
127+
InfoTypes: []*dlppb.InfoType{},
128+
PrimitiveTransformation: &dlppb.PrimitiveTransformation{
129+
Transformation: &dlppb.PrimitiveTransformation_ReplaceConfig{
130+
ReplaceConfig: &dlppb.ReplaceValueConfig{
131+
NewValue: &dlppb.Value{
132+
Type: &dlppb.Value_StringValue{StringValue: "REDACTED"},
133+
},
134+
},
135+
},
136+
},
137+
},
138+
},
139+
},
140+
},
141+
},
142+
},
143+
}
144+
deidentifyResponse, err := dlpClient.CreateDeidentifyTemplate(ctx, deidentifyRequest)
145+
if err != nil {
146+
t.Fatal(err)
147+
}
148+
149+
// Cleanup the templates after test.
150+
defer func() {
151+
time.Sleep(5 * time.Second)
152+
err := dlpClient.DeleteInspectTemplate(ctx, &dlppb.DeleteInspectTemplateRequest{Name: inspectResponse.Name})
153+
if err != nil {
154+
t.Errorf("failed to delete inspect template: %v, err: %v", inspectResponse.Name, err)
155+
}
156+
err = dlpClient.DeleteDeidentifyTemplate(ctx, &dlppb.DeleteDeidentifyTemplateRequest{Name: deidentifyResponse.Name})
157+
if err != nil {
158+
t.Errorf("failed to delete deidentify template: %v, err: %v", deidentifyResponse.Name, err)
159+
}
160+
}()
161+
162+
return inspectResponse.Name, deidentifyResponse.Name
163+
}
164+
165+
// TestCreateModelArmorTemplateWithAdvancedSDP tests creating a
166+
// Model Armor template with advanced SDP using DLP templates.
167+
func TestCreateModelArmorTemplateWithAdvancedSDP(t *testing.T) {
168+
tc := testutil.SystemTest(t)
169+
170+
templateID := fmt.Sprintf("test-model-armor-%s", uuid.New().String())
171+
inspectTemplateName, deideintifyTemplateName := testSDPTemplate(t, tc.ProjectID, testLocation(t))
172+
templateName := fmt.Sprintf("projects/%s/locations/%s/templates/%s", tc.ProjectID, testLocation(t), templateID)
173+
var buf bytes.Buffer
174+
if err := createModelArmorTemplateWithAdvancedSDP(&buf, tc.ProjectID, testLocation(t), templateID, inspectTemplateName, deideintifyTemplateName); err != nil {
175+
t.Fatal(err)
176+
}
177+
defer testCleanupTemplate(t, templateName)
178+
179+
if got, want := buf.String(), "Created Template with advanced SDP: "; !strings.Contains(got, want) {
180+
t.Errorf("createModelArmorTemplateWithAdvancedSDP: expected %q to contain %q", got, want)
181+
}
182+
}
183+
81184
// TestCreateModelArmorTemplate verifies the creation of a Model Armor template.
82185
// It ensures the output contains a confirmation message after creation.
83186
func TestCreateModelArmorTemplate(t *testing.T) {

0 commit comments

Comments
 (0)