Skip to content

Commit b0eca89

Browse files
feat(modelarmor): Added code samples to update model armor templates (#10069)
* Added code samples to update model armor templates * address-review-comments * sync-requireenvvar-function * remove-require-env-location-condition * address-review-comment * fix-lint-post-merge-conflicts --------- Co-authored-by: Harsh Nasit <[email protected]> Co-authored-by: harshnasitcrest <[email protected]>
1 parent aa7bbc9 commit b0eca89

File tree

8 files changed

+345
-7
lines changed

8 files changed

+345
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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";

modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java

Lines changed: 1 addition & 1 deletion
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

modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java

Lines changed: 1 addition & 1 deletion
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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public static void main(String[] args) throws IOException {
4040

4141
public static void deleteTemplate(String projectId, String locationId, String templateId)
4242
throws IOException {
43+
4344
// Construct the API endpoint URL.
4445
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
4546
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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_update_template]
20+
21+
import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
22+
import com.google.cloud.modelarmor.v1.FilterConfig;
23+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
24+
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
25+
import com.google.cloud.modelarmor.v1.RaiFilterSettings;
26+
import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter;
27+
import com.google.cloud.modelarmor.v1.RaiFilterType;
28+
import com.google.cloud.modelarmor.v1.Template;
29+
import com.google.cloud.modelarmor.v1.TemplateName;
30+
import com.google.cloud.modelarmor.v1.UpdateTemplateRequest;
31+
import com.google.protobuf.FieldMask;
32+
import java.io.IOException;
33+
import java.util.List;
34+
35+
public class UpdateTemplate {
36+
37+
public static void main(String[] args) throws IOException {
38+
// TODO(developer): Replace these variables before running the sample.
39+
40+
// Specify the Google Project ID.
41+
String projectId = "your-project-id";
42+
// Specify the location ID. For example, us-central1.
43+
String locationId = "your-location-id";
44+
// Specify the template ID.
45+
String templateId = "your-template-id";
46+
47+
updateTemplate(projectId, locationId, templateId);
48+
}
49+
50+
public static Template updateTemplate(String projectId, String locationId, String templateId)
51+
throws IOException {
52+
// Construct the API endpoint URL.
53+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
54+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
55+
.build();
56+
57+
// Initialize the client that will be used to send requests. This client
58+
// only needs to be created once, and can be reused for multiple requests.
59+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
60+
// Get the template name.
61+
String name = TemplateName.of(projectId, locationId, templateId).toString();
62+
63+
// Build the updated Model Armor template with modified filters.
64+
// For more details on filters, please refer to the following doc:
65+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
66+
RaiFilterSettings raiFilterSettings =
67+
RaiFilterSettings.newBuilder()
68+
.addAllRaiFilters(
69+
List.of(
70+
RaiFilter.newBuilder()
71+
.setFilterType(RaiFilterType.DANGEROUS)
72+
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
73+
.build(),
74+
RaiFilter.newBuilder()
75+
.setFilterType(RaiFilterType.HATE_SPEECH)
76+
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
77+
.build(),
78+
RaiFilter.newBuilder()
79+
.setFilterType(RaiFilterType.HARASSMENT)
80+
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
81+
.build(),
82+
RaiFilter.newBuilder()
83+
.setFilterType(RaiFilterType.SEXUALLY_EXPLICIT)
84+
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
85+
.build()))
86+
.build();
87+
88+
FilterConfig modelArmorFilter = FilterConfig.newBuilder()
89+
.setRaiSettings(raiFilterSettings)
90+
.build();
91+
92+
Template template = Template.newBuilder()
93+
.setName(name)
94+
.setFilterConfig(modelArmorFilter)
95+
.build();
96+
97+
// Create a field mask to specify which fields to update.
98+
// Ref: https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask
99+
FieldMask updateMask = FieldMask.newBuilder()
100+
.addPaths("filter_config.rai_settings")
101+
.build();
102+
103+
UpdateTemplateRequest request = UpdateTemplateRequest.newBuilder()
104+
.setTemplate(template)
105+
.setUpdateMask(updateMask)
106+
.build();
107+
108+
Template updatedTemplate = client.updateTemplate(request);
109+
System.out.println("Updated template: " + updatedTemplate.getName());
110+
111+
return updatedTemplate;
112+
}
113+
}
114+
}
115+
116+
// [END modelarmor_update_template]
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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_update_template_labels]
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 com.google.cloud.modelarmor.v1.UpdateTemplateRequest;
26+
import com.google.protobuf.FieldMask;
27+
import java.io.IOException;
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
31+
public class UpdateTemplateWithLabels {
32+
33+
public static void main(String[] args) throws IOException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
36+
// Specify the Google Project ID.
37+
String projectId = "your-project-id";
38+
// Specify the location ID. For example, us-central1.
39+
String locationId = "your-location-id";
40+
// Specify the template ID.
41+
String templateId = "your-template-id";
42+
43+
updateTemplateWithLabels(projectId, locationId, templateId);
44+
}
45+
46+
public static Template updateTemplateWithLabels(String projectId, String locationId,
47+
String templateId) throws IOException {
48+
// Construct the API endpoint URL.
49+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
50+
51+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
52+
.build();
53+
54+
// Initialize the client that will be used to send requests. This client
55+
// only needs to be created once, and can be reused for multiple requests.
56+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
57+
// Get the template name.
58+
String name = TemplateName.of(projectId, locationId, templateId).toString();
59+
60+
// Create a new labels map.
61+
Map<String, String> labels = new HashMap<>();
62+
63+
// Add or update labels.
64+
labels.put("key1", "value2");
65+
labels.put("key2", "value3");
66+
67+
// Update the template with the new labels.
68+
Template template = Template.newBuilder()
69+
.setName(name)
70+
.putAllLabels(labels)
71+
.build();
72+
73+
// Create a field mask to specify that only labels should be updated.
74+
FieldMask updateMask = FieldMask.newBuilder()
75+
.addPaths("labels")
76+
.build();
77+
78+
UpdateTemplateRequest request =
79+
UpdateTemplateRequest.newBuilder()
80+
.setTemplate(template)
81+
.setUpdateMask(updateMask)
82+
.build();
83+
84+
Template updatedTemplate = client.updateTemplate(request);
85+
System.out.println("Updated labels of template: " + updatedTemplate.getName());
86+
87+
return updatedTemplate;
88+
}
89+
}
90+
}
91+
92+
// [END modelarmor_update_template_labels]
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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_update_template_metadata]
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.Template.TemplateMetadata;
25+
import com.google.cloud.modelarmor.v1.TemplateName;
26+
import com.google.cloud.modelarmor.v1.UpdateTemplateRequest;
27+
import com.google.protobuf.FieldMask;
28+
import java.io.IOException;
29+
30+
public class UpdateTemplateWithMetadata {
31+
32+
public static void main(String[] args) throws IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
35+
// Specify the Google Project ID.
36+
String projectId = "your-project-id";
37+
// Specify the location ID. For example, us-central1.
38+
String locationId = "your-location-id";
39+
// Specify the template ID.
40+
String templateId = "your-template-id";
41+
42+
updateTemplateWithMetadata(projectId, locationId, templateId);
43+
}
44+
45+
public static Template updateTemplateWithMetadata(String projectId, String locationId,
46+
String templateId) throws IOException {
47+
// Construct the API endpoint URL.
48+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
49+
50+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
51+
.build();
52+
53+
// Initialize the client that will be used to send requests. This client
54+
// only needs to be created once, and can be reused for multiple requests.
55+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
56+
// Get the template name.
57+
String name = TemplateName.of(projectId, locationId, templateId).toString();
58+
59+
// For more details about metadata, refer to the following documentation:
60+
// https://cloud.google.com/security-command-center/docs/reference/model-armor/rest/v1/projects.locations.templates#templatemetadata
61+
TemplateMetadata updatedMetadata = TemplateMetadata.newBuilder()
62+
.setIgnorePartialInvocationFailures(false)
63+
.setLogSanitizeOperations(false)
64+
.setCustomPromptSafetyErrorCode(400)
65+
.build();
66+
67+
// Update the template with new metadata.
68+
Template template = Template.newBuilder()
69+
.setName(name)
70+
.setTemplateMetadata(updatedMetadata)
71+
.build();
72+
73+
// Create a field mask to specify which metadata fields should be updated.
74+
FieldMask updateMask = FieldMask.newBuilder()
75+
.addPaths("template_metadata")
76+
.build();
77+
78+
UpdateTemplateRequest request =
79+
UpdateTemplateRequest.newBuilder()
80+
.setTemplate(template)
81+
.setUpdateMask(updateMask)
82+
.build();
83+
84+
Template updatedTemplate = client.updateTemplate(request);
85+
System.out.println("Updated metadata of template: " + updatedTemplate.getName());
86+
87+
return updatedTemplate;
88+
}
89+
}
90+
}
91+
92+
// [END modelarmor_update_template_metadata]

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,43 @@ private static String randomId() {
134134
return "java-ma-" + random.nextLong();
135135
}
136136

137+
@Test
138+
public void testUpdateModelArmorTemplate() throws IOException {
139+
CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID);
140+
141+
// Update the existing template.
142+
Template updatedTemplate = UpdateTemplate.updateTemplate(PROJECT_ID, LOCATION_ID,
143+
TEST_TEMPLATE_ID);
144+
145+
assertEquals(updatedTemplate.getName(), TEST_TEMPLATE_NAME);
146+
}
147+
148+
@Test
149+
public void testUpdateModelArmorTemplateWithLabels() throws IOException {
150+
CreateTemplateWithLabels.createTemplateWithLabels(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID);
151+
152+
// Update the existing template.
153+
Template updatedTemplate = UpdateTemplateWithLabels.updateTemplateWithLabels(PROJECT_ID,
154+
LOCATION_ID, TEST_TEMPLATE_ID);
155+
156+
assertEquals(updatedTemplate.getName(), TEST_TEMPLATE_NAME);
157+
}
158+
159+
@Test
160+
public void testUpdateModelArmorTemplateWithMetadata() throws IOException {
161+
CreateTemplateWithMetadata.createTemplateWithMetadata(PROJECT_ID, LOCATION_ID,
162+
TEST_TEMPLATE_ID);
163+
164+
// Update the existing template.
165+
Template updatedTemplate = UpdateTemplateWithMetadata.updateTemplateWithMetadata(PROJECT_ID,
166+
LOCATION_ID, TEST_TEMPLATE_ID);
167+
168+
assertEquals(updatedTemplate.getName(), TEST_TEMPLATE_NAME);
169+
assertEquals(false, updatedTemplate.getTemplateMetadata().getIgnorePartialInvocationFailures());
170+
assertEquals(false, updatedTemplate.getTemplateMetadata().getLogSanitizeOperations());
171+
assertEquals(400, updatedTemplate.getTemplateMetadata().getCustomPromptSafetyErrorCode());
172+
}
173+
137174
@Test
138175
public void testGetModelArmorTemplate() throws IOException {
139176
CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID);
@@ -294,10 +331,10 @@ private static DeidentifyTemplate createDeidentifyTemplate(String templateId) th
294331

295332
CreateDeidentifyTemplateRequest createDeidentifyTemplateRequest =
296333
CreateDeidentifyTemplateRequest.newBuilder()
297-
.setParent(LocationName.of(PROJECT_ID, LOCATION_ID).toString())
298-
.setTemplateId(templateId)
299-
.setDeidentifyTemplate(deidentifyTemplate)
300-
.build();
334+
.setParent(LocationName.of(PROJECT_ID, LOCATION_ID).toString())
335+
.setTemplateId(templateId)
336+
.setDeidentifyTemplate(deidentifyTemplate)
337+
.build();
301338

302339
return dlpServiceClient.createDeidentifyTemplate(createDeidentifyTemplateRequest);
303340
}

0 commit comments

Comments
 (0)