diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplate.java b/modelarmor/src/main/java/modelarmor/CreateTemplate.java index f695cb36ede..a34275a8e7a 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplate.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplate.java @@ -38,7 +38,7 @@ public static void main(String[] args) throws IOException { // Specify the Google Project ID. String projectId = "your-project-id"; - // Specify the location ID. For example, us-central1. + // Specify the location ID. For example, us-central1. String locationId = "your-location-id"; // Specify the template ID. String templateId = "your-template-id"; diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java index 0faf2c7a42f..1dc6216c301 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java @@ -12,7 +12,7 @@ * 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. - */ +*/ package modelarmor; diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java index 5abf2bdb4cf..b6e320b4078 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java @@ -12,7 +12,7 @@ * 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. - */ +*/ package modelarmor; diff --git a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java index 0c76c44c5da..83c982da47f 100644 --- a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java +++ b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java @@ -40,6 +40,7 @@ public static void main(String[] args) throws IOException { public static void deleteTemplate(String projectId, String locationId, String templateId) throws IOException { + // Construct the API endpoint URL. String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) diff --git a/modelarmor/src/main/java/modelarmor/UpdateTemplate.java b/modelarmor/src/main/java/modelarmor/UpdateTemplate.java new file mode 100644 index 00000000000..5ee9f9dff5e --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/UpdateTemplate.java @@ -0,0 +1,116 @@ +/* + * 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 + * + * http://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. +*/ + +package modelarmor; + +// [START modelarmor_update_template] + +import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; +import com.google.cloud.modelarmor.v1.FilterConfig; +import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorSettings; +import com.google.cloud.modelarmor.v1.RaiFilterSettings; +import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter; +import com.google.cloud.modelarmor.v1.RaiFilterType; +import com.google.cloud.modelarmor.v1.Template; +import com.google.cloud.modelarmor.v1.TemplateName; +import com.google.cloud.modelarmor.v1.UpdateTemplateRequest; +import com.google.protobuf.FieldMask; +import java.io.IOException; +import java.util.List; + +public class UpdateTemplate { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + + // Specify the Google Project ID. + String projectId = "your-project-id"; + // Specify the location ID. For example, us-central1. + String locationId = "your-location-id"; + // Specify the template ID. + String templateId = "your-template-id"; + + updateTemplate(projectId, locationId, templateId); + } + + public static Template updateTemplate(String projectId, String locationId, String templateId) + throws IOException { + // Construct the API endpoint URL. + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); + + // Initialize the client that will be used to send requests. This client + // only needs to be created once, and can be reused for multiple requests. + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + // Get the template name. + String name = TemplateName.of(projectId, locationId, templateId).toString(); + + // Build the updated Model Armor template with modified 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 + RaiFilterSettings raiFilterSettings = + RaiFilterSettings.newBuilder() + .addAllRaiFilters( + List.of( + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HATE_SPEECH) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build())) + .build(); + + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setRaiSettings(raiFilterSettings) + .build(); + + Template template = Template.newBuilder() + .setName(name) + .setFilterConfig(modelArmorFilter) + .build(); + + // Create a field mask to specify which fields to update. + // Ref: https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask + FieldMask updateMask = FieldMask.newBuilder() + .addPaths("filter_config.rai_settings") + .build(); + + UpdateTemplateRequest request = UpdateTemplateRequest.newBuilder() + .setTemplate(template) + .setUpdateMask(updateMask) + .build(); + + Template updatedTemplate = client.updateTemplate(request); + System.out.println("Updated template: " + updatedTemplate.getName()); + + return updatedTemplate; + } + } +} + +// [END modelarmor_update_template] diff --git a/modelarmor/src/main/java/modelarmor/UpdateTemplateWithLabels.java b/modelarmor/src/main/java/modelarmor/UpdateTemplateWithLabels.java new file mode 100644 index 00000000000..8d5850dd753 --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/UpdateTemplateWithLabels.java @@ -0,0 +1,92 @@ +/* + * 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 + * + * http://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. +*/ + +package modelarmor; + +// [START modelarmor_update_template_labels] + +import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorSettings; +import com.google.cloud.modelarmor.v1.Template; +import com.google.cloud.modelarmor.v1.TemplateName; +import com.google.cloud.modelarmor.v1.UpdateTemplateRequest; +import com.google.protobuf.FieldMask; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class UpdateTemplateWithLabels { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + + // Specify the Google Project ID. + String projectId = "your-project-id"; + // Specify the location ID. For example, us-central1. + String locationId = "your-location-id"; + // Specify the template ID. + String templateId = "your-template-id"; + + updateTemplateWithLabels(projectId, locationId, templateId); + } + + public static Template updateTemplateWithLabels(String projectId, String locationId, + String templateId) throws IOException { + // Construct the API endpoint URL. + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); + + // Initialize the client that will be used to send requests. This client + // only needs to be created once, and can be reused for multiple requests. + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + // Get the template name. + String name = TemplateName.of(projectId, locationId, templateId).toString(); + + // Create a new labels map. + Map labels = new HashMap<>(); + + // Add or update labels. + labels.put("key1", "value2"); + labels.put("key2", "value3"); + + // Update the template with the new labels. + Template template = Template.newBuilder() + .setName(name) + .putAllLabels(labels) + .build(); + + // Create a field mask to specify that only labels should be updated. + FieldMask updateMask = FieldMask.newBuilder() + .addPaths("labels") + .build(); + + UpdateTemplateRequest request = + UpdateTemplateRequest.newBuilder() + .setTemplate(template) + .setUpdateMask(updateMask) + .build(); + + Template updatedTemplate = client.updateTemplate(request); + System.out.println("Updated labels of template: " + updatedTemplate.getName()); + + return updatedTemplate; + } + } +} + +// [END modelarmor_update_template_labels] diff --git a/modelarmor/src/main/java/modelarmor/UpdateTemplateWithMetadata.java b/modelarmor/src/main/java/modelarmor/UpdateTemplateWithMetadata.java new file mode 100644 index 00000000000..8fc3563d3be --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/UpdateTemplateWithMetadata.java @@ -0,0 +1,92 @@ +/* + * 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 + * + * http://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. +*/ + +package modelarmor; + +// [START modelarmor_update_template_metadata] + +import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorSettings; +import com.google.cloud.modelarmor.v1.Template; +import com.google.cloud.modelarmor.v1.Template.TemplateMetadata; +import com.google.cloud.modelarmor.v1.TemplateName; +import com.google.cloud.modelarmor.v1.UpdateTemplateRequest; +import com.google.protobuf.FieldMask; +import java.io.IOException; + +public class UpdateTemplateWithMetadata { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + + // Specify the Google Project ID. + String projectId = "your-project-id"; + // Specify the location ID. For example, us-central1. + String locationId = "your-location-id"; + // Specify the template ID. + String templateId = "your-template-id"; + + updateTemplateWithMetadata(projectId, locationId, templateId); + } + + public static Template updateTemplateWithMetadata(String projectId, String locationId, + String templateId) throws IOException { + // Construct the API endpoint URL. + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); + + // Initialize the client that will be used to send requests. This client + // only needs to be created once, and can be reused for multiple requests. + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + // Get the template name. + String name = TemplateName.of(projectId, locationId, templateId).toString(); + + // For more details about metadata, refer to the following documentation: + // https://cloud.google.com/security-command-center/docs/reference/model-armor/rest/v1/projects.locations.templates#templatemetadata + TemplateMetadata updatedMetadata = TemplateMetadata.newBuilder() + .setIgnorePartialInvocationFailures(false) + .setLogSanitizeOperations(false) + .setCustomPromptSafetyErrorCode(400) + .build(); + + // Update the template with new metadata. + Template template = Template.newBuilder() + .setName(name) + .setTemplateMetadata(updatedMetadata) + .build(); + + // Create a field mask to specify which metadata fields should be updated. + FieldMask updateMask = FieldMask.newBuilder() + .addPaths("template_metadata") + .build(); + + UpdateTemplateRequest request = + UpdateTemplateRequest.newBuilder() + .setTemplate(template) + .setUpdateMask(updateMask) + .build(); + + Template updatedTemplate = client.updateTemplate(request); + System.out.println("Updated metadata of template: " + updatedTemplate.getName()); + + return updatedTemplate; + } + } +} + +// [END modelarmor_update_template_metadata] diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index a627f7f93b2..f9ed4cf23b4 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -134,6 +134,43 @@ private static String randomId() { return "java-ma-" + random.nextLong(); } + @Test + public void testUpdateModelArmorTemplate() throws IOException { + CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + + // Update the existing template. + Template updatedTemplate = UpdateTemplate.updateTemplate(PROJECT_ID, LOCATION_ID, + TEST_TEMPLATE_ID); + + assertEquals(updatedTemplate.getName(), TEST_TEMPLATE_NAME); + } + + @Test + public void testUpdateModelArmorTemplateWithLabels() throws IOException { + CreateTemplateWithLabels.createTemplateWithLabels(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + + // Update the existing template. + Template updatedTemplate = UpdateTemplateWithLabels.updateTemplateWithLabels(PROJECT_ID, + LOCATION_ID, TEST_TEMPLATE_ID); + + assertEquals(updatedTemplate.getName(), TEST_TEMPLATE_NAME); + } + + @Test + public void testUpdateModelArmorTemplateWithMetadata() throws IOException { + CreateTemplateWithMetadata.createTemplateWithMetadata(PROJECT_ID, LOCATION_ID, + TEST_TEMPLATE_ID); + + // Update the existing template. + Template updatedTemplate = UpdateTemplateWithMetadata.updateTemplateWithMetadata(PROJECT_ID, + LOCATION_ID, TEST_TEMPLATE_ID); + + assertEquals(updatedTemplate.getName(), TEST_TEMPLATE_NAME); + assertEquals(false, updatedTemplate.getTemplateMetadata().getIgnorePartialInvocationFailures()); + assertEquals(false, updatedTemplate.getTemplateMetadata().getLogSanitizeOperations()); + assertEquals(400, updatedTemplate.getTemplateMetadata().getCustomPromptSafetyErrorCode()); + } + @Test public void testGetModelArmorTemplate() throws IOException { CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); @@ -294,10 +331,10 @@ private static DeidentifyTemplate createDeidentifyTemplate(String templateId) th CreateDeidentifyTemplateRequest createDeidentifyTemplateRequest = CreateDeidentifyTemplateRequest.newBuilder() - .setParent(LocationName.of(PROJECT_ID, LOCATION_ID).toString()) - .setTemplateId(templateId) - .setDeidentifyTemplate(deidentifyTemplate) - .build(); + .setParent(LocationName.of(PROJECT_ID, LOCATION_ID).toString()) + .setTemplateId(templateId) + .setDeidentifyTemplate(deidentifyTemplate) + .build(); return dlpServiceClient.createDeidentifyTemplate(createDeidentifyTemplateRequest); }