Skip to content

feat(modelarmor): Added code samples for delete, get and list model armor templates #10070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions modelarmor/main/java/modelarmor/DeleteTemplate.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
48 changes: 48 additions & 0 deletions modelarmor/main/java/modelarmor/GetTemplate.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
49 changes: 49 additions & 0 deletions modelarmor/main/java/modelarmor/ListTemplates.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
}
53 changes: 53 additions & 0 deletions modelarmor/main/java/modelarmor/ListTemplatesWithFilter.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
}
111 changes: 111 additions & 0 deletions modelarmor/test/java/modelarmor/SnippetsIT.java
Original file line number Diff line number Diff line change
@@ -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);
}
}