Skip to content

Commit d40e2b2

Browse files
Added code samples for delete, get and list model armor templates
1 parent 2771ec8 commit d40e2b2

File tree

5 files changed

+306
-0
lines changed

5 files changed

+306
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package modelarmor;
18+
19+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
20+
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
21+
import com.google.cloud.modelarmor.v1.TemplateName;
22+
23+
public class DeleteTemplate {
24+
25+
public static void main(String[] args) throws Exception {
26+
// TODO(developer): Replace these variables before running the sample.
27+
String projectId = "your-project-id";
28+
String locationId = "your-location-id";
29+
String templateId = "your-template-id";
30+
31+
deleteTemplate(projectId, locationId, templateId);
32+
}
33+
34+
public static void deleteTemplate(String projectId, String locationId, String templateId)
35+
throws Exception {
36+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
37+
ModelArmorSettings modelArmorSettings =
38+
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
39+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
40+
String name = TemplateName.of(projectId, locationId, templateId).toString();
41+
client.deleteTemplate(name);
42+
System.out.println("Deleted template: " + name);
43+
}
44+
}
45+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package modelarmor;
18+
19+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
20+
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
21+
import com.google.cloud.modelarmor.v1.Template;
22+
import com.google.cloud.modelarmor.v1.TemplateName;
23+
import com.google.protobuf.util.JsonFormat;
24+
25+
public class GetTemplate {
26+
27+
public static void main(String[] args) throws Exception {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String projectId = "your-project-id";
30+
String locationId = "your-location-id";
31+
String templateId = "your-template-id";
32+
33+
getTemplate(projectId, locationId, templateId);
34+
}
35+
36+
public static void getTemplate(String projectId, String locationId, String templateId)
37+
throws Exception {
38+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
39+
ModelArmorSettings modelArmorSettings =
40+
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
41+
42+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
43+
String name = TemplateName.of(projectId, locationId, templateId).toString();
44+
Template template = client.getTemplate(name);
45+
System.out.println("Retrieved template: " + JsonFormat.printer().print(template));
46+
}
47+
}
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package modelarmor;
18+
19+
import com.google.cloud.modelarmor.v1.ListTemplatesRequest;
20+
import com.google.cloud.modelarmor.v1.LocationName;
21+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
22+
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
23+
import com.google.cloud.modelarmor.v1.Template;
24+
import com.google.protobuf.util.JsonFormat;
25+
26+
public class ListTemplates {
27+
28+
public static void main(String[] args) throws Exception {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "your-project-id";
31+
String locationId = "your-location-id";
32+
33+
listTemplates(projectId, locationId);
34+
}
35+
36+
public static void listTemplates(String projectId, String locationId) throws Exception {
37+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
38+
ModelArmorSettings modelArmorSettings =
39+
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
40+
41+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
42+
String parent = LocationName.of(projectId, locationId).toString();
43+
ListTemplatesRequest request = ListTemplatesRequest.newBuilder().setParent(parent).build();
44+
for (Template template : client.listTemplates(request).iterateAll()) {
45+
System.out.println("Retrived Templates: " + JsonFormat.printer().print(template));
46+
}
47+
}
48+
}
49+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package modelarmor;
18+
19+
import com.google.cloud.modelarmor.v1.ListTemplatesRequest;
20+
import com.google.cloud.modelarmor.v1.LocationName;
21+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
22+
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
23+
import com.google.cloud.modelarmor.v1.Template;
24+
import com.google.protobuf.util.JsonFormat;
25+
26+
public class ListTemplatesWithFilter {
27+
28+
public static void main(String[] args) throws Exception {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "your-project-id";
31+
String locationId = "your-location-id";
32+
String templateId = "your-template-id";
33+
34+
listTemplatesWithFilter(projectId, locationId, templateId);
35+
}
36+
37+
public static void listTemplatesWithFilter(String projectId, String locationId, String templateId)
38+
throws Exception {
39+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
40+
ModelArmorSettings modelArmorSettings =
41+
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
42+
43+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
44+
String parent = LocationName.of(projectId, locationId).toString();
45+
String filter = String.format("name=\"%s/templates/%s\"", parent, templateId);
46+
ListTemplatesRequest request =
47+
ListTemplatesRequest.newBuilder().setParent(parent).setFilter(filter).build();
48+
for (Template template : client.listTemplates(request).iterateAll()) {
49+
System.out.println("Template with filter: " + JsonFormat.printer().print(template));
50+
}
51+
}
52+
}
53+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package modelarmor;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.modelarmor.v1.Template;
22+
import com.google.common.base.Strings;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.PrintStream;
25+
import java.util.UUID;
26+
import org.junit.After;
27+
import org.junit.AfterClass;
28+
import org.junit.Assert;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
/** Integration (system) tests for {@link Snippets}. */
36+
@RunWith(JUnit4.class)
37+
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
38+
public class SnippetsIT {
39+
40+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
41+
private static final String LOCATION = "us-central1";
42+
private static final String MA_REGIONAL_ENDPOINT =
43+
String.format("modelarmor.%s.rep.googleapis.com:443", LOCATION);
44+
private static final String DLP_REGIONAL_ENDPOINT =
45+
String.format("dlp.%s.rep.googleapis.com:443", LOCATION);
46+
private static final String INSPECT_TEMPLATE_ID =
47+
"model-armour-inspect-template-" + UUID.randomUUID().toString();
48+
private static final String DEIDENTIFY_TEMPLATE_ID =
49+
"model-armour-deidentify-template-" + UUID.randomUUID().toString();
50+
private static Template TEST_MODELARMOR_TEMPLATE;
51+
private static Template TEST_MODELARMOR_TEMPLATE_NAME;
52+
private static String TEMPLATE_ID;
53+
54+
private ByteArrayOutputStream stdOut;
55+
56+
@BeforeClass
57+
public static void beforeAll() throws Exception {
58+
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
59+
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION));
60+
}
61+
62+
@AfterClass
63+
public static void afterAll() throws Exception {
64+
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
65+
}
66+
67+
@Before
68+
public void beforeEach() {
69+
stdOut = new ByteArrayOutputStream();
70+
System.setOut(new PrintStream(stdOut));
71+
72+
TEMPLATE_ID = "test-model-armor-" + UUID.randomUUID().toString();
73+
}
74+
75+
@After
76+
public void afterEach() throws Exception {
77+
stdOut = null;
78+
System.setOut(null);
79+
}
80+
81+
@Test
82+
public void testDeleteModelArmorTemplate() throws Exception {
83+
CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID);
84+
DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID);
85+
assertThat(stdOut.toString()).contains("Deleted template");
86+
}
87+
88+
@Test
89+
public void testGetModelArmorTemplate() throws Exception {
90+
CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID);
91+
GetTemplate.getTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID);
92+
assertThat(stdOut.toString()).contains("Retrieved template");
93+
DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID);
94+
}
95+
96+
@Test
97+
public void testListModelArmorTemplates() throws Exception {
98+
CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID);
99+
ListTemplates.listTemplates(PROJECT_ID, LOCATION);
100+
assertThat(stdOut.toString()).contains("Retrived Templates");
101+
DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID);
102+
}
103+
104+
@Test
105+
public void testListTemplatesWithFilter() throws Exception {
106+
CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID);
107+
ListTemplatesWithFilter.listTemplatesWithFilter(PROJECT_ID, LOCATION, TEMPLATE_ID);
108+
assertThat(stdOut.toString()).contains("Template with filter");
109+
DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID);
110+
}
111+
}

0 commit comments

Comments
 (0)