diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplate.java b/modelarmor/src/main/java/modelarmor/CreateTemplate.java index dea12f19a8b..f695cb36ede 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; @@ -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"; @@ -48,7 +48,6 @@ public static void main(String[] args) throws IOException { public static Template createTemplate(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/DeleteTemplate.java b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java index 20cdb670cb0..0c76c44c5da 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; @@ -30,7 +30,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"; @@ -40,7 +40,6 @@ 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/GetTemplate.java b/modelarmor/src/main/java/modelarmor/GetTemplate.java new file mode 100644 index 00000000000..686268c9141 --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/GetTemplate.java @@ -0,0 +1,64 @@ +/* + * 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_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 java.io.IOException; + +public class GetTemplate { + + 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"; + + getTemplate(projectId, locationId, templateId); + } + + 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); + + 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)) { + // Build the template name. + String name = TemplateName.of(projectId, locationId, templateId).toString(); + + // Get the template. + Template template = client.getTemplate(name); + + // 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; + } + } +} + +// [END modelarmor_get_template] diff --git a/modelarmor/src/main/java/modelarmor/ListTemplates.java b/modelarmor/src/main/java/modelarmor/ListTemplates.java new file mode 100644 index 00000000000..d83a955010e --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/ListTemplates.java @@ -0,0 +1,68 @@ +/* + * 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_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 java.io.IOException; + +public class ListTemplates { + + 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"; + + listTemplates(projectId, locationId); + } + + 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); + + 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)) { + // Build the parent name. + String parent = LocationName.of(projectId, locationId).toString(); + + 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()); + }); + + return pagedResponse; + } + } +} + +// [END modelarmor_list_templates] diff --git a/modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java b/modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java new file mode 100644 index 00000000000..68e6998d162 --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java @@ -0,0 +1,72 @@ +/* + * 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_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 java.io.IOException; + +public class ListTemplatesWithFilter { + + 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"; + // Filter to applied. + // Example: "name=\"projects/your-project-id/locations/us-central1/your-template-id\"" + String filter = "your-filter-condition"; + + listTemplatesWithFilter(projectId, locationId, filter); + } + + 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); + + 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)) { + // Build the parent name. + String parent = LocationName.of(projectId, locationId).toString(); + + 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()); + }); + + 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 3ba8ca22fe4..a627f7f93b2 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -19,6 +19,7 @@ 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.dlp.v2.DlpServiceClient; @@ -134,6 +135,45 @@ private static String randomId() { } @Test + 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); + + assertEquals(retrievedTemplate.getName(), TEST_TEMPLATE_NAME); + } + + @Test + public void testListModelArmorTemplates() throws IOException { + 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 IOException { + 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); + } + public void testCreateModelArmorTemplate() throws IOException { Template createdTemplate = CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID);