Skip to content

feat(modelarmor): #14 Added snippets for update template with mask config #5280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d0b6026
feat(modelarmor): Added samples for creating, listing, updating and …
tirthrajsinh-zala-crest Apr 8, 2025
78110c9
feat(modelarmor): Added samples for creating, listing, updating and d…
tirthrajsinh-zala-crest Apr 9, 2025
a7b4cff
Merge branch 'main' into modelarmor-curd-snippets
tirthrajsinh-zala-crest Apr 9, 2025
4095b9a
feat(modelarmor): resolve review comments
tirthrajsinh-zala-crest Apr 9, 2025
caf8c7e
Merge branch 'main' into modelarmor-curd-snippets
tirthrajsinh-zala-crest Apr 9, 2025
1e5a58d
Merge branch 'main' into modelarmor-curd-snippets
tirthrajsinh-zala-crest Apr 10, 2025
0b76486
feat(modelarmor): enhancement
tirthrajsinh-zala-crest Apr 11, 2025
13a731a
feat(modelarmor): added code snippet for update modelarmor template w…
tirthrajsinh-zala-crest Apr 11, 2025
b45b0df
feat(modelarmor): added code snippet for update modelarmor template w…
tirthrajsinh-zala-crest Apr 11, 2025
70919ef
Merge branch 'main' into modelarmor-update-template-with-mask-config
tirthrajsinh-zala-crest Apr 11, 2025
607e43c
Merge branch 'main' into modelarmor-update-template-with-mask-config
tirthrajsinh-zala-crest Apr 14, 2025
c9d7f39
feat(modelarmor): code refactor
tirthrajsinh-zala-crest Apr 14, 2025
7524857
feat(modelarmor): code refactor
tirthrajsinh-zala-crest Apr 14, 2025
0cd21f7
Merge branch 'main' into modelarmor-update-template-with-mask-config
tirthrajsinh-zala-crest Apr 17, 2025
4ed4ad9
Merge branch 'main' into modelarmor-update-template-with-mask-config
tirthrajsinh-zala-crest Apr 18, 2025
1cac3f3
feat(modelarmor): resolve conflicts
tirthrajsinh-zala-crest Apr 18, 2025
63589d0
feat(modelarmor): resolve conflicts
tirthrajsinh-zala-crest Apr 18, 2025
17bc506
feat(modelarmor): resolve conflicts
tirthrajsinh-zala-crest Apr 18, 2025
44246a2
Merge branch 'main' into modelarmor-update-template-with-mask-config
tirthrajsinh-zala-crest Apr 23, 2025
4e92050
Merge branch 'main' into modelarmor-update-template-with-mask-config
tirthrajsinh-zala-crest Apr 24, 2025
683a55c
Merge branch 'main' into modelarmor-update-template-with-mask-config
tirthrajsinh-zala-crest Apr 25, 2025
1aeda80
feat(modelarmor): resolve conflicts
tirthrajsinh-zala-crest Apr 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modelarmor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/google/uuid v1.6.0
google.golang.org/api v0.226.0
google.golang.org/grpc v1.71.0
google.golang.org/protobuf v1.36.5
)

require (
Expand Down Expand Up @@ -53,5 +54,4 @@ require (
google.golang.org/genproto v0.0.0-20250313205543-e70fdf4c4cb4 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250313205543-e70fdf4c4cb4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb // indirect
google.golang.org/protobuf v1.36.5 // indirect
)
55 changes: 47 additions & 8 deletions modelarmor/modelarmor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,41 @@ func testClient(t *testing.T) (*modelarmor.Client, context.Context) {
return client, ctx
}

// testModelArmorTemplate creates a new ModelArmor template for use in tests.
// It returns the created template or an error.
func testModelArmorTemplate(t *testing.T, templateID string) (*modelarmorpb.Template, error) {
t.Helper()
tc := testutil.SystemTest(t)
locationID := testLocation(t)
client, ctx := testClient(t)

template := &modelarmorpb.Template{
FilterConfig: &modelarmorpb.FilterConfig{
PiAndJailbreakFilterSettings: &modelarmorpb.PiAndJailbreakFilterSettings{
FilterEnforcement: modelarmorpb.PiAndJailbreakFilterSettings_ENABLED,
ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_MEDIUM_AND_ABOVE,
},
MaliciousUriFilterSettings: &modelarmorpb.MaliciousUriFilterSettings{
FilterEnforcement: modelarmorpb.MaliciousUriFilterSettings_ENABLED,
},
},
}

req := &modelarmorpb.CreateTemplateRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", tc.ProjectID, locationID),
TemplateId: templateID,
Template: template,
}

response, err := client.CreateTemplate(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to create template: %w", err)
}

return response, nil
}


// testCleanupTemplate deletes the specified Model Armor template if it exists,
// ignoring the error if the template is already deleted.
func testCleanupTemplate(t *testing.T, templateName string) {
Expand Down Expand Up @@ -222,21 +257,25 @@ func TestDeleteModelArmorTemplate(t *testing.T) {
}
}

// TestCreateModelArmorTemplateWithBasicSDP tests the creation of a Model Armor
// template using a basic Secure Deployment Policy (SDP) and verifies that the
// operation completes successfully and logs the expected output.
func TestCreateModelArmorTemplateWithBasicSDP(t *testing.T) {
// TestUpdateTemplateWithMaskConfiguration verifies that a Model Armor template
// can be updated with a mask configuration. It creates a test template, performs
// the update, and checks the output for confirmation.
func TestUpdateTemplateWithMaskConfiguration(t *testing.T) {
tc := testutil.SystemTest(t)
locationID := testLocation(t)
templateID := fmt.Sprintf("test-model-armor-%s", uuid.New().String())
templateName := fmt.Sprintf("projects/%s/locations/%s/templates/%s", tc.ProjectID, locationID, templateID)
var b bytes.Buffer
if err := createModelArmorTemplateWithBasicSDP(&b, tc.ProjectID, locationID, templateID); err != nil {
var buf bytes.Buffer
if _, err := testModelArmorTemplate(t, templateID); err != nil {
t.Fatal(err)
}
defer testCleanupTemplate(t, templateName)

if got, want := b.String(), "Created Template with basic SDP: "; !strings.Contains(got, want) {
t.Errorf("createModelArmorTemplateWithBasicSDP: expected %q to contain %q", got, want)
if err := updateModelArmorTemplateWithMaskConfiguration(&buf, tc.ProjectID, locationID, templateID); err != nil {
t.Fatal(err)
}

if got, want := buf.String(), "Updated Model Armor Template: "; !strings.Contains(got, want) {
t.Errorf("updateModelArmorTemplateWithMaskConfiguration: expected %q to contain %q", got, want)
}
}
114 changes: 114 additions & 0 deletions modelarmor/update_template_with_mask_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0)
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Sample code for updating the model armor template with update mask.

package modelarmor

// [START modelarmor_update_template_with_mask_configuration]

import (
"context"
"fmt"
"io"

modelarmor "cloud.google.com/go/modelarmor/apiv1"
modelarmorpb "cloud.google.com/go/modelarmor/apiv1/modelarmorpb"
"google.golang.org/api/option"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)

// updateModelArmorTemplateWithMaskConfiguration updates a Model Armor template with mask configuration.
//
// w io.Writer: The writer to use for logging.
// projectID string: The ID of the project.
// locationID string: The ID of the location.
// templateID string: The ID of the template.
func updateModelArmorTemplateWithMaskConfiguration(w io.Writer, projectID, locationID, templateID string) error {
ctx := context.Background()

//Create options for Model Armor client.
opts := option.WithEndpoint(fmt.Sprintf("modelarmor.%s.rep.googleapis.com:443", locationID))
// Create the Model Armor client.
client, err := modelarmor.NewClient(ctx, opts)
if err != nil {
return fmt.Errorf("failed to create client for project %s, location %s: %w", projectID, locationID, err)
}
defer client.Close()

// Build the full resource path for the template.
templateName := fmt.Sprintf("projects/%s/locations/%s/templates/%s", projectID, locationID, templateID)

// Build the Model Armor template with your preferred filters.
// For more details on filters, please refer to the following doc:
// [https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters](https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters)
template := &modelarmorpb.Template{
Name: templateName,
FilterConfig: &modelarmorpb.FilterConfig{
RaiSettings: &modelarmorpb.RaiFilterSettings{
RaiFilters: []*modelarmorpb.RaiFilterSettings_RaiFilter{
{
FilterType: modelarmorpb.RaiFilterType_DANGEROUS,
ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_HIGH,
},
{
FilterType: modelarmorpb.RaiFilterType_HARASSMENT,
ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_MEDIUM_AND_ABOVE,
},
{
FilterType: modelarmorpb.RaiFilterType_HATE_SPEECH,
ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_HIGH,
},
{
FilterType: modelarmorpb.RaiFilterType_SEXUALLY_EXPLICIT,
ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_HIGH,
},
},
},
SdpSettings: &modelarmorpb.SdpFilterSettings{
SdpConfiguration: &modelarmorpb.SdpFilterSettings_BasicConfig{
BasicConfig: &modelarmorpb.SdpBasicConfig{
FilterEnforcement: modelarmorpb.SdpBasicConfig_DISABLED,
},
},
},
},
}

// Mask config for specifying field to update
// Refer to following documentation for more details on update mask field and its usage:
// [https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask](https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask)
updateMask := &fieldmaskpb.FieldMask{
Paths: []string{"filter_config"},
}

// Prepare the request to update the template.
// If mask configuration is not provided, all provided fields will be overwritten.
req := &modelarmorpb.UpdateTemplateRequest{
Template: template,
UpdateMask: updateMask,
}

// Update the template.
response, err := client.UpdateTemplate(ctx, req)
if err != nil {
return fmt.Errorf("failed to update template: %w", err)
}

fmt.Fprintf(w, "Updated Model Armor Template: %s\n", response.Name)

return nil
}

// [END modelarmor_update_template_with_mask_configuration]