Skip to content

feat(modelarmor): #12 Added snippets for update template with labels #5278

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 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 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
6ffeab5
feat(modelarmor): added code snippet for update template with labels
tirthrajsinh-zala-crest Apr 11, 2025
1648fc2
Merge branch 'main' into modelarmor-update-template-with-labels
tirthrajsinh-zala-crest Apr 11, 2025
cd0628c
Merge branch 'main' into modelarmor-update-template-with-labels
tirthrajsinh-zala-crest Apr 14, 2025
ae2c7f5
feat(modelarmor): code refactor.
tirthrajsinh-zala-crest Apr 14, 2025
7994a33
feat(modelarmor): code refactor
tirthrajsinh-zala-crest Apr 14, 2025
6646b8e
Merge branch 'main' into modelarmor-update-template-with-labels
tirthrajsinh-zala-crest Apr 17, 2025
45e9d9b
Merge branch 'main' into modelarmor-update-template-with-labels
tirthrajsinh-zala-crest Apr 18, 2025
6a6b55b
feat(modelarmor): resolve conflicts
tirthrajsinh-zala-crest Apr 18, 2025
28c7c3f
feat(modelarmor): resolve conflicts
tirthrajsinh-zala-crest Apr 18, 2025
e9d24e5
feat(modelarmor): resolve conflicts
tirthrajsinh-zala-crest Apr 18, 2025
29d0530
Merge branch 'main' into modelarmor-update-template-with-labels
tirthrajsinh-zala-crest Apr 23, 2025
c0829e6
Merge branch 'main' into modelarmor-update-template-with-labels
tirthrajsinh-zala-crest Apr 24, 2025
982433c
Merge branch 'main' into modelarmor-update-template-with-labels
tirthrajsinh-zala-crest Apr 25, 2025
c98df78
feat(modelarmor): resolve comments
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
)
61 changes: 59 additions & 2 deletions modelarmor/modelarmor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,42 @@ func testClient(t *testing.T) (*modelarmor.Client, context.Context) {
return client, ctx
}

// testCleanupTemplate deletes the specified Model Armor template if it exists,
// ignoring the error if the template is already deleted.
// testModelArmorTemplate creates a new Model Armor template with default
// filter settings for use in integration tests. Returns the created template.
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: %v", err)
}

return response, nil
}

// testCleanupTemplate deletes a Model Armor template by name, used for cleanup
// after tests. Ignores errors if the template is already deleted.
func testCleanupTemplate(t *testing.T, templateName string) {
t.Helper()

Expand All @@ -79,6 +113,29 @@ func testCleanupTemplate(t *testing.T, templateName string) {
t.Fatalf("testCleanupTemplate: failed to delete template: %v", err)
}
}

}

// TestUpdateTemplate verifies that the updateModelArmorTemplate function
// successfully updates the filter configuration of an existing template.
func TestUpdateTemplateLabels(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 buf bytes.Buffer
if _, err := testModelArmorTemplate(t, templateID); err != nil {
t.Fatal(err)
}
defer testCleanupTemplate(t, templateName)

if err := updateModelArmorTemplateLabels(&buf, tc.ProjectID, locationID, templateID, map[string]string{"testkey": "testvalue"}); err != nil {
t.Fatal(err)
}

if got, want := buf.String(), "Updated Model Armor Template Labels: "; !strings.Contains(got, want) {
t.Errorf("updateModelArmorTemplateLabels: expected %q to contain %q", got, want)
}
}

// testSDPTemplate creates DLP inspect and deidentify templates for use in tests.
Expand Down
81 changes: 81 additions & 0 deletions modelarmor/update_template_labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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 labels of the given model armor template.

package modelarmor

// [START modelarmor_update_template_with_labels]

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"
)

// updateModelArmorTemplateLabels method updates
// the labels of a Model Armor template.
//
// 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.
// labels map[string]string: The updated labels.
func updateModelArmorTemplateLabels(w io.Writer, projectID, locationID, templateID string, labels map[string]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 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: fmt.Sprintf("projects/%s/locations/%s/templates/%s", projectID, locationID, templateID),
Labels: labels,
}

// Prepare the request to update the template.
updateMask := &fieldmaskpb.FieldMask{
Paths: []string{"labels"},
}

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 Labels: %s\n", response.Name)

return nil
}

// [END modelarmor_update_template_with_labels]