Skip to content

closed as deprecated: feat(modelarmor): Added create, update, delete, list modelarmor code snippets #10063

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

Conversation

tirthrajsinh-zala-crest
Copy link
Contributor

@tirthrajsinh-zala-crest tirthrajsinh-zala-crest commented Apr 10, 2025

Description

Created samples for creating, updating, reading, deleting model armor templates.

Checklist

  • I have followed Sample Format Guide
  • pom.xml parent set to latest shared-configuration
  • Appropriate changes to README are included in PR
  • These samples need a new API enabled in testing projects to pass (let us know which ones)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • Tests pass: mvn clean verify required
  • Lint passes: mvn -P lint checkstyle:check required
  • Static Analysis: mvn -P lint clean compile pmd:cpd-check spotbugs:check advisory only
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Apr 10, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @tirthrajsinh-zala-crest, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request introduces Java code samples for interacting with the Google Cloud Model Armor API. It includes samples for creating, updating, reading (getting), deleting, and listing Model Armor templates. The samples cover various aspects of template creation, such as including basic SDP, labels, and metadata. The PR also includes integration tests to verify the functionality of the code samples.

Highlights

  • Create Template Samples: Added samples for creating Model Armor templates with different configurations, including basic SDP, labels, and metadata.
  • CRUD Operations: Implemented samples for Create, Read (Get), Update, and Delete operations on Model Armor templates.
  • List Operations: Implemented samples for listing all Model Armor templates and listing templates with filtering.
  • Update Template Samples: Added samples for updating Model Armor templates, including updating labels, metadata and using mask configurations.
  • Integration Tests: Included integration tests to validate the functionality of the Model Armor code samples.

Changelog

Click here to see the changelog
  • modelarmor/README.md
    • Added a README file with a description of the Model Armor service and instructions for enabling the API, granting permissions, and setting environment variables.
  • modelarmor/pom.xml
    • Added dependencies for google-cloud-modelarmor, google-cloud-dlp, protobuf-java-util, and lombok.
    • Configured the maven-compiler-plugin to use Java 11 and enable annotation processing for Lombok.
  • modelarmor/src/main/java/modelarmor/CreateTemplate.java
    • Created a sample for creating a Model Armor template with basic configuration.
  • modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java
    • Created a sample for creating a Model Armor template with basic SDP (Session Description Protocol).
  • modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java
    • Created a sample for creating a Model Armor template with labels.
  • modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java
    • Created a sample for creating a Model Armor template with metadata.
  • modelarmor/src/main/java/modelarmor/DeleteTemplate.java
    • Created a sample for deleting a Model Armor template.
  • modelarmor/src/main/java/modelarmor/GetTemplate.java
    • Created a sample for retrieving (getting) a Model Armor template.
  • modelarmor/src/main/java/modelarmor/ListTemplates.java
    • Created a sample for listing all Model Armor templates.
  • modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java
    • Created a sample for listing Model Armor templates with a filter.
  • modelarmor/src/main/java/modelarmor/UpdateTemplate.java
    • Created a sample for updating a Model Armor template.
  • modelarmor/src/main/java/modelarmor/UpdateTemplateLabels.java
    • Created a sample for updating the labels of a Model Armor template.
  • modelarmor/src/main/java/modelarmor/UpdateTemplateMetadata.java
    • Created a sample for updating the metadata of a Model Armor template.
  • modelarmor/src/main/java/modelarmor/UpdateTemplateWithMaskConfiguration.java
    • Created a sample for updating a Model Armor template with a mask configuration.
  • modelarmor/src/test/java/modelarmor/SnippetsIT.java
    • Created integration tests for all the Model Armor code samples.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


In clouds of code, a template takes flight,
Protecting models, day and through night.
With filters and masks,
It completes its tasks,
Guiding AI towards what is right.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces code snippets for creating, updating, reading, and deleting Model Armor templates. The samples cover various aspects of template management, including basic creation, adding labels and metadata, and updating configurations. Overall, the code is well-structured and easy to follow. However, there are a few areas that could be improved for clarity and completeness.

Summary of Findings

  • Missing region tags: The TODO(nasit): Add region tags in all snippets. comment indicates that region tags are missing. Adding these tags would improve the discoverability and reusability of the code snippets.
  • Inconsistent copyright year: The copyright year in pom.xml is 2025, while some Java files have 2020. Ensure consistency across all files.
  • Test Coverage: While integration tests are provided, it would be beneficial to include unit tests to cover individual functions and edge cases for more robust testing.

Merge Readiness

The pull request is almost ready for merging. Before merging, it's recommended to address the missing region tags and copyright year inconsistencies. While not critical, adding unit tests would further enhance the quality of the samples. I am unable to directly approve this pull request, and recommend that others review and approve this code before merging.


package modelarmor;

// TODO(nasit): Add region tags in all snippets.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding region tags as indicated by the TODO comment. This will help users easily identify and reuse specific parts of the code.

For example:
// [START model_armor_create_template]

Suggested change
// TODO(nasit): Add region tags in all snippets.
// [START model_armor_create_template]

Comment on lines +31 to +32
import java.util.List;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding region tags as indicated by the TODO comment. This will help users easily identify and reuse specific parts of the code.

For example:
// [START model_armor_create_template_metadata]

Suggested change
import java.util.List;
// [START model_armor_create_template_metadata]

Comment on lines +39 to +43
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String name = TemplateName.of(projectId, locationId, templateId).toString();
client.deleteTemplate(name);
System.out.println("Deleted template: " + name);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding region tags as indicated by the TODO comment. This will help users easily identify and reuse specific parts of the code.

For example:
// [START model_armor_delete_template]

    try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
      // [START model_armor_delete_template]
      String name = TemplateName.of(projectId, locationId, templateId).toString();
      client.deleteTemplate(name);
      System.out.println("Deleted template: " + name);
      // [END model_armor_delete_template]
    }

Comment on lines +42 to +46
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String name = TemplateName.of(projectId, locationId, templateId).toString();
Template template = client.getTemplate(name);
System.out.println("Retrieved template: " + JsonFormat.printer().print(template));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding region tags as indicated by the TODO comment. This will help users easily identify and reuse specific parts of the code.

For example:
// [START model_armor_get_template]

    try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
      // [START model_armor_get_template]
      String name = TemplateName.of(projectId, locationId, templateId).toString();
      Template template = client.getTemplate(name);
      System.out.println("Retrieved template: " + JsonFormat.printer().print(template));
      // [END model_armor_get_template]
    }

Comment on lines +41 to +47
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String parent = LocationName.of(projectId, locationId).toString();
ListTemplatesRequest request = ListTemplatesRequest.newBuilder().setParent(parent).build();
for (Template template : client.listTemplates(request).iterateAll()) {
System.out.println("Retrived Templates: " + JsonFormat.printer().print(template));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding region tags as indicated by the TODO comment. This will help users easily identify and reuse specific parts of the code.

For example:
// [START model_armor_list_templates]

    try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
      // [START model_armor_list_templates]
      String parent = LocationName.of(projectId, locationId).toString();
      ListTemplatesRequest request = ListTemplatesRequest.newBuilder().setParent(parent).build();
      for (Template template : client.listTemplates(request).iterateAll()) {
        System.out.println("Retrived Templates: " + JsonFormat.printer().print(template));
      }
      // [END model_armor_list_templates]
    }

Comment on lines +43 to +50
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String parent = LocationName.of(projectId, locationId).toString();
String filter = String.format("name=\"%s/templates/%s\"", parent, templateId);
ListTemplatesRequest request =
ListTemplatesRequest.newBuilder().setParent(parent).setFilter(filter).build();
for (Template template : client.listTemplates(request).iterateAll()) {
System.out.println("Template with filter: " + JsonFormat.printer().print(template));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding region tags as indicated by the TODO comment. This will help users easily identify and reuse specific parts of the code.

For example:
// [START model_armor_list_templates_with_filter]

    try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
      // [START model_armor_list_templates_with_filter]
      String parent = LocationName.of(projectId, locationId).toString();
      String filter = String.format("name=\"%s/templates/%s\"", parent, templateId);
      ListTemplatesRequest request = ListTemplatesRequest.newBuilder().setParent(parent).setFilter(filter).build();
      for (Template template : client.listTemplates(request).iterateAll()) {
        System.out.println("Template with filter: " + JsonFormat.printer().print(template));
      }
      // [END model_armor_list_templates_with_filter]
    }

Comment on lines +50 to +79
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String name = TemplateName.of(projectId, locationId, templateId).toString();

Template template =
Template.newBuilder()
.setName(name)
.setFilterConfig(
FilterConfig.newBuilder()
.setRaiSettings(
RaiFilterSettings.newBuilder()
.addAllRaiFilters(
List.of(
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.HARASSMENT)
.setConfidenceLevel(
DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
.build()))
.build())
.build())
.build();

UpdateTemplateRequest request =
UpdateTemplateRequest.newBuilder()
.setTemplate(template)
.setUpdateMask(FieldMask.newBuilder().addPaths("filter_config").build())
.build();

Template updatedTemplate = client.updateTemplate(request);
System.out.println("Updated template: " + JsonFormat.printer().print(updatedTemplate));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding region tags as indicated by the TODO comment. This will help users easily identify and reuse specific parts of the code.

For example:
// [START model_armor_update_template]

    try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
      // [START model_armor_update_template]
      String name = TemplateName.of(projectId, locationId, templateId).toString();

      Template template =
          Template.newBuilder()
              .setName(name)
              .setFilterConfig(
                  FilterConfig.newBuilder()
                      .setRaiSettings(
                          RaiFilterSettings.newBuilder()
                              .addAllRaiFilters(
                                  List.of(
                                      RaiFilter.newBuilder()
                                          .setFilterType(RaiFilterType.HARASSMENT)
                                          .setConfidenceLevel(
                                              DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
                                          .build()))
                              .build())
                      .build())
              .build();

      UpdateTemplateRequest request =
          UpdateTemplateRequest.newBuilder()
              .setTemplate(template)
              .setUpdateMask(FieldMask.newBuilder().addPaths("filter_config").build())
              .build();

      Template updatedTemplate = client.updateTemplate(request);
      System.out.println("Updated template: " + JsonFormat.printer().print(updatedTemplate));
      // [END model_armor_update_template]
    }

Comment on lines +46 to +63
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String name = TemplateName.of(projectId, locationId, templateId).toString();

Map<String, String> labels = new HashMap<>();
labels.put("key3", "value3");
labels.put("key4", "value4");

Template template = Template.newBuilder().setName(name).putAllLabels(labels).build();

UpdateTemplateRequest request =
UpdateTemplateRequest.newBuilder()
.setTemplate(template)
.setUpdateMask(FieldMask.newBuilder().addPaths("labels").build())
.build();

Template updatedTemplate = client.updateTemplate(request);
System.out.println("Updated template labels: " + JsonFormat.printer().print(updatedTemplate));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding region tags as indicated by the TODO comment. This will help users easily identify and reuse specific parts of the code.

For example:
// [START model_armor_update_template_labels]

    try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
      // [START model_armor_update_template_labels]
      String name = TemplateName.of(projectId, locationId, templateId).toString();

      Map<String, String> labels = new HashMap<>();
      labels.put("key3", "value3");
      labels.put("key4", "value4");

      Template template = Template.newBuilder().setName(name).putAllLabels(labels).build();

      UpdateTemplateRequest request =
          UpdateTemplateRequest.newBuilder()
              .setTemplate(template)
              .setUpdateMask(FieldMask.newBuilder().addPaths("labels").build())
              .build();

      Template updatedTemplate = client.updateTemplate(request);
      System.out.println("Updated template labels: " + JsonFormat.printer().print(updatedTemplate));
      // [END model_armor_update_template_labels]
    }

Comment on lines +49 to +83
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String name = TemplateName.of(projectId, locationId, templateId).toString();

Template template =
Template.newBuilder()
.setName(name)
// Ensure the rest of the template is correctly populated as needed.
.setTemplateMetadata(
Template.TemplateMetadata.newBuilder()
.setIgnorePartialInvocationFailures(true)
.setLogSanitizeOperations(true))
.setFilterConfig(
FilterConfig.newBuilder()
.setRaiSettings(
RaiFilterSettings.newBuilder()
.addAllRaiFilters(
List.of(
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.DANGEROUS)
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
.build()))
.build())
.build())
.build();

UpdateTemplateRequest request =
UpdateTemplateRequest.newBuilder()
.setTemplate(template)
// Removed the setUpdateMask line to attempt a full update.
.build();

Template updatedTemplate = client.updateTemplate(request);
System.out.println(
"Updated template metadata: " + JsonFormat.printer().print(updatedTemplate));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding region tags as indicated by the TODO comment. This will help users easily identify and reuse specific parts of the code.

For example:
// [START model_armor_update_template_metadata]

    try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
      // [START model_armor_update_template_metadata]
      String name = TemplateName.of(projectId, locationId, templateId).toString();

      Template template =
          Template.newBuilder()
              .setName(name)
              // Ensure the rest of the template is correctly populated as needed.
              .setTemplateMetadata(
                  Template.TemplateMetadata.newBuilder()
                      .setIgnorePartialInvocationFailures(true)
                      .setLogSanitizeOperations(true))
              .setFilterConfig(
                  FilterConfig.newBuilder()
                      .setRaiSettings(
                          RaiFilterSettings.newBuilder()
                              .addAllRaiFilters(
                                  List.of(
                                      RaiFilter.newBuilder()
                                          .setFilterType(RaiFilterType.DANGEROUS)
                                          .setConfidenceLevel(DetectionConfidenceLevel.HIGH)
                                          .build()))
                              .build()))
              .build();

      UpdateTemplateRequest request =
          UpdateTemplateRequest.newBuilder()
              .setTemplate(template)
              // Removed the setUpdateMask line to attempt a full update.
              .build();

      Template updatedTemplate = client.updateTemplate(request);
      System.out.println(
          "Updated template metadata: " + JsonFormat.printer().print(updatedTemplate));
      // [END model_armor_update_template_metadata]
    }

Comment on lines +50 to +82
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String name = TemplateName.of(projectId, locationId, templateId).toString();

Template template =
Template.newBuilder()
.setName(name)
.setFilterConfig(
FilterConfig.newBuilder()
.setRaiSettings(
RaiFilterSettings.newBuilder()
.addAllRaiFilters(
List.of(
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.HARASSMENT)
.setConfidenceLevel(
DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
.build()))
.build())
.build())
.build();

UpdateTemplateRequest request =
UpdateTemplateRequest.newBuilder()
.setTemplate(template)
.setUpdateMask(
FieldMask.newBuilder().addPaths("filter_config.rai_settings.rai_filters").build())
.build();

Template updatedTemplate = client.updateTemplate(request);
System.out.println(
"Updated template with mask configuration: "
+ JsonFormat.printer().print(updatedTemplate));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding region tags as indicated by the TODO comment. This will help users easily identify and reuse specific parts of the code.

For example:
// [START model_armor_update_template_with_mask_configuration]

    try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
      // [START model_armor_update_template_with_mask_configuration]
      String name = TemplateName.of(projectId, locationId, templateId).toString();

      Template template =
          Template.newBuilder()
              .setName(name)
              .setFilterConfig(
                  FilterConfig.newBuilder()
                      .setRaiSettings(
                          RaiFilterSettings.newBuilder()
                              .addAllRaiFilters(
                                  List.of(
                                      RaiFilter.newBuilder()
                                          .setFilterType(RaiFilterType.HARASSMENT)
                                          .setConfidenceLevel(
                                              DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
                                          .build()))
                              .build())
                      .build())
              .build();

      UpdateTemplateRequest request =
          UpdateTemplateRequest.newBuilder()
              .setTemplate(template)
              .setUpdateMask(
                  FieldMask.newBuilder().addPaths("filter_config.rai_settings.rai_filters").build())
              .build();

      Template updatedTemplate = client.updateTemplate(request);
      System.out.println(
          "Updated template with mask configuration: "
              + JsonFormat.printer().print(updatedTemplate));
      // [END model_armor_update_template_with_mask_configuration]
    }

@telpirion telpirion self-assigned this Apr 11, 2025
Copy link
Contributor

@telpirion telpirion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR has the same global issues as #10068 . Some of the samples in this PR are also missing region tags.

@tirthrajsinh-zala-crest
Copy link
Contributor Author

Closing this pr since we have separated this pr with the smaller PRs, however, those are still in draft.

@harshnasitcrest harshnasitcrest deleted the modelarmor-curd-code-snippets branch April 16, 2025 13:55
@tirthrajsinh-zala-crest tirthrajsinh-zala-crest changed the title feat(modelarmor): Added create, update, delete, list modelarmor code snippets closed as deprecated: feat(modelarmor): Added create, update, delete, list modelarmor code snippets Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants