Skip to content

feat(modelarmor): modelarmor create and delete code snippets #5271

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 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
16 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
c116a7b
feat(modelarmor): Added modelarmor code snippet for basic template cr…
tirthrajsinh-zala-crest Apr 11, 2025
88bd2e8
feat(modelarmor): Added modelarmor code snippet for basic template cr…
tirthrajsinh-zala-crest Apr 11, 2025
001e341
feat(modelarmor): Added modelarmor code snippet for basic template cr…
tirthrajsinh-zala-crest Apr 11, 2025
8c202d5
Merge branch 'main' into modelarmor_create_and_delete_code_snippets
tirthrajsinh-zala-crest Apr 11, 2025
f4ca54c
feat(modelarmor): resolve comments
tirthrajsinh-zala-crest Apr 11, 2025
f816f5a
feat(modelarmor): resolve review comments
tirthrajsinh-zala-crest Apr 14, 2025
0c3f2ce
feat(modelarmor): code refactor
tirthrajsinh-zala-crest Apr 14, 2025
ca8a5a4
Merge branch 'main' into modelarmor_create_and_delete_code_snippets
tirthrajsinh-zala-crest Apr 14, 2025
7b64933
feat(modelarmor): code refactor
tirthrajsinh-zala-crest Apr 14, 2025
5bab87f
feat(modelarmor): code refactor
tirthrajsinh-zala-crest Apr 14, 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
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ use (
./media
./memorystore
./monitoring
./modelarmor
./opentelemetry/instrumentation/app
./opentelemetry/trace
./parametermanager
Expand Down
105 changes: 105 additions & 0 deletions modelarmor/create_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// 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
//
// 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 creating a new model armor template.

package modelarmor

// [START modelarmor_create_template]

import (
"context"
"fmt"
"io"

modelarmor "cloud.google.com/go/modelarmor/apiv1"
modelarmorpb "cloud.google.com/go/modelarmor/apiv1/modelarmorpb"
"google.golang.org/api/option"
)

// createModelArmorTemplate creates a new Model Armor template.
//
// This method creates a new Model Armor template with the provided settings.
//
// Args:
//
// w io.Writer: The writer to use for logging.
// projectID string: The ID of the Google Cloud project.
// locationID string: The ID of the Google Cloud location.
// templateID string: The ID of the template to create.
//
// Returns:
//
// *modelarmorpb.Template: The created template.
// error: Any error that occurred during template creation.
//
// Example:
//
// template, err := createModelArmorTemplate(
// os.Stdout,
// "my-project",
// "us-central1",
// "my-template",
// )
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(template)
func createModelArmorTemplate(w io.Writer, projectID, location, templateID string) (*modelarmorpb.Template, error) {
ctx := context.Background()

// Create the Model Armor client.
client, err := modelarmor.NewClient(ctx,
option.WithEndpoint(fmt.Sprintf("modelarmor.%s.rep.googleapis.com:443", location)),
)
if err != nil {
return nil, fmt.Errorf("failed to create client for project %s, location %s: %v", projectID, location, 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{
FilterConfig: &modelarmorpb.FilterConfig{
PiAndJailbreakFilterSettings: &modelarmorpb.PiAndJailbreakFilterSettings{
FilterEnforcement: modelarmorpb.PiAndJailbreakFilterSettings_ENABLED,
ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_MEDIUM_AND_ABOVE,
},
MaliciousUriFilterSettings: &modelarmorpb.MaliciousUriFilterSettings{
FilterEnforcement: modelarmorpb.MaliciousUriFilterSettings_ENABLED,
},
},
}

// Prepare the request for creating the template.
req := &modelarmorpb.CreateTemplateRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
TemplateId: templateID,
Template: template,
}

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

// Print the new template name using fmt.Fprintf with the io.Writer.
fmt.Fprintf(w, "Created template: %s\n", response.Name)

// [END modelarmor_create_template]

return response, nil
}
84 changes: 84 additions & 0 deletions modelarmor/delete_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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
//
// 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 deleting a model armor template.

package modelarmor

// [START modelarmor_delete_template]

import (
"context"
"fmt"
"io"

modelarmor "cloud.google.com/go/modelarmor/apiv1"
modelarmorpb "cloud.google.com/go/modelarmor/apiv1/modelarmorpb"
"google.golang.org/api/option"
)

// deleteModelArmorTemplate deletes a Model Armor template.
//
// This method deletes a Model Armor template with the provided ID.
//
// Args:
//
// w io.Writer: The writer to use for logging.
// projectID string: The ID of the Google Cloud project.
// locationID string: The ID of the Google Cloud location.
// templateID string: The ID of the template to delete.
//
// Returns:
//
// error: Any error that occurred during template deletion.
//
// Example:
//
// err := deleteModelArmorTemplate(
// os.Stdout,
// "my-project",
// "us-central1",
// "my-template",
// )
// if err != nil {
// log.Fatal(err)
// }
func deleteModelArmorTemplate(w io.Writer, projectID, location, templateID string) error {
ctx := context.Background()

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

// Build the request for deleting the template.
req := &modelarmorpb.DeleteTemplateRequest{
Name: fmt.Sprintf("projects/%s/locations/%s/templates/%s", projectID, location, templateID),
}

// Delete the template.
if err := client.DeleteTemplate(ctx, req); err != nil {
return fmt.Errorf("failed to delete template: %v", err)
}

// Print the success message using fmt.Fprintf with the io.Writer.
fmt.Fprintf(w, "Successfully deleted Model Armor template: %s\n", req.Name)
// [END modelarmor_delete_template]

return nil
}
57 changes: 57 additions & 0 deletions modelarmor/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module github.com/GoogleCloudPlatform/golang-samples/modelarmor

go 1.23.0

require (
cloud.google.com/go/modelarmor v0.1.0
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20250404170905-0aca11152736
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
google.golang.org/api v0.226.0
google.golang.org/grpc v1.71.0
)

require (
cel.dev/expr v0.19.1 // indirect
cloud.google.com/go v0.118.3 // indirect
cloud.google.com/go/auth v0.15.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.7 // indirect
cloud.google.com/go/compute/metadata v0.6.0 // indirect
cloud.google.com/go/iam v1.4.0 // indirect
cloud.google.com/go/monitoring v1.24.0 // indirect
cloud.google.com/go/storage v1.50.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.49.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cncf/xds/go v0.0.0-20250121191232-2f005788dc42 // indirect
github.com/envoyproxy/go-control-plane/envoy v1.32.4 // indirect
github.com/envoyproxy/protoc-gen-validate v1.2.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/s2a-go v0.1.9 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.5 // indirect
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/detectors/gcp v1.34.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/net v0.37.0 // indirect
golang.org/x/oauth2 v0.28.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.11.0 // indirect
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
)
Loading