Skip to content

Commit 2e68e03

Browse files
feat(modelarmor): Added floor settings snippets (#10066)
* feat(modelarmor): Added modelarmor floorsettings snippets * address-review-comments * fix-lint * address-review-comment * fix-tests-post-merge-conflicts * fix-tests * make-update-template-metadata-consistent-with-python-snippet * update-metadata-in-create-snippets * update-libraries-bom-version * fix-tests * fix-lints * Remove folder and org floor settings snips * Update floor setting snips with empty filters (Empty RAI block is required) * Remove all floor setting tests
1 parent 21982fc commit 2e68e03

10 files changed

+608
-170
lines changed

modelarmor/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<dependency>
4444
<groupId>com.google.cloud</groupId>
4545
<artifactId>libraries-bom</artifactId>
46-
<version>26.59.0</version>
46+
<version>26.60.0</version>
4747
<type>pom</type>
4848
<scope>import</scope>
4949
</dependency>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ public static Template createTemplateWithMetadata(
9595
// For more details about metadata, refer to the following documentation:
9696
// https://cloud.google.com/security-command-center/docs/reference/model-armor/rest/v1/projects.locations.templates#templatemetadata
9797
TemplateMetadata templateMetadata = TemplateMetadata.newBuilder()
98-
.setIgnorePartialInvocationFailures(true)
98+
.setLogTemplateOperations(true)
9999
.setLogSanitizeOperations(true)
100-
.setCustomPromptSafetyErrorCode(500)
101100
.build();
102101

103102
Template template = Template.newBuilder()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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_folder_floor_settings]
20+
21+
import com.google.cloud.modelarmor.v1.FloorSetting;
22+
import com.google.cloud.modelarmor.v1.FloorSettingName;
23+
import com.google.cloud.modelarmor.v1.GetFloorSettingRequest;
24+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
25+
import java.io.IOException;
26+
27+
public class GetFolderFloorSetting {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String folderId = "your-folder-id";
32+
33+
getFolderFloorSetting(folderId);
34+
}
35+
36+
public static FloorSetting getFolderFloorSetting(String folderId) throws IOException {
37+
38+
// Initialize client that will be used to send requests. This client only
39+
// needs to be created once, and can be reused for multiple requests.
40+
try (ModelArmorClient client = ModelArmorClient.create()) {
41+
String name = FloorSettingName.of(folderId, "global").toString();
42+
43+
GetFloorSettingRequest request = GetFloorSettingRequest.newBuilder().setName(name).build();
44+
45+
FloorSetting floorSetting = client.getFloorSetting(request);
46+
System.out.println("Fetched floor setting for folder: " + folderId);
47+
48+
return floorSetting;
49+
}
50+
}
51+
}
52+
// [END modelarmor_get_folder_floor_settings]
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+
// [START modelarmor_get_organization_floor_settings]
20+
21+
import com.google.cloud.modelarmor.v1.FloorSetting;
22+
import com.google.cloud.modelarmor.v1.FloorSettingName;
23+
import com.google.cloud.modelarmor.v1.GetFloorSettingRequest;
24+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
25+
import java.io.IOException;
26+
27+
public class GetOrganizationFloorSetting {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String organizationId = "your-organization-id";
32+
33+
getOrganizationFloorSetting(organizationId);
34+
}
35+
36+
public static FloorSetting getOrganizationFloorSetting(String organizationId) throws IOException {
37+
38+
// Initialize client that will be used to send requests. This client only
39+
// needs to be created once, and can be reused for multiple requests.
40+
try (ModelArmorClient client = ModelArmorClient.create()) {
41+
String name = FloorSettingName.ofOrganizationLocationName(organizationId, "global")
42+
.toString();
43+
44+
GetFloorSettingRequest request = GetFloorSettingRequest.newBuilder().setName(name).build();
45+
46+
FloorSetting floorSetting = client.getFloorSetting(request);
47+
System.out.println("Fetched floor setting for organization: " + organizationId);
48+
49+
return floorSetting;
50+
}
51+
}
52+
}
53+
// [END modelarmor_get_organization_floor_settings]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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_project_floor_settings]
20+
21+
import com.google.cloud.modelarmor.v1.FloorSetting;
22+
import com.google.cloud.modelarmor.v1.FloorSettingName;
23+
import com.google.cloud.modelarmor.v1.GetFloorSettingRequest;
24+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
25+
import java.io.IOException;
26+
27+
public class GetProjectFloorSetting {
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+
33+
getProjectFloorSetting(projectId);
34+
}
35+
36+
public static FloorSetting getProjectFloorSetting(String projectId) throws IOException {
37+
38+
// Initialize client that will be used to send requests. This client only
39+
// needs to be created once, and can be reused for multiple requests.
40+
try (ModelArmorClient client = ModelArmorClient.create()) {
41+
String name = FloorSettingName.of(projectId, "global").toString();
42+
43+
GetFloorSettingRequest request = GetFloorSettingRequest.newBuilder().setName(name).build();
44+
45+
FloorSetting floorSetting = client.getFloorSetting(request);
46+
System.out.println("Fetched floor setting for project: " + projectId);
47+
48+
return floorSetting;
49+
}
50+
}
51+
}
52+
// [END modelarmor_get_project_floor_settings]
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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_folder_floor_settings]
20+
21+
import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
22+
import com.google.cloud.modelarmor.v1.FilterConfig;
23+
import com.google.cloud.modelarmor.v1.FloorSetting;
24+
import com.google.cloud.modelarmor.v1.FloorSettingName;
25+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
26+
import com.google.cloud.modelarmor.v1.RaiFilterSettings;
27+
import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter;
28+
import com.google.cloud.modelarmor.v1.RaiFilterType;
29+
import com.google.cloud.modelarmor.v1.UpdateFloorSettingRequest;
30+
import com.google.protobuf.FieldMask;
31+
import java.io.IOException;
32+
import java.util.List;
33+
34+
public class UpdateFolderFloorSetting {
35+
36+
public static void main(String[] args) throws IOException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String folderId = "your-folder-id";
39+
40+
updateFolderFloorSetting(folderId);
41+
}
42+
43+
public static FloorSetting updateFolderFloorSetting(String folderId)
44+
throws IOException {
45+
46+
// Initialize client that will be used to send requests. This client only
47+
// needs to be created once, and can be reused for multiple requests.
48+
try (ModelArmorClient client = ModelArmorClient.create()) {
49+
String name = FloorSettingName.ofFolderLocationName(folderId, "global").toString();
50+
51+
// For more details on filters, please refer to the following doc:
52+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
53+
RaiFilterSettings raiFilterSettings =
54+
RaiFilterSettings.newBuilder()
55+
.addAllRaiFilters(
56+
List.of(
57+
RaiFilter.newBuilder()
58+
.setFilterType(RaiFilterType.HARASSMENT)
59+
.setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE)
60+
.build(),
61+
RaiFilter.newBuilder()
62+
.setFilterType(RaiFilterType.SEXUALLY_EXPLICIT)
63+
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
64+
.build()))
65+
.build();
66+
67+
FilterConfig modelArmorFilter = FilterConfig.newBuilder()
68+
.setRaiSettings(raiFilterSettings)
69+
.build();
70+
71+
// Create a field mask to specify which fields to update.
72+
// Ref: https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask
73+
FieldMask updateMask = FieldMask.newBuilder().addPaths("filter_config.rai_settings").build();
74+
75+
FloorSetting floorSetting = FloorSetting.newBuilder()
76+
.setName(name)
77+
.setFilterConfig(modelArmorFilter)
78+
.setEnableFloorSettingEnforcement(true)
79+
.build();
80+
81+
UpdateFloorSettingRequest request = UpdateFloorSettingRequest.newBuilder()
82+
.setFloorSetting(floorSetting)
83+
.setUpdateMask(updateMask)
84+
.build();
85+
86+
FloorSetting updatedFloorSetting = client.updateFloorSetting(request);
87+
System.out.println("Updated floor setting for folder: " + folderId);
88+
89+
return updatedFloorSetting;
90+
}
91+
}
92+
}
93+
// [END modelarmor_update_folder_floor_settings]
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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_organization_floor_settings]
20+
21+
import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
22+
import com.google.cloud.modelarmor.v1.FilterConfig;
23+
import com.google.cloud.modelarmor.v1.FloorSetting;
24+
import com.google.cloud.modelarmor.v1.FloorSettingName;
25+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
26+
import com.google.cloud.modelarmor.v1.RaiFilterSettings;
27+
import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter;
28+
import com.google.cloud.modelarmor.v1.RaiFilterType;
29+
import com.google.cloud.modelarmor.v1.UpdateFloorSettingRequest;
30+
import com.google.protobuf.FieldMask;
31+
import java.io.IOException;
32+
import java.util.List;
33+
34+
public class UpdateOrganizationsFloorSetting {
35+
36+
public static void main(String[] args) throws IOException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String organizationId = "your-organization-id";
39+
40+
updateOrganizationFloorSetting(organizationId);
41+
}
42+
43+
public static FloorSetting updateOrganizationFloorSetting(String organizationId)
44+
throws IOException {
45+
46+
// Initialize client that will be used to send requests. This client only
47+
// needs to be created once, and can be reused for multiple requests.
48+
try (ModelArmorClient client = ModelArmorClient.create()) {
49+
String name = FloorSettingName.ofOrganizationLocationName(organizationId, "global")
50+
.toString();
51+
52+
// For more details on filters, please refer to the following doc:
53+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
54+
RaiFilterSettings raiFilterSettings =
55+
RaiFilterSettings.newBuilder()
56+
.addAllRaiFilters(
57+
List.of(
58+
RaiFilter.newBuilder()
59+
.setFilterType(RaiFilterType.HARASSMENT)
60+
.setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE)
61+
.build(),
62+
RaiFilter.newBuilder()
63+
.setFilterType(RaiFilterType.SEXUALLY_EXPLICIT)
64+
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
65+
.build()))
66+
.build();
67+
68+
FilterConfig modelArmorFilter = FilterConfig.newBuilder()
69+
.setRaiSettings(raiFilterSettings)
70+
.build();
71+
72+
// Create a field mask to specify which fields to update.
73+
// Ref: https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask
74+
FieldMask updateMask = FieldMask.newBuilder()
75+
.addPaths("filter_config.rai_settings")
76+
.build();
77+
78+
FloorSetting floorSetting = FloorSetting.newBuilder()
79+
.setName(name)
80+
.setFilterConfig(modelArmorFilter)
81+
.setEnableFloorSettingEnforcement(true)
82+
.build();
83+
84+
UpdateFloorSettingRequest request = UpdateFloorSettingRequest.newBuilder()
85+
.setFloorSetting(floorSetting)
86+
.setUpdateMask(updateMask)
87+
.build();
88+
89+
FloorSetting updatedFloorSetting = client.updateFloorSetting(request);
90+
System.out.println("Updated floor setting for organization: " + organizationId);
91+
92+
return updatedFloorSetting;
93+
}
94+
}
95+
}
96+
// [END modelarmor_update_organization_floor_settings]

0 commit comments

Comments
 (0)