-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(modelarmor): Added floor settings snippets #10066
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
Open
tirthrajsinh-zala-crest
wants to merge
9
commits into
GoogleCloudPlatform:main
Choose a base branch
from
tirthrajsinh-zala-crest:modelarmor-floorsettings-code-snippets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6629006
feat(modelarmor): Added modelarmor floorsettings snippets
tirthrajsinh-zala-crest 0ba4c57
address-review-comments
harshnasitcrest f1517b1
fix-lint
harshnasitcrest a2a453a
address-review-comment
harshnasitcrest 7bf92e3
Merge branch 'main' into modelarmor-floorsettings-code-snippets
harshnasitcrest b2445ab
fix-tests-post-merge-conflicts
harshnasitcrest 209dedf
fix-tests
harshnasitcrest 305a083
make-update-template-metadata-consistent-with-python-snippet
harshnasitcrest a4216dd
update-metadata-in-create-snippets
harshnasitcrest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
modelarmor/src/main/java/modelarmor/GetFolderFloorSetting.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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; | ||
|
||
// [START modelarmor_get_folder_floor_settings] | ||
|
||
import com.google.cloud.modelarmor.v1.FloorSetting; | ||
import com.google.cloud.modelarmor.v1.FloorSettingName; | ||
import com.google.cloud.modelarmor.v1.GetFloorSettingRequest; | ||
import com.google.cloud.modelarmor.v1.ModelArmorClient; | ||
import java.io.IOException; | ||
|
||
public class GetFolderFloorSetting { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String folderId = "your-folder-id"; | ||
|
||
getFolderFloorSetting(folderId); | ||
} | ||
|
||
public static FloorSetting getFolderFloorSetting(String folderId) throws IOException { | ||
|
||
// Initialize client that will be used to send requests. This client only | ||
// needs to be created once, and can be reused for multiple requests. | ||
try (ModelArmorClient client = ModelArmorClient.create()) { | ||
String name = FloorSettingName.of(folderId, "global").toString(); | ||
|
||
GetFloorSettingRequest request = GetFloorSettingRequest.newBuilder().setName(name).build(); | ||
|
||
FloorSetting floorSetting = client.getFloorSetting(request); | ||
System.out.println("Fetched floor setting for folder: " + folderId); | ||
|
||
return floorSetting; | ||
} | ||
} | ||
} | ||
// [END modelarmor_get_folder_floor_settings] |
53 changes: 53 additions & 0 deletions
53
modelarmor/src/main/java/modelarmor/GetOrganizationFloorSetting.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
// [START modelarmor_get_organization_floor_settings] | ||
|
||
import com.google.cloud.modelarmor.v1.FloorSetting; | ||
import com.google.cloud.modelarmor.v1.FloorSettingName; | ||
import com.google.cloud.modelarmor.v1.GetFloorSettingRequest; | ||
import com.google.cloud.modelarmor.v1.ModelArmorClient; | ||
import java.io.IOException; | ||
|
||
public class GetOrganizationFloorSetting { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String organizationId = "your-organization-id"; | ||
|
||
getOrganizationFloorSetting(organizationId); | ||
} | ||
|
||
public static FloorSetting getOrganizationFloorSetting(String organizationId) throws IOException { | ||
|
||
// Initialize client that will be used to send requests. This client only | ||
// needs to be created once, and can be reused for multiple requests. | ||
try (ModelArmorClient client = ModelArmorClient.create()) { | ||
String name = FloorSettingName.ofOrganizationLocationName(organizationId, "global") | ||
.toString(); | ||
|
||
GetFloorSettingRequest request = GetFloorSettingRequest.newBuilder().setName(name).build(); | ||
|
||
FloorSetting floorSetting = client.getFloorSetting(request); | ||
System.out.println("Fetched floor setting for organization: " + organizationId); | ||
|
||
return floorSetting; | ||
} | ||
} | ||
} | ||
// [END modelarmor_get_organization_floor_settings] |
52 changes: 52 additions & 0 deletions
52
modelarmor/src/main/java/modelarmor/GetProjectFloorSetting.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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; | ||
|
||
// [START modelarmor_get_project_floor_settings] | ||
|
||
import com.google.cloud.modelarmor.v1.FloorSetting; | ||
import com.google.cloud.modelarmor.v1.FloorSettingName; | ||
import com.google.cloud.modelarmor.v1.GetFloorSettingRequest; | ||
import com.google.cloud.modelarmor.v1.ModelArmorClient; | ||
import java.io.IOException; | ||
|
||
public class GetProjectFloorSetting { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "your-project-id"; | ||
|
||
getProjectFloorSetting(projectId); | ||
} | ||
|
||
public static FloorSetting getProjectFloorSetting(String projectId) throws IOException { | ||
|
||
// Initialize client that will be used to send requests. This client only | ||
// needs to be created once, and can be reused for multiple requests. | ||
try (ModelArmorClient client = ModelArmorClient.create()) { | ||
String name = FloorSettingName.of(projectId, "global").toString(); | ||
|
||
GetFloorSettingRequest request = GetFloorSettingRequest.newBuilder().setName(name).build(); | ||
|
||
FloorSetting floorSetting = client.getFloorSetting(request); | ||
System.out.println("Fetched floor setting for project: " + projectId); | ||
|
||
return floorSetting; | ||
} | ||
} | ||
} | ||
// [END modelarmor_get_project_floor_settings] |
101 changes: 101 additions & 0 deletions
101
modelarmor/src/main/java/modelarmor/UpdateFolderFloorSetting.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* 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; | ||
|
||
// [START modelarmor_update_folder_floor_settings] | ||
|
||
import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; | ||
import com.google.cloud.modelarmor.v1.FilterConfig; | ||
import com.google.cloud.modelarmor.v1.FloorSetting; | ||
import com.google.cloud.modelarmor.v1.FloorSettingName; | ||
import com.google.cloud.modelarmor.v1.ModelArmorClient; | ||
import com.google.cloud.modelarmor.v1.RaiFilterSettings; | ||
import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter; | ||
import com.google.cloud.modelarmor.v1.RaiFilterType; | ||
import com.google.cloud.modelarmor.v1.UpdateFloorSettingRequest; | ||
import com.google.protobuf.FieldMask; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class UpdateFolderFloorSetting { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String folderId = "your-folder-id"; | ||
|
||
updateFolderFloorSetting(folderId); | ||
} | ||
|
||
public static FloorSetting updateFolderFloorSetting(String folderId) | ||
throws IOException { | ||
|
||
// Initialize client that will be used to send requests. This client only | ||
// needs to be created once, and can be reused for multiple requests. | ||
try (ModelArmorClient client = ModelArmorClient.create()) { | ||
String name = FloorSettingName.ofFolderLocationName(folderId, "global").toString(); | ||
|
||
// For more details on filters, please refer to the following doc: | ||
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters | ||
RaiFilterSettings raiFilterSettings = | ||
RaiFilterSettings.newBuilder() | ||
.addAllRaiFilters( | ||
List.of( | ||
RaiFilter.newBuilder() | ||
.setFilterType(RaiFilterType.DANGEROUS) | ||
.setConfidenceLevel(DetectionConfidenceLevel.HIGH) | ||
.build(), | ||
RaiFilter.newBuilder() | ||
.setFilterType(RaiFilterType.HATE_SPEECH) | ||
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) | ||
.build(), | ||
RaiFilter.newBuilder() | ||
.setFilterType(RaiFilterType.HARASSMENT) | ||
.setConfidenceLevel(DetectionConfidenceLevel.HIGH) | ||
.build(), | ||
RaiFilter.newBuilder() | ||
.setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) | ||
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) | ||
.build())) | ||
.build(); | ||
|
||
FilterConfig modelArmorFilter = FilterConfig.newBuilder() | ||
.setRaiSettings(raiFilterSettings) | ||
.build(); | ||
|
||
// Create a field mask to specify which fields to update. | ||
// Ref: https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask | ||
FieldMask updateMask = FieldMask.newBuilder().addPaths("filter_config.rai_settings").build(); | ||
|
||
FloorSetting floorSetting = FloorSetting.newBuilder() | ||
.setName(name) | ||
.setFilterConfig(modelArmorFilter) | ||
.setEnableFloorSettingEnforcement(true) | ||
.build(); | ||
|
||
UpdateFloorSettingRequest request = UpdateFloorSettingRequest.newBuilder() | ||
.setFloorSetting(floorSetting) | ||
.setUpdateMask(updateMask) | ||
.build(); | ||
|
||
FloorSetting updatedFloorSetting = client.updateFloorSetting(request); | ||
System.out.println("Updated floor setting for folder: " + folderId); | ||
|
||
return updatedFloorSetting; | ||
} | ||
} | ||
} | ||
// [END modelarmor_update_folder_floor_settings] |
104 changes: 104 additions & 0 deletions
104
modelarmor/src/main/java/modelarmor/UpdateOrganizationsFloorSetting.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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; | ||
|
||
// [START modelarmor_update_organization_floor_settings] | ||
|
||
import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; | ||
import com.google.cloud.modelarmor.v1.FilterConfig; | ||
import com.google.cloud.modelarmor.v1.FloorSetting; | ||
import com.google.cloud.modelarmor.v1.FloorSettingName; | ||
import com.google.cloud.modelarmor.v1.ModelArmorClient; | ||
import com.google.cloud.modelarmor.v1.RaiFilterSettings; | ||
import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter; | ||
import com.google.cloud.modelarmor.v1.RaiFilterType; | ||
import com.google.cloud.modelarmor.v1.UpdateFloorSettingRequest; | ||
import com.google.protobuf.FieldMask; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class UpdateOrganizationsFloorSetting { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String organizationId = "your-organization-id"; | ||
|
||
updateOrganizationFloorSetting(organizationId); | ||
} | ||
|
||
public static FloorSetting updateOrganizationFloorSetting(String organizationId) | ||
throws IOException { | ||
|
||
// Initialize client that will be used to send requests. This client only | ||
// needs to be created once, and can be reused for multiple requests. | ||
try (ModelArmorClient client = ModelArmorClient.create()) { | ||
String name = FloorSettingName.ofOrganizationLocationName(organizationId, "global") | ||
.toString(); | ||
|
||
// For more details on filters, please refer to the following doc: | ||
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters | ||
RaiFilterSettings raiFilterSettings = | ||
RaiFilterSettings.newBuilder() | ||
.addAllRaiFilters( | ||
List.of( | ||
RaiFilter.newBuilder() | ||
.setFilterType(RaiFilterType.DANGEROUS) | ||
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) | ||
.build(), | ||
RaiFilter.newBuilder() | ||
.setFilterType(RaiFilterType.HATE_SPEECH) | ||
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) | ||
.build(), | ||
RaiFilter.newBuilder() | ||
.setFilterType(RaiFilterType.HARASSMENT) | ||
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) | ||
.build(), | ||
RaiFilter.newBuilder() | ||
.setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) | ||
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) | ||
.build())) | ||
.build(); | ||
|
||
FilterConfig modelArmorFilter = FilterConfig.newBuilder() | ||
.setRaiSettings(raiFilterSettings) | ||
.build(); | ||
|
||
// Create a field mask to specify which fields to update. | ||
// Ref: https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask | ||
FieldMask updateMask = FieldMask.newBuilder() | ||
.addPaths("filter_config.rai_settings") | ||
.build(); | ||
|
||
FloorSetting floorSetting = FloorSetting.newBuilder() | ||
.setName(name) | ||
.setFilterConfig(modelArmorFilter) | ||
.setEnableFloorSettingEnforcement(true) | ||
.build(); | ||
|
||
UpdateFloorSettingRequest request = UpdateFloorSettingRequest.newBuilder() | ||
.setFloorSetting(floorSetting) | ||
.setUpdateMask(updateMask) | ||
.build(); | ||
|
||
FloorSetting updatedFloorSetting = client.updateFloorSetting(request); | ||
System.out.println("Updated floor setting for organization: " + organizationId); | ||
|
||
return updatedFloorSetting; | ||
} | ||
} | ||
} | ||
// [END modelarmor_update_organization_floor_settings] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.