Skip to content

Commit 414e26b

Browse files
feat(modelarmor): Added code samples for delete, get and list model armor templates (#10070)
* Added code samples for delete, get and list model armor templates * Added create template code snippet * feat(modelarmor): refactor code * feat(modelarmor): refactor code * address-review-comments * update-exception * remove-unused-annotation * sync-requireenvvar-function-usage * remove-require-env-location-condition * address-review-comment Ref: #10069 (comment) * resolve-merge-conflicts * resolve-merge-conflicts
1 parent f8206cc commit 414e26b

File tree

6 files changed

+248
-6
lines changed

6 files changed

+248
-6
lines changed

modelarmor/src/main/java/modelarmor/CreateTemplate.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/
15+
*/
1616

1717
package modelarmor;
1818

@@ -38,7 +38,7 @@ public static void main(String[] args) throws IOException {
3838

3939
// Specify the Google Project ID.
4040
String projectId = "your-project-id";
41-
// Specify the location ID. For example, us-central1.
41+
// Specify the location ID. For example, us-central1.
4242
String locationId = "your-location-id";
4343
// Specify the template ID.
4444
String templateId = "your-template-id";
@@ -48,7 +48,6 @@ public static void main(String[] args) throws IOException {
4848

4949
public static Template createTemplate(String projectId, String locationId, String templateId)
5050
throws IOException {
51-
5251
// Construct the API endpoint URL.
5352
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
5453
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)

modelarmor/src/main/java/modelarmor/DeleteTemplate.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/
15+
*/
1616

1717
package modelarmor;
1818

@@ -30,7 +30,7 @@ public static void main(String[] args) throws IOException {
3030

3131
// Specify the Google Project ID.
3232
String projectId = "your-project-id";
33-
// Specify the location ID. For example, us-central1.
33+
// Specify the location ID. For example, us-central1.
3434
String locationId = "your-location-id";
3535
// Specify the template ID.
3636
String templateId = "your-template-id";
@@ -40,7 +40,6 @@ public static void main(String[] args) throws IOException {
4040

4141
public static void deleteTemplate(String projectId, String locationId, String templateId)
4242
throws IOException {
43-
4443
// Construct the API endpoint URL.
4544
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
4645
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
// [START modelarmor_get_template]
20+
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.cloud.modelarmor.v1.TemplateName;
25+
import java.io.IOException;
26+
27+
public class GetTemplate {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "your-project-id";
32+
String locationId = "your-location-id";
33+
String templateId = "your-template-id";
34+
35+
getTemplate(projectId, locationId, templateId);
36+
}
37+
38+
public static Template getTemplate(String projectId, String locationId, String templateId)
39+
throws IOException {
40+
// Construct the API endpoint URL.
41+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
42+
43+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
44+
.build();
45+
46+
// Initialize the client that will be used to send requests. This client
47+
// only needs to be created once, and can be reused for multiple requests.
48+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
49+
// Build the template name.
50+
String name = TemplateName.of(projectId, locationId, templateId).toString();
51+
52+
// Get the template.
53+
Template template = client.getTemplate(name);
54+
55+
// Find more details about Template object here:
56+
// https://cloud.google.com/security-command-center/docs/reference/model-armor/rest/v1/projects.locations.templates#Template
57+
System.out.printf("Retrieved template: %s\n", template.getName());
58+
59+
return template;
60+
}
61+
}
62+
}
63+
64+
// [END modelarmor_get_template]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
// [START modelarmor_list_templates]
20+
21+
import com.google.cloud.modelarmor.v1.ListTemplatesRequest;
22+
import com.google.cloud.modelarmor.v1.LocationName;
23+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
24+
import com.google.cloud.modelarmor.v1.ModelArmorClient.ListTemplatesPagedResponse;
25+
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
26+
import java.io.IOException;
27+
28+
public class ListTemplates {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "your-project-id";
33+
String locationId = "your-location-id";
34+
35+
listTemplates(projectId, locationId);
36+
}
37+
38+
public static ListTemplatesPagedResponse listTemplates(String projectId, String locationId)
39+
throws IOException {
40+
// Construct the API endpoint URL.
41+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
42+
43+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
44+
.build();
45+
46+
// Initialize the client that will be used to send requests. This client
47+
// only needs to be created once, and can be reused for multiple requests.
48+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
49+
// Build the parent name.
50+
String parent = LocationName.of(projectId, locationId).toString();
51+
52+
ListTemplatesRequest request =
53+
ListTemplatesRequest.newBuilder()
54+
.setParent(parent)
55+
.build();
56+
57+
// List all templates.
58+
ListTemplatesPagedResponse pagedResponse = client.listTemplates(request);
59+
pagedResponse.iterateAll().forEach(template -> {
60+
System.out.printf("Template %s\n", template.getName());
61+
});
62+
63+
return pagedResponse;
64+
}
65+
}
66+
}
67+
68+
// [END modelarmor_list_templates]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
// [START modelarmor_list_templates_with_filter]
20+
21+
import com.google.cloud.modelarmor.v1.ListTemplatesRequest;
22+
import com.google.cloud.modelarmor.v1.LocationName;
23+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
24+
import com.google.cloud.modelarmor.v1.ModelArmorClient.ListTemplatesPagedResponse;
25+
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
26+
import java.io.IOException;
27+
28+
public class ListTemplatesWithFilter {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
33+
String projectId = "your-project-id";
34+
String locationId = "your-location-id";
35+
// Filter to applied.
36+
// Example: "name=\"projects/your-project-id/locations/us-central1/your-template-id\""
37+
String filter = "your-filter-condition";
38+
39+
listTemplatesWithFilter(projectId, locationId, filter);
40+
}
41+
42+
public static ListTemplatesPagedResponse listTemplatesWithFilter(String projectId,
43+
String locationId, String filter) throws IOException {
44+
// Construct the API endpoint URL.
45+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
46+
47+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
48+
.build();
49+
50+
// Initialize the client that will be used to send requests. This client
51+
// only needs to be created once, and can be reused for multiple requests.
52+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
53+
// Build the parent name.
54+
String parent = LocationName.of(projectId, locationId).toString();
55+
56+
ListTemplatesRequest request = ListTemplatesRequest.newBuilder()
57+
.setParent(parent)
58+
.setFilter(filter)
59+
.build();
60+
61+
// List all templates.
62+
ListTemplatesPagedResponse pagedResponse = client.listTemplates(request);
63+
pagedResponse.iterateAll().forEach(template -> {
64+
System.out.printf("Template %s\n", template.getName());
65+
});
66+
67+
return pagedResponse;
68+
}
69+
}
70+
}
71+
72+
// [END modelarmor_list_templates_with_filter]

modelarmor/src/test/java/modelarmor/SnippetsIT.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertTrue;
2223

2324
import com.google.api.gax.rpc.NotFoundException;
2425
import com.google.cloud.dlp.v2.DlpServiceClient;
@@ -134,6 +135,45 @@ private static String randomId() {
134135
}
135136

136137
@Test
138+
public void testGetModelArmorTemplate() throws IOException {
139+
CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID);
140+
Template retrievedTemplate = GetTemplate.getTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID);
141+
142+
assertEquals(retrievedTemplate.getName(), TEST_TEMPLATE_NAME);
143+
}
144+
145+
@Test
146+
public void testListModelArmorTemplates() throws IOException {
147+
CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID);
148+
149+
ListTemplates.listTemplates(PROJECT_ID, LOCATION_ID);
150+
151+
boolean templatePresentInList = false;
152+
for (Template template : ListTemplates.listTemplates(PROJECT_ID, LOCATION_ID).iterateAll()) {
153+
if (TEST_TEMPLATE_NAME.equals(template.getName())) {
154+
templatePresentInList = true;
155+
}
156+
}
157+
assertTrue(templatePresentInList);
158+
}
159+
160+
@Test
161+
public void testListTemplatesWithFilter() throws IOException {
162+
CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID);
163+
String filter = "name=\"projects/" + PROJECT_ID + "/locations/" + LOCATION_ID + "/"
164+
+ TEST_TEMPLATE_ID + "\"";
165+
166+
ListTemplatesWithFilter.listTemplatesWithFilter(PROJECT_ID, LOCATION_ID, filter);
167+
168+
boolean templatePresentInList = false;
169+
for (Template template : ListTemplates.listTemplates(PROJECT_ID, LOCATION_ID).iterateAll()) {
170+
if (TEST_TEMPLATE_NAME.equals(template.getName())) {
171+
templatePresentInList = true;
172+
}
173+
}
174+
assertTrue(templatePresentInList);
175+
}
176+
137177
public void testCreateModelArmorTemplate() throws IOException {
138178
Template createdTemplate = CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID,
139179
TEST_TEMPLATE_ID);

0 commit comments

Comments
 (0)