From d40e2b26746bae9156227f60ca9c81642a6f5921 Mon Sep 17 00:00:00 2001 From: rudrakhsha Date: Fri, 11 Apr 2025 12:39:47 +0000 Subject: [PATCH 01/12] Added code samples for delete, get and list model armor templates --- .../main/java/modelarmor/DeleteTemplate.java | 45 +++++++ .../main/java/modelarmor/GetTemplate.java | 48 ++++++++ .../main/java/modelarmor/ListTemplates.java | 49 ++++++++ .../modelarmor/ListTemplatesWithFilter.java | 53 +++++++++ .../test/java/modelarmor/SnippetsIT.java | 111 ++++++++++++++++++ 5 files changed, 306 insertions(+) create mode 100644 modelarmor/main/java/modelarmor/DeleteTemplate.java create mode 100644 modelarmor/main/java/modelarmor/GetTemplate.java create mode 100644 modelarmor/main/java/modelarmor/ListTemplates.java create mode 100644 modelarmor/main/java/modelarmor/ListTemplatesWithFilter.java create mode 100644 modelarmor/test/java/modelarmor/SnippetsIT.java diff --git a/modelarmor/main/java/modelarmor/DeleteTemplate.java b/modelarmor/main/java/modelarmor/DeleteTemplate.java new file mode 100644 index 00000000000..7e2f2f49529 --- /dev/null +++ b/modelarmor/main/java/modelarmor/DeleteTemplate.java @@ -0,0 +1,45 @@ +/* + * 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; + +import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorSettings; +import com.google.cloud.modelarmor.v1.TemplateName; + +public class DeleteTemplate { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String locationId = "your-location-id"; + String templateId = "your-template-id"; + + deleteTemplate(projectId, locationId, templateId); + } + + public static void deleteTemplate(String projectId, String locationId, String templateId) + throws Exception { + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + ModelArmorSettings modelArmorSettings = + ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + String name = TemplateName.of(projectId, locationId, templateId).toString(); + client.deleteTemplate(name); + System.out.println("Deleted template: " + name); + } + } +} diff --git a/modelarmor/main/java/modelarmor/GetTemplate.java b/modelarmor/main/java/modelarmor/GetTemplate.java new file mode 100644 index 00000000000..0909cd80879 --- /dev/null +++ b/modelarmor/main/java/modelarmor/GetTemplate.java @@ -0,0 +1,48 @@ +/* + * 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; + +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.protobuf.util.JsonFormat; + +public class GetTemplate { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String locationId = "your-location-id"; + String templateId = "your-template-id"; + + getTemplate(projectId, locationId, templateId); + } + + public static void getTemplate(String projectId, String locationId, String templateId) + throws Exception { + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + ModelArmorSettings modelArmorSettings = + ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + + 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)); + } + } +} diff --git a/modelarmor/main/java/modelarmor/ListTemplates.java b/modelarmor/main/java/modelarmor/ListTemplates.java new file mode 100644 index 00000000000..c58eb046be8 --- /dev/null +++ b/modelarmor/main/java/modelarmor/ListTemplates.java @@ -0,0 +1,49 @@ +/* + * 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; + +import com.google.cloud.modelarmor.v1.ListTemplatesRequest; +import com.google.cloud.modelarmor.v1.LocationName; +import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorSettings; +import com.google.cloud.modelarmor.v1.Template; +import com.google.protobuf.util.JsonFormat; + +public class ListTemplates { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String locationId = "your-location-id"; + + listTemplates(projectId, locationId); + } + + public static void listTemplates(String projectId, String locationId) throws Exception { + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + ModelArmorSettings modelArmorSettings = + ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + + 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)); + } + } + } +} diff --git a/modelarmor/main/java/modelarmor/ListTemplatesWithFilter.java b/modelarmor/main/java/modelarmor/ListTemplatesWithFilter.java new file mode 100644 index 00000000000..590ef9b4f29 --- /dev/null +++ b/modelarmor/main/java/modelarmor/ListTemplatesWithFilter.java @@ -0,0 +1,53 @@ +/* + * 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; + +import com.google.cloud.modelarmor.v1.ListTemplatesRequest; +import com.google.cloud.modelarmor.v1.LocationName; +import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorSettings; +import com.google.cloud.modelarmor.v1.Template; +import com.google.protobuf.util.JsonFormat; + +public class ListTemplatesWithFilter { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String locationId = "your-location-id"; + String templateId = "your-template-id"; + + listTemplatesWithFilter(projectId, locationId, templateId); + } + + public static void listTemplatesWithFilter(String projectId, String locationId, String templateId) + throws Exception { + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + ModelArmorSettings modelArmorSettings = + ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + + 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)); + } + } + } +} diff --git a/modelarmor/test/java/modelarmor/SnippetsIT.java b/modelarmor/test/java/modelarmor/SnippetsIT.java new file mode 100644 index 00000000000..0321a7121fc --- /dev/null +++ b/modelarmor/test/java/modelarmor/SnippetsIT.java @@ -0,0 +1,111 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.modelarmor.v1.Template; +import com.google.common.base.Strings; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link Snippets}. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:AbbreviationAsWordInName") +public class SnippetsIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION = "us-central1"; + private static final String MA_REGIONAL_ENDPOINT = + String.format("modelarmor.%s.rep.googleapis.com:443", LOCATION); + private static final String DLP_REGIONAL_ENDPOINT = + String.format("dlp.%s.rep.googleapis.com:443", LOCATION); + private static final String INSPECT_TEMPLATE_ID = + "model-armour-inspect-template-" + UUID.randomUUID().toString(); + private static final String DEIDENTIFY_TEMPLATE_ID = + "model-armour-deidentify-template-" + UUID.randomUUID().toString(); + private static Template TEST_MODELARMOR_TEMPLATE; + private static Template TEST_MODELARMOR_TEMPLATE_NAME; + private static String TEMPLATE_ID; + + private ByteArrayOutputStream stdOut; + + @BeforeClass + public static void beforeAll() throws Exception { + Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); + Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION)); + } + + @AfterClass + public static void afterAll() throws Exception { + Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); + } + + @Before + public void beforeEach() { + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + + TEMPLATE_ID = "test-model-armor-" + UUID.randomUUID().toString(); + } + + @After + public void afterEach() throws Exception { + stdOut = null; + System.setOut(null); + } + + @Test + public void testDeleteModelArmorTemplate() throws Exception { + CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + assertThat(stdOut.toString()).contains("Deleted template"); + } + + @Test + public void testGetModelArmorTemplate() throws Exception { + CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + GetTemplate.getTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + assertThat(stdOut.toString()).contains("Retrieved template"); + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + } + + @Test + public void testListModelArmorTemplates() throws Exception { + CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + ListTemplates.listTemplates(PROJECT_ID, LOCATION); + assertThat(stdOut.toString()).contains("Retrived Templates"); + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + } + + @Test + public void testListTemplatesWithFilter() throws Exception { + CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + ListTemplatesWithFilter.listTemplatesWithFilter(PROJECT_ID, LOCATION, TEMPLATE_ID); + assertThat(stdOut.toString()).contains("Template with filter"); + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + } +} From 4bc2d54413d5345cc58aa09fb0415f9be04bcce9 Mon Sep 17 00:00:00 2001 From: rudrakhsha Date: Fri, 11 Apr 2025 13:24:28 +0000 Subject: [PATCH 02/12] Added create template code snippet --- .../main/java/modelarmor/CreateTemplate.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 modelarmor/main/java/modelarmor/CreateTemplate.java diff --git a/modelarmor/main/java/modelarmor/CreateTemplate.java b/modelarmor/main/java/modelarmor/CreateTemplate.java new file mode 100644 index 00000000000..3d3e0b44c3d --- /dev/null +++ b/modelarmor/main/java/modelarmor/CreateTemplate.java @@ -0,0 +1,80 @@ +/* + * 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; + +import com.google.cloud.modelarmor.v1.CreateTemplateRequest; +import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; +import com.google.cloud.modelarmor.v1.FilterConfig; +import com.google.cloud.modelarmor.v1.LocationName; +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.protobuf.util.JsonFormat; +import java.util.List; + +public class CreateTemplate { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String locationId = "your-location-id"; + String templateId = "your-template-id"; + + createTemplate(projectId, locationId, templateId); + } + + public static Template createTemplate(String projectId, String locationId, String templateId) + throws Exception { + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + ModelArmorSettings modelArmorSettings = + ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + String parent = LocationName.of(projectId, locationId).toString(); + + Template template = + Template.newBuilder() + .setFilterConfig( + FilterConfig.newBuilder() + .setRaiSettings( + RaiFilterSettings.newBuilder() + .addAllRaiFilters( + List.of( + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build())) + .build()) + .build()) + .build(); + + CreateTemplateRequest request = + CreateTemplateRequest.newBuilder() + .setParent(parent) + .setTemplateId(templateId) + .setTemplate(template) + .build(); + + Template createdTemplate = client.createTemplate(request); + System.out.println("Created template: " + JsonFormat.printer().print(createdTemplate)); + return createdTemplate; + } + } +} From 22f1b674012791040056f9dd37864a779a2634d8 Mon Sep 17 00:00:00 2001 From: tirthrajsinh-zala-crest Date: Fri, 11 Apr 2025 19:50:26 +0530 Subject: [PATCH 03/12] feat(modelarmor): refactor code --- modelarmor/pom.xml | 84 +++++++++++++++++++ .../main/java/modelarmor/CreateTemplate.java | 0 .../main/java/modelarmor/DeleteTemplate.java | 0 .../main/java/modelarmor/GetTemplate.java | 0 .../main/java/modelarmor/ListTemplates.java | 0 .../modelarmor/ListTemplatesWithFilter.java | 0 .../test/java/modelarmor/SnippetsIT.java | 0 7 files changed, 84 insertions(+) create mode 100644 modelarmor/pom.xml rename modelarmor/{ => src}/main/java/modelarmor/CreateTemplate.java (100%) rename modelarmor/{ => src}/main/java/modelarmor/DeleteTemplate.java (100%) rename modelarmor/{ => src}/main/java/modelarmor/GetTemplate.java (100%) rename modelarmor/{ => src}/main/java/modelarmor/ListTemplates.java (100%) rename modelarmor/{ => src}/main/java/modelarmor/ListTemplatesWithFilter.java (100%) rename modelarmor/{ => src}/test/java/modelarmor/SnippetsIT.java (100%) diff --git a/modelarmor/pom.xml b/modelarmor/pom.xml new file mode 100644 index 00000000000..ee0c4f24516 --- /dev/null +++ b/modelarmor/pom.xml @@ -0,0 +1,84 @@ + + + + 4.0.0 + com.example.modelarmor + modelarmor-samples + jar + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + UTF-8 + 11 + 11 + + + + + + com.google.cloud + libraries-bom + 26.59.0 + pom + import + + + + + + + com.google.cloud + google-cloud-modelarmor + + + + com.google.cloud + google-cloud-dlp + + + + com.google.protobuf + protobuf-java-util + + + + + junit + junit + 4.13.2 + test + + + com.google.truth + truth + 1.4.0 + test + + + + diff --git a/modelarmor/main/java/modelarmor/CreateTemplate.java b/modelarmor/src/main/java/modelarmor/CreateTemplate.java similarity index 100% rename from modelarmor/main/java/modelarmor/CreateTemplate.java rename to modelarmor/src/main/java/modelarmor/CreateTemplate.java diff --git a/modelarmor/main/java/modelarmor/DeleteTemplate.java b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java similarity index 100% rename from modelarmor/main/java/modelarmor/DeleteTemplate.java rename to modelarmor/src/main/java/modelarmor/DeleteTemplate.java diff --git a/modelarmor/main/java/modelarmor/GetTemplate.java b/modelarmor/src/main/java/modelarmor/GetTemplate.java similarity index 100% rename from modelarmor/main/java/modelarmor/GetTemplate.java rename to modelarmor/src/main/java/modelarmor/GetTemplate.java diff --git a/modelarmor/main/java/modelarmor/ListTemplates.java b/modelarmor/src/main/java/modelarmor/ListTemplates.java similarity index 100% rename from modelarmor/main/java/modelarmor/ListTemplates.java rename to modelarmor/src/main/java/modelarmor/ListTemplates.java diff --git a/modelarmor/main/java/modelarmor/ListTemplatesWithFilter.java b/modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java similarity index 100% rename from modelarmor/main/java/modelarmor/ListTemplatesWithFilter.java rename to modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java diff --git a/modelarmor/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java similarity index 100% rename from modelarmor/test/java/modelarmor/SnippetsIT.java rename to modelarmor/src/test/java/modelarmor/SnippetsIT.java From 7dff76b30d6803463c764c9dff79cdb293aa1455 Mon Sep 17 00:00:00 2001 From: tirthrajsinh-zala-crest Date: Fri, 11 Apr 2025 20:07:04 +0530 Subject: [PATCH 04/12] feat(modelarmor): refactor code --- .../main/java/modelarmor/CreateTemplate.java | 30 ++++++++++++++++ .../main/java/modelarmor/DeleteTemplate.java | 28 +++++++++++++++ .../src/main/java/modelarmor/GetTemplate.java | 32 +++++++++++++++++ .../main/java/modelarmor/ListTemplates.java | 35 ++++++++++++++++++- .../modelarmor/ListTemplatesWithFilter.java | 35 +++++++++++++++++++ .../src/test/java/modelarmor/SnippetsIT.java | 2 +- 6 files changed, 160 insertions(+), 2 deletions(-) diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplate.java b/modelarmor/src/main/java/modelarmor/CreateTemplate.java index 3d3e0b44c3d..40c8cc2b8ef 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplate.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplate.java @@ -16,6 +16,8 @@ package modelarmor; +// [START modelarmor_create_template] + import com.google.cloud.modelarmor.v1.CreateTemplateRequest; import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; import com.google.cloud.modelarmor.v1.FilterConfig; @@ -29,8 +31,15 @@ import com.google.protobuf.util.JsonFormat; import java.util.List; +/** This class contains a main method that creates a template using the Model Armor API. */ public class CreateTemplate { + /** + * Main method that calls the createTemplate method to create a template. + * + * @param args command line arguments (not used) + * @throws Exception if an error occurs during template creation + */ public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; @@ -40,15 +49,29 @@ public static void main(String[] args) throws Exception { createTemplate(projectId, locationId, templateId); } + /** + * Creates a template using the Model Armor API. + * + * @param projectId the ID of the project + * @param locationId the ID of the location + * @param templateId the ID of the template + * @return the created template + * @throws Exception if an error occurs during template creation + */ public static Template createTemplate(String projectId, String locationId, String templateId) throws Exception { + // Construct the API endpoint URL String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + + // Create a Model Armor settings object with the API endpoint ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + // Construct the parent resource name String parent = LocationName.of(projectId, locationId).toString(); + // Create a template object with a filter config Template template = Template.newBuilder() .setFilterConfig( @@ -65,6 +88,7 @@ public static Template createTemplate(String projectId, String locationId, Strin .build()) .build(); + // Create a create template request object CreateTemplateRequest request = CreateTemplateRequest.newBuilder() .setParent(parent) @@ -72,9 +96,15 @@ public static Template createTemplate(String projectId, String locationId, Strin .setTemplate(template) .build(); + // Create the template using the Model Armor client Template createdTemplate = client.createTemplate(request); + + // Print the created template System.out.println("Created template: " + JsonFormat.printer().print(createdTemplate)); + return createdTemplate; } } } + +// [END modelarmor_create_template] diff --git a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java index 7e2f2f49529..9bdfc04d443 100644 --- a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java +++ b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java @@ -16,12 +16,21 @@ package modelarmor; +// [START modelarmor_delete_template] + import com.google.cloud.modelarmor.v1.ModelArmorClient; import com.google.cloud.modelarmor.v1.ModelArmorSettings; import com.google.cloud.modelarmor.v1.TemplateName; +/** This class contains a main method that deletes a template using the Model Armor API. */ public class DeleteTemplate { + /** + * Main method that calls the deleteTemplate method to delete a template. + * + * @param args command line arguments (not used) + * @throws Exception if an error occurs during template deletion + */ public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; @@ -31,15 +40,34 @@ public static void main(String[] args) throws Exception { deleteTemplate(projectId, locationId, templateId); } + /** + * Deletes a template using the Model Armor API. + * + * @param projectId the ID of the project + * @param locationId the ID of the location + * @param templateId the ID of the template + * @throws Exception if an error occurs during template deletion + */ public static void deleteTemplate(String projectId, String locationId, String templateId) throws Exception { + // Construct the API endpoint URL String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + + // Create a Model Armor settings object with the API endpoint ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + // Construct the template name String name = TemplateName.of(projectId, locationId, templateId).toString(); + + // Delete the template using the Model Armor client client.deleteTemplate(name); + + // Print a success message System.out.println("Deleted template: " + name); } } } + +// [END modelarmor_delete_template] diff --git a/modelarmor/src/main/java/modelarmor/GetTemplate.java b/modelarmor/src/main/java/modelarmor/GetTemplate.java index 0909cd80879..02423710f0b 100644 --- a/modelarmor/src/main/java/modelarmor/GetTemplate.java +++ b/modelarmor/src/main/java/modelarmor/GetTemplate.java @@ -16,14 +16,28 @@ package modelarmor; +/** + * This class demonstrates how to retrieve a template using the Model Armor API. + * + * @author [Your Name] + */ 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.protobuf.util.JsonFormat; +// [START modelarmor_get_template] + +/** This class contains a main method that retrieves a template using the Model Armor API. */ public class GetTemplate { + /** + * Main method that calls the getTemplate method to retrieve a template. + * + * @param args command line arguments (not used) + * @throws Exception if an error occurs during template retrieval + */ public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; @@ -33,16 +47,34 @@ public static void main(String[] args) throws Exception { getTemplate(projectId, locationId, templateId); } + /** + * Retrieves a template using the Model Armor API. + * + * @param projectId the ID of the project + * @param locationId the ID of the location + * @param templateId the ID of the template + * @throws Exception if an error occurs during template retrieval + */ public static void getTemplate(String projectId, String locationId, String templateId) throws Exception { + // Construct the API endpoint URL String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + + // Create a Model Armor settings object with the API endpoint ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + // Construct the template name String name = TemplateName.of(projectId, locationId, templateId).toString(); + + // Retrieve the template using the Model Armor client Template template = client.getTemplate(name); + + // Print the retrieved template System.out.println("Retrieved template: " + JsonFormat.printer().print(template)); } } } + +// [END modelarmor_get_template] diff --git a/modelarmor/src/main/java/modelarmor/ListTemplates.java b/modelarmor/src/main/java/modelarmor/ListTemplates.java index c58eb046be8..458cf62a32e 100644 --- a/modelarmor/src/main/java/modelarmor/ListTemplates.java +++ b/modelarmor/src/main/java/modelarmor/ListTemplates.java @@ -16,6 +16,14 @@ package modelarmor; +/** + * This class demonstrates how to list templates using the Model Armor API. + * + * @author [Your Name] + */ + +// [START modelarmor_list_templates] + import com.google.cloud.modelarmor.v1.ListTemplatesRequest; import com.google.cloud.modelarmor.v1.LocationName; import com.google.cloud.modelarmor.v1.ModelArmorClient; @@ -23,8 +31,15 @@ import com.google.cloud.modelarmor.v1.Template; import com.google.protobuf.util.JsonFormat; +/** This class contains a main method that lists templates using the Model Armor API. */ public class ListTemplates { + /** + * Main method that calls the listTemplates method to list templates. + * + * @param args command line arguments (not used) + * @throws Exception if an error occurs during template listing + */ public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; @@ -33,17 +48,35 @@ public static void main(String[] args) throws Exception { listTemplates(projectId, locationId); } + /** + * Lists templates using the Model Armor API. + * + * @param projectId the ID of the project + * @param locationId the ID of the location + * @throws Exception if an error occurs during template listing + */ public static void listTemplates(String projectId, String locationId) throws Exception { + // Construct the API endpoint URL String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + + // Create a Model Armor settings object with the API endpoint ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + // Construct the parent resource name String parent = LocationName.of(projectId, locationId).toString(); + + // Create a list templates request object ListTemplatesRequest request = ListTemplatesRequest.newBuilder().setParent(parent).build(); + + // List templates using the Model Armor client for (Template template : client.listTemplates(request).iterateAll()) { - System.out.println("Retrived Templates: " + JsonFormat.printer().print(template)); + // Print each retrieved template + System.out.println("Retrieved Templates: " + JsonFormat.printer().print(template)); } } } } + +// [END modelarmor_list_templates] diff --git a/modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java b/modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java index 590ef9b4f29..34a24b0cc06 100644 --- a/modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java +++ b/modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java @@ -16,6 +16,11 @@ package modelarmor; +/** + * This class demonstrates how to list templates with a filter using the Model Armor API. + * + * @author [Your Name] + */ import com.google.cloud.modelarmor.v1.ListTemplatesRequest; import com.google.cloud.modelarmor.v1.LocationName; import com.google.cloud.modelarmor.v1.ModelArmorClient; @@ -23,8 +28,19 @@ import com.google.cloud.modelarmor.v1.Template; import com.google.protobuf.util.JsonFormat; +// [START modelarmor_list_templates_with_filter] + +/** + * This class contains a main method that lists templates with a filter using the Model Armor API. + */ public class ListTemplatesWithFilter { + /** + * Main method that calls the listTemplatesWithFilter method to list templates with a filter. + * + * @param args command line arguments (not used) + * @throws Exception if an error occurs during template listing + */ public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; @@ -34,18 +50,37 @@ public static void main(String[] args) throws Exception { listTemplatesWithFilter(projectId, locationId, templateId); } + /** + * Lists templates with a filter using the Model Armor API. + * + * @param projectId the ID of the project + * @param locationId the ID of the location + * @param templateId the ID of the template + * @throws Exception if an error occurs during template listing + */ public static void listTemplatesWithFilter(String projectId, String locationId, String templateId) throws Exception { + // Construct the API endpoint URL String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + + // Create a Model Armor settings object with the API endpoint ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + // Construct the parent resource name String parent = LocationName.of(projectId, locationId).toString(); + + // Construct the filter string String filter = String.format("name=\"%s/templates/%s\"", parent, templateId); + + // Create a list templates request object with the filter ListTemplatesRequest request = ListTemplatesRequest.newBuilder().setParent(parent).setFilter(filter).build(); + + // List templates using the Model Armor client for (Template template : client.listTemplates(request).iterateAll()) { + // Print each retrieved template System.out.println("Template with filter: " + JsonFormat.printer().print(template)); } } diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index 0321a7121fc..bb717c1c2be 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -97,7 +97,7 @@ public void testGetModelArmorTemplate() throws Exception { public void testListModelArmorTemplates() throws Exception { CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); ListTemplates.listTemplates(PROJECT_ID, LOCATION); - assertThat(stdOut.toString()).contains("Retrived Templates"); + assertThat(stdOut.toString()).contains("Retrieved Templates"); DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); } From d851777b8e33cdd3aa0be6a7b2620211795e32f2 Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Tue, 15 Apr 2025 18:08:49 +0530 Subject: [PATCH 05/12] address-review-comments --- .../main/java/modelarmor/CreateTemplate.java | 101 +++++++++--------- .../main/java/modelarmor/DeleteTemplate.java | 38 ++----- .../src/main/java/modelarmor/GetTemplate.java | 54 ++++------ .../main/java/modelarmor/ListTemplates.java | 60 ++++------- .../modelarmor/ListTemplatesWithFilter.java | 78 ++++++-------- .../src/test/java/modelarmor/SnippetsIT.java | 94 +++++++++------- 6 files changed, 189 insertions(+), 236 deletions(-) diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplate.java b/modelarmor/src/main/java/modelarmor/CreateTemplate.java index 40c8cc2b8ef..1f1b625554a 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplate.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplate.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; @@ -28,19 +28,12 @@ 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.protobuf.util.JsonFormat; +import java.io.IOException; import java.util.List; -/** This class contains a main method that creates a template using the Model Armor API. */ public class CreateTemplate { - /** - * Main method that calls the createTemplate method to create a template. - * - * @param args command line arguments (not used) - * @throws Exception if an error occurs during template creation - */ - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String locationId = "your-location-id"; @@ -49,58 +42,62 @@ public static void main(String[] args) throws Exception { createTemplate(projectId, locationId, templateId); } - /** - * Creates a template using the Model Armor API. - * - * @param projectId the ID of the project - * @param locationId the ID of the location - * @param templateId the ID of the template - * @return the created template - * @throws Exception if an error occurs during template creation - */ public static Template createTemplate(String projectId, String locationId, String templateId) - throws Exception { - // Construct the API endpoint URL + 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(); - // Create a Model Armor settings object with the API endpoint - 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)) { - // Construct the parent resource name String parent = LocationName.of(projectId, locationId).toString(); - // Create a template object with a filter config - Template template = - Template.newBuilder() - .setFilterConfig( - FilterConfig.newBuilder() - .setRaiSettings( - RaiFilterSettings.newBuilder() - .addAllRaiFilters( - List.of( - RaiFilter.newBuilder() - .setFilterType(RaiFilterType.DANGEROUS) - .setConfidenceLevel(DetectionConfidenceLevel.HIGH) - .build())) - .build()) - .build()) - .build(); + // 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 - // Create a create template request object - CreateTemplateRequest request = - CreateTemplateRequest.newBuilder() - .setParent(parent) - .setTemplateId(templateId) - .setTemplate(template) + // Configure Responsible AI filter with multiple categories and their confidence + // levels. + RaiFilterSettings raiFilterSettings = + RaiFilterSettings.newBuilder() + .addAllRaiFilters( + List.of( + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HATE_SPEECH) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build())) .build(); - // Create the template using the Model Armor client - Template createdTemplate = client.createTemplate(request); + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setRaiSettings(raiFilterSettings) + .build(); - // Print the created template - System.out.println("Created template: " + JsonFormat.printer().print(createdTemplate)); + Template template = Template.newBuilder() + .setFilterConfig(modelArmorFilter) + .build(); + + CreateTemplateRequest request = CreateTemplateRequest.newBuilder() + .setParent(parent) + .setTemplateId(templateId) + .setTemplate(template) + .build(); + + Template createdTemplate = client.createTemplate(request); + System.out.println("Created template: " + createdTemplate.getName()); return createdTemplate; } diff --git a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java index 9bdfc04d443..3a6fef7e53b 100644 --- a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java +++ b/modelarmor/src/main/java/modelarmor/DeleteTemplate.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; @@ -21,17 +21,11 @@ import com.google.cloud.modelarmor.v1.ModelArmorClient; import com.google.cloud.modelarmor.v1.ModelArmorSettings; import com.google.cloud.modelarmor.v1.TemplateName; +import java.io.IOException; -/** This class contains a main method that deletes a template using the Model Armor API. */ public class DeleteTemplate { - /** - * Main method that calls the deleteTemplate method to delete a template. - * - * @param args command line arguments (not used) - * @throws Exception if an error occurs during template deletion - */ - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String locationId = "your-location-id"; @@ -40,34 +34,24 @@ public static void main(String[] args) throws Exception { deleteTemplate(projectId, locationId, templateId); } - /** - * Deletes a template using the Model Armor API. - * - * @param projectId the ID of the project - * @param locationId the ID of the location - * @param templateId the ID of the template - * @throws Exception if an error occurs during template deletion - */ public static void deleteTemplate(String projectId, String locationId, String templateId) - throws Exception { - // Construct the API endpoint URL + 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(); - // Create a Model Armor settings object with the API endpoint - 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)) { - // Construct the template name String name = TemplateName.of(projectId, locationId, templateId).toString(); - // Delete the template using the Model Armor client + // Note: Ensure that the template you are deleting isn't used by any models. client.deleteTemplate(name); - - // Print a success message System.out.println("Deleted template: " + name); } } } // [END modelarmor_delete_template] + \ No newline at end of file diff --git a/modelarmor/src/main/java/modelarmor/GetTemplate.java b/modelarmor/src/main/java/modelarmor/GetTemplate.java index 02423710f0b..686268c9141 100644 --- a/modelarmor/src/main/java/modelarmor/GetTemplate.java +++ b/modelarmor/src/main/java/modelarmor/GetTemplate.java @@ -12,33 +12,21 @@ * 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; -/** - * This class demonstrates how to retrieve a template using the Model Armor API. - * - * @author [Your Name] - */ +// [START modelarmor_get_template] + 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.protobuf.util.JsonFormat; +import java.io.IOException; -// [START modelarmor_get_template] - -/** This class contains a main method that retrieves a template using the Model Armor API. */ public class GetTemplate { - /** - * Main method that calls the getTemplate method to retrieve a template. - * - * @param args command line arguments (not used) - * @throws Exception if an error occurs during template retrieval - */ - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String locationId = "your-location-id"; @@ -47,32 +35,28 @@ public static void main(String[] args) throws Exception { getTemplate(projectId, locationId, templateId); } - /** - * Retrieves a template using the Model Armor API. - * - * @param projectId the ID of the project - * @param locationId the ID of the location - * @param templateId the ID of the template - * @throws Exception if an error occurs during template retrieval - */ - public static void getTemplate(String projectId, String locationId, String templateId) - throws Exception { - // Construct the API endpoint URL + public static Template getTemplate(String projectId, String locationId, String templateId) + throws IOException { + // Construct the API endpoint URL. String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - // Create a Model Armor settings object with the API endpoint - ModelArmorSettings modelArmorSettings = - ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + 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)) { - // Construct the template name + // Build the template name. String name = TemplateName.of(projectId, locationId, templateId).toString(); - // Retrieve the template using the Model Armor client + // Get the template. Template template = client.getTemplate(name); - // Print the retrieved template - System.out.println("Retrieved template: " + JsonFormat.printer().print(template)); + // Find more details about Template object here: + // https://cloud.google.com/security-command-center/docs/reference/model-armor/rest/v1/projects.locations.templates#Template + System.out.printf("Retrieved template: %s\n", template.getName()); + + return template; } } } diff --git a/modelarmor/src/main/java/modelarmor/ListTemplates.java b/modelarmor/src/main/java/modelarmor/ListTemplates.java index 458cf62a32e..d83a955010e 100644 --- a/modelarmor/src/main/java/modelarmor/ListTemplates.java +++ b/modelarmor/src/main/java/modelarmor/ListTemplates.java @@ -12,35 +12,22 @@ * 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; -/** - * This class demonstrates how to list templates using the Model Armor API. - * - * @author [Your Name] - */ - // [START modelarmor_list_templates] import com.google.cloud.modelarmor.v1.ListTemplatesRequest; import com.google.cloud.modelarmor.v1.LocationName; import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorClient.ListTemplatesPagedResponse; import com.google.cloud.modelarmor.v1.ModelArmorSettings; -import com.google.cloud.modelarmor.v1.Template; -import com.google.protobuf.util.JsonFormat; +import java.io.IOException; -/** This class contains a main method that lists templates using the Model Armor API. */ public class ListTemplates { - /** - * Main method that calls the listTemplates method to list templates. - * - * @param args command line arguments (not used) - * @throws Exception if an error occurs during template listing - */ - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String locationId = "your-location-id"; @@ -48,33 +35,32 @@ public static void main(String[] args) throws Exception { listTemplates(projectId, locationId); } - /** - * Lists templates using the Model Armor API. - * - * @param projectId the ID of the project - * @param locationId the ID of the location - * @throws Exception if an error occurs during template listing - */ - public static void listTemplates(String projectId, String locationId) throws Exception { - // Construct the API endpoint URL + public static ListTemplatesPagedResponse listTemplates(String projectId, String locationId) + throws IOException { + // Construct the API endpoint URL. String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - // Create a Model Armor settings object with the API endpoint - ModelArmorSettings modelArmorSettings = - ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + 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)) { - // Construct the parent resource name + // Build the parent name. String parent = LocationName.of(projectId, locationId).toString(); - // Create a list templates request object - ListTemplatesRequest request = ListTemplatesRequest.newBuilder().setParent(parent).build(); + ListTemplatesRequest request = + ListTemplatesRequest.newBuilder() + .setParent(parent) + .build(); + + // List all templates. + ListTemplatesPagedResponse pagedResponse = client.listTemplates(request); + pagedResponse.iterateAll().forEach(template -> { + System.out.printf("Template %s\n", template.getName()); + }); - // List templates using the Model Armor client - for (Template template : client.listTemplates(request).iterateAll()) { - // Print each retrieved template - System.out.println("Retrieved Templates: " + JsonFormat.printer().print(template)); - } + return pagedResponse; } } } diff --git a/modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java b/modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java index 34a24b0cc06..68e6998d162 100644 --- a/modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java +++ b/modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java @@ -12,77 +12,61 @@ * 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; -/** - * This class demonstrates how to list templates with a filter using the Model Armor API. - * - * @author [Your Name] - */ +// [START modelarmor_list_templates_with_filter] + import com.google.cloud.modelarmor.v1.ListTemplatesRequest; import com.google.cloud.modelarmor.v1.LocationName; import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorClient.ListTemplatesPagedResponse; import com.google.cloud.modelarmor.v1.ModelArmorSettings; -import com.google.cloud.modelarmor.v1.Template; -import com.google.protobuf.util.JsonFormat; +import java.io.IOException; -// [START modelarmor_list_templates_with_filter] - -/** - * This class contains a main method that lists templates with a filter using the Model Armor API. - */ public class ListTemplatesWithFilter { - /** - * Main method that calls the listTemplatesWithFilter method to list templates with a filter. - * - * @param args command line arguments (not used) - * @throws Exception if an error occurs during template listing - */ - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; String locationId = "your-location-id"; - String templateId = "your-template-id"; + // Filter to applied. + // Example: "name=\"projects/your-project-id/locations/us-central1/your-template-id\"" + String filter = "your-filter-condition"; - listTemplatesWithFilter(projectId, locationId, templateId); + listTemplatesWithFilter(projectId, locationId, filter); } - /** - * Lists templates with a filter using the Model Armor API. - * - * @param projectId the ID of the project - * @param locationId the ID of the location - * @param templateId the ID of the template - * @throws Exception if an error occurs during template listing - */ - public static void listTemplatesWithFilter(String projectId, String locationId, String templateId) - throws Exception { - // Construct the API endpoint URL + public static ListTemplatesPagedResponse listTemplatesWithFilter(String projectId, + String locationId, String filter) throws IOException { + // Construct the API endpoint URL. String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - // Create a Model Armor settings object with the API endpoint - ModelArmorSettings modelArmorSettings = - ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + 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)) { - // Construct the parent resource name + // Build the parent name. String parent = LocationName.of(projectId, locationId).toString(); - // Construct the filter string - String filter = String.format("name=\"%s/templates/%s\"", parent, templateId); + ListTemplatesRequest request = ListTemplatesRequest.newBuilder() + .setParent(parent) + .setFilter(filter) + .build(); - // Create a list templates request object with the filter - ListTemplatesRequest request = - ListTemplatesRequest.newBuilder().setParent(parent).setFilter(filter).build(); + // List all templates. + ListTemplatesPagedResponse pagedResponse = client.listTemplates(request); + pagedResponse.iterateAll().forEach(template -> { + System.out.printf("Template %s\n", template.getName()); + }); - // List templates using the Model Armor client - for (Template template : client.listTemplates(request).iterateAll()) { - // Print each retrieved template - System.out.println("Template with filter: " + JsonFormat.printer().print(template)); - } + return pagedResponse; } } } + +// [END modelarmor_list_templates_with_filter] diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index bb717c1c2be..b1a730f54eb 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -12,17 +12,22 @@ * 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; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import com.google.api.gax.rpc.NotFoundException; import com.google.cloud.modelarmor.v1.Template; +import com.google.cloud.modelarmor.v1.TemplateName; import com.google.common.base.Strings; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; -import java.util.UUID; +import java.util.Random; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; @@ -36,32 +41,23 @@ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:AbbreviationAsWordInName") public class SnippetsIT { - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String LOCATION = "us-central1"; - private static final String MA_REGIONAL_ENDPOINT = - String.format("modelarmor.%s.rep.googleapis.com:443", LOCATION); - private static final String DLP_REGIONAL_ENDPOINT = - String.format("dlp.%s.rep.googleapis.com:443", LOCATION); - private static final String INSPECT_TEMPLATE_ID = - "model-armour-inspect-template-" + UUID.randomUUID().toString(); - private static final String DEIDENTIFY_TEMPLATE_ID = - "model-armour-deidentify-template-" + UUID.randomUUID().toString(); - private static Template TEST_MODELARMOR_TEMPLATE; - private static Template TEST_MODELARMOR_TEMPLATE_NAME; - private static String TEMPLATE_ID; - + private static final String LOCATION_ID = System.getenv() + .getOrDefault("GOOGLE_CLOUD_PROJECT_LOCATION", "us-central1"); + private static String TEST_TEMPLATE_ID; + private static String TEST_TEMPLATE_NAME; private ByteArrayOutputStream stdOut; @BeforeClass - public static void beforeAll() throws Exception { + public static void beforeAll() throws IOException { Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION)); + Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION_ID)); } @AfterClass - public static void afterAll() throws Exception { + public static void afterAll() throws IOException { Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); + Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION_ID)); } @Before @@ -69,43 +65,65 @@ public void beforeEach() { stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); - TEMPLATE_ID = "test-model-armor-" + UUID.randomUUID().toString(); + TEST_TEMPLATE_ID = randomId(); + TEST_TEMPLATE_NAME = TemplateName.of(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID).toString(); } @After - public void afterEach() throws Exception { + public void afterEach() throws IOException { + try { + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + } catch (NotFoundException e) { + // Ignore not found error - template already deleted. + } + stdOut = null; System.setOut(null); } - @Test - public void testDeleteModelArmorTemplate() throws Exception { - CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); - DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Deleted template"); + private static String randomId() { + Random random = new Random(); + return "java-ma-" + random.nextLong(); } @Test public void testGetModelArmorTemplate() throws Exception { - CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); - GetTemplate.getTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Retrieved template"); - DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + Template retrievedTemplate = GetTemplate.getTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + + assertThat(stdOut.toString()).contains("Retrieved template:"); + assertEquals(retrievedTemplate.getName(), TEST_TEMPLATE_NAME); } @Test public void testListModelArmorTemplates() throws Exception { - CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); - ListTemplates.listTemplates(PROJECT_ID, LOCATION); - assertThat(stdOut.toString()).contains("Retrieved Templates"); - DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + + ListTemplates.listTemplates(PROJECT_ID, LOCATION_ID); + + boolean templatePresentInList = false; + for (Template template : ListTemplates.listTemplates(PROJECT_ID, LOCATION_ID).iterateAll()) { + if (TEST_TEMPLATE_NAME.equals(template.getName())) { + templatePresentInList = true; + } + } + assertTrue(templatePresentInList); } @Test public void testListTemplatesWithFilter() throws Exception { - CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); - ListTemplatesWithFilter.listTemplatesWithFilter(PROJECT_ID, LOCATION, TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Template with filter"); - DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + String filter = "name=\"projects/" + PROJECT_ID + "/locations/" + LOCATION_ID + "/" + + TEST_TEMPLATE_ID + "\""; + + ListTemplatesWithFilter.listTemplatesWithFilter(PROJECT_ID, LOCATION_ID, filter); + + boolean templatePresentInList = false; + for (Template template : ListTemplates.listTemplates(PROJECT_ID, LOCATION_ID).iterateAll()) { + if (TEST_TEMPLATE_NAME.equals(template.getName())) { + templatePresentInList = true; + } + } + assertTrue(templatePresentInList); } } From a06a1ceeef6cf27cb6e91e397c60d722bbe074b6 Mon Sep 17 00:00:00 2001 From: harshnasitcrest <131268456+harshnasitcrest@users.noreply.github.com> Date: Tue, 15 Apr 2025 19:05:37 +0530 Subject: [PATCH 06/12] update-exception --- modelarmor/src/test/java/modelarmor/SnippetsIT.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index b1a730f54eb..c300e23c666 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -87,7 +87,7 @@ private static String randomId() { } @Test - public void testGetModelArmorTemplate() throws Exception { + public void testGetModelArmorTemplate() throws IOException { CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); Template retrievedTemplate = GetTemplate.getTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); @@ -96,7 +96,7 @@ public void testGetModelArmorTemplate() throws Exception { } @Test - public void testListModelArmorTemplates() throws Exception { + public void testListModelArmorTemplates() throws IOException { CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); ListTemplates.listTemplates(PROJECT_ID, LOCATION_ID); @@ -111,7 +111,7 @@ public void testListModelArmorTemplates() throws Exception { } @Test - public void testListTemplatesWithFilter() throws Exception { + public void testListTemplatesWithFilter() throws IOException { CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); String filter = "name=\"projects/" + PROJECT_ID + "/locations/" + LOCATION_ID + "/" + TEST_TEMPLATE_ID + "\""; From 64b62bf94f35272799586ac927623025b7c3242e Mon Sep 17 00:00:00 2001 From: harshnasitcrest <131268456+harshnasitcrest@users.noreply.github.com> Date: Tue, 15 Apr 2025 22:23:35 +0530 Subject: [PATCH 07/12] remove-unused-annotation --- modelarmor/src/test/java/modelarmor/SnippetsIT.java | 1 - 1 file changed, 1 deletion(-) diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index c300e23c666..43db7ca0883 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -39,7 +39,6 @@ /** Integration (system) tests for {@link Snippets}. */ @RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:AbbreviationAsWordInName") public class SnippetsIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String LOCATION_ID = System.getenv() From 135fa7334e9d01fd342015cdccc9d5d92a331efb Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Wed, 16 Apr 2025 19:08:50 +0530 Subject: [PATCH 08/12] sync-requireenvvar-function-usage --- .../src/test/java/modelarmor/SnippetsIT.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index 43db7ca0883..fefe47ad465 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -18,19 +18,18 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.google.api.gax.rpc.NotFoundException; import com.google.cloud.modelarmor.v1.Template; import com.google.cloud.modelarmor.v1.TemplateName; -import com.google.common.base.Strings; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.Random; import org.junit.After; import org.junit.AfterClass; -import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -47,16 +46,24 @@ public class SnippetsIT { private static String TEST_TEMPLATE_NAME; private ByteArrayOutputStream stdOut; + // Check if the required environment variables are set. + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull("Environment variable " + varName + " is required to run these tests.", + System.getenv(varName)); + return value; + } + @BeforeClass - public static void beforeAll() throws IOException { - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION_ID)); + public static void beforeAll() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("GOOGLE_CLOUD_PROJECT_LOCATION"); } @AfterClass public static void afterAll() throws IOException { - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION_ID)); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("GOOGLE_CLOUD_PROJECT_LOCATION"); } @Before From c0766f93776a68dc14b9e0a6a151fc2d9f9aeac0 Mon Sep 17 00:00:00 2001 From: harshnasitcrest <131268456+harshnasitcrest@users.noreply.github.com> Date: Thu, 17 Apr 2025 16:02:06 +0530 Subject: [PATCH 09/12] remove-require-env-location-condition --- modelarmor/src/test/java/modelarmor/SnippetsIT.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index fefe47ad465..2efef816538 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -57,13 +57,11 @@ private static String requireEnvVar(String varName) { @BeforeClass public static void beforeAll() { requireEnvVar("GOOGLE_CLOUD_PROJECT"); - requireEnvVar("GOOGLE_CLOUD_PROJECT_LOCATION"); } @AfterClass public static void afterAll() throws IOException { requireEnvVar("GOOGLE_CLOUD_PROJECT"); - requireEnvVar("GOOGLE_CLOUD_PROJECT_LOCATION"); } @Before From b6abdadb60680f2e568106f723a527687270f1fb Mon Sep 17 00:00:00 2001 From: harshnasitcrest <131268456+harshnasitcrest@users.noreply.github.com> Date: Fri, 18 Apr 2025 13:08:00 +0530 Subject: [PATCH 10/12] address-review-comment Ref: https://github.com/GoogleCloudPlatform/java-docs-samples/pull/10069#discussion_r2048341435 --- modelarmor/src/test/java/modelarmor/SnippetsIT.java | 1 - 1 file changed, 1 deletion(-) diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index 2efef816538..f323e7fb4e7 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -95,7 +95,6 @@ public void testGetModelArmorTemplate() throws IOException { CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); Template retrievedTemplate = GetTemplate.getTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Retrieved template:"); assertEquals(retrievedTemplate.getName(), TEST_TEMPLATE_NAME); } From bc405db085934c68647ec0beba360ee88df85ff0 Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Wed, 7 May 2025 23:01:56 +0530 Subject: [PATCH 11/12] resolve-merge-conflicts --- modelarmor/src/main/java/modelarmor/CreateTemplate.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplate.java b/modelarmor/src/main/java/modelarmor/CreateTemplate.java index a34275a8e7a..f695cb36ede 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"; From 47b255b3792b814e6413b81a1206726afb83e8c8 Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Wed, 7 May 2025 23:02:33 +0530 Subject: [PATCH 12/12] resolve-merge-conflicts --- modelarmor/src/test/java/modelarmor/SnippetsIT.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index 1341f86a8c9..a627f7f93b2 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -292,12 +292,12 @@ private static DeidentifyTemplate createDeidentifyTemplate(String templateId) th .setDeidentifyConfig(redactConfig) .build(); - CreateDeidentifyTemplateRequest createDeidentifyTemplateRequest = CreateDeidentifyTemplateRequest - .newBuilder() - .setParent(LocationName.of(PROJECT_ID, LOCATION_ID).toString()) - .setTemplateId(templateId) - .setDeidentifyTemplate(deidentifyTemplate) - .build(); + CreateDeidentifyTemplateRequest createDeidentifyTemplateRequest = + CreateDeidentifyTemplateRequest.newBuilder() + .setParent(LocationName.of(PROJECT_ID, LOCATION_ID).toString()) + .setTemplateId(templateId) + .setDeidentifyTemplate(deidentifyTemplate) + .build(); return dlpServiceClient.createDeidentifyTemplate(createDeidentifyTemplateRequest); }