-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(modelarmor): Added code samples for delete, get and list model armor templates #10070
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
Merged
telpirion
merged 13 commits into
GoogleCloudPlatform:main
from
tirthrajsinh-zala-crest:modelarmor-list-delete-get-template-samples
May 8, 2025
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d40e2b2
Added code samples for delete, get and list model armor templates
rudrakhsha-crest 4bc2d54
Added create template code snippet
rudrakhsha-crest 22f1b67
feat(modelarmor): refactor code
tirthrajsinh-zala-crest 7dff76b
feat(modelarmor): refactor code
tirthrajsinh-zala-crest d851777
address-review-comments
harshnasitcrest a06a1ce
update-exception
harshnasitcrest 64b62bf
remove-unused-annotation
harshnasitcrest 135fa73
sync-requireenvvar-function-usage
harshnasitcrest c0766f9
remove-require-env-location-condition
harshnasitcrest b6abdad
address-review-comment
harshnasitcrest 82ec0f7
Merge branch 'main' into modelarmor-list-delete-get-template-samples
harshnasitcrest bc405db
resolve-merge-conflicts
harshnasitcrest 47b255b
resolve-merge-conflicts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<!-- | ||
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. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.example.modelarmor</groupId> | ||
<artifactId>modelarmor-samples</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<!-- | ||
The parent pom defines common style checks and testing strategies for our samples. | ||
Removing or replacing it should not affect the execution of the samples in anyway. | ||
--> | ||
<parent> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>shared-configuration</artifactId> | ||
<version>1.2.0</version> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>libraries-bom</artifactId> | ||
<version>26.59.0</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-modelarmor</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-dlp</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.protobuf</groupId> | ||
<artifactId>protobuf-java-util</artifactId> | ||
</dependency> | ||
|
||
<!-- test dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>1.4.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
110 changes: 110 additions & 0 deletions
110
modelarmor/src/main/java/modelarmor/CreateTemplate.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,110 @@ | ||
/* | ||
* 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_create_template] | ||
|
||
import com.google.cloud.modelarmor.v1.CreateTemplateRequest; | ||
import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; | ||
import com.google.cloud.modelarmor.v1.FilterConfig; | ||
import com.google.cloud.modelarmor.v1.LocationName; | ||
import com.google.cloud.modelarmor.v1.ModelArmorClient; | ||
import com.google.cloud.modelarmor.v1.ModelArmorSettings; | ||
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.Template; | ||
import com.google.protobuf.util.JsonFormat; | ||
import java.util.List; | ||
|
||
/** This class contains a main method that creates a template using the Model Armor API. */ | ||
public class CreateTemplate { | ||
|
||
/** | ||
* Main method that calls the createTemplate method to create a template. | ||
* | ||
* @param args command line arguments (not used) | ||
* @throws Exception if an error occurs during template creation | ||
*/ | ||
public static void main(String[] args) throws Exception { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "your-project-id"; | ||
String locationId = "your-location-id"; | ||
String templateId = "your-template-id"; | ||
|
||
createTemplate(projectId, locationId, templateId); | ||
} | ||
|
||
/** | ||
* Creates a template using the Model Armor API. | ||
* | ||
* @param projectId the ID of the project | ||
* @param locationId the ID of the location | ||
* @param templateId the ID of the template | ||
* @return the created template | ||
* @throws Exception if an error occurs during template creation | ||
*/ | ||
public static Template createTemplate(String projectId, String locationId, String templateId) | ||
throws Exception { | ||
// Construct the API endpoint URL | ||
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); | ||
|
||
// Create a Model Armor settings object with the API endpoint | ||
ModelArmorSettings modelArmorSettings = | ||
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); | ||
|
||
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { | ||
// Construct the parent resource name | ||
String parent = LocationName.of(projectId, locationId).toString(); | ||
|
||
// Create a template object with a filter config | ||
Template template = | ||
Template.newBuilder() | ||
.setFilterConfig( | ||
FilterConfig.newBuilder() | ||
.setRaiSettings( | ||
RaiFilterSettings.newBuilder() | ||
.addAllRaiFilters( | ||
List.of( | ||
RaiFilter.newBuilder() | ||
.setFilterType(RaiFilterType.DANGEROUS) | ||
.setConfidenceLevel(DetectionConfidenceLevel.HIGH) | ||
.build())) | ||
.build()) | ||
.build()) | ||
.build(); | ||
|
||
// Create a create template request object | ||
CreateTemplateRequest request = | ||
CreateTemplateRequest.newBuilder() | ||
.setParent(parent) | ||
.setTemplateId(templateId) | ||
.setTemplate(template) | ||
.build(); | ||
|
||
// Create the template using the Model Armor client | ||
Template createdTemplate = client.createTemplate(request); | ||
|
||
// Print the created template | ||
System.out.println("Created template: " + JsonFormat.printer().print(createdTemplate)); | ||
|
||
return createdTemplate; | ||
} | ||
} | ||
} | ||
|
||
// [END modelarmor_create_template] |
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,73 @@ | ||
/* | ||
* 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_delete_template] | ||
|
||
import com.google.cloud.modelarmor.v1.ModelArmorClient; | ||
import com.google.cloud.modelarmor.v1.ModelArmorSettings; | ||
import com.google.cloud.modelarmor.v1.TemplateName; | ||
|
||
/** This class contains a main method that deletes a template using the Model Armor API. */ | ||
public class DeleteTemplate { | ||
|
||
/** | ||
* Main method that calls the deleteTemplate method to delete a template. | ||
* | ||
* @param args command line arguments (not used) | ||
* @throws Exception if an error occurs during template deletion | ||
*/ | ||
public static void main(String[] args) throws Exception { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "your-project-id"; | ||
String locationId = "your-location-id"; | ||
String templateId = "your-template-id"; | ||
|
||
deleteTemplate(projectId, locationId, templateId); | ||
} | ||
|
||
/** | ||
* Deletes a template using the Model Armor API. | ||
* | ||
* @param projectId the ID of the project | ||
* @param locationId the ID of the location | ||
* @param templateId the ID of the template | ||
* @throws Exception if an error occurs during template deletion | ||
*/ | ||
public static void deleteTemplate(String projectId, String locationId, String templateId) | ||
throws Exception { | ||
// Construct the API endpoint URL | ||
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); | ||
|
||
// Create a Model Armor settings object with the API endpoint | ||
ModelArmorSettings modelArmorSettings = | ||
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); | ||
|
||
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { | ||
// Construct the template name | ||
String name = TemplateName.of(projectId, locationId, templateId).toString(); | ||
|
||
// Delete the template using the Model Armor client | ||
client.deleteTemplate(name); | ||
|
||
// Print a success message | ||
System.out.println("Deleted template: " + name); | ||
} | ||
} | ||
} | ||
|
||
// [END modelarmor_delete_template] |
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,80 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* This class demonstrates how to retrieve a template using the Model Armor API. | ||
* | ||
* @author [Your Name] | ||
*/ | ||
import com.google.cloud.modelarmor.v1.ModelArmorClient; | ||
import com.google.cloud.modelarmor.v1.ModelArmorSettings; | ||
import com.google.cloud.modelarmor.v1.Template; | ||
import com.google.cloud.modelarmor.v1.TemplateName; | ||
import com.google.protobuf.util.JsonFormat; | ||
|
||
// [START modelarmor_get_template] | ||
|
||
/** This class contains a main method that retrieves a template using the Model Armor API. */ | ||
public class GetTemplate { | ||
|
||
/** | ||
* Main method that calls the getTemplate method to retrieve a template. | ||
* | ||
* @param args command line arguments (not used) | ||
* @throws Exception if an error occurs during template retrieval | ||
*/ | ||
public static void main(String[] args) throws Exception { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "your-project-id"; | ||
String locationId = "your-location-id"; | ||
String templateId = "your-template-id"; | ||
|
||
getTemplate(projectId, locationId, templateId); | ||
} | ||
|
||
/** | ||
* Retrieves a template using the Model Armor API. | ||
* | ||
* @param projectId the ID of the project | ||
* @param locationId the ID of the location | ||
* @param templateId the ID of the template | ||
* @throws Exception if an error occurs during template retrieval | ||
*/ | ||
public static void getTemplate(String projectId, String locationId, String templateId) | ||
throws Exception { | ||
// Construct the API endpoint URL | ||
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); | ||
|
||
// Create a Model Armor settings object with the API endpoint | ||
ModelArmorSettings modelArmorSettings = | ||
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); | ||
|
||
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { | ||
// Construct the template name | ||
String name = TemplateName.of(projectId, locationId, templateId).toString(); | ||
|
||
// Retrieve the template using the Model Armor client | ||
Template template = client.getTemplate(name); | ||
|
||
// Print the retrieved template | ||
System.out.println("Retrieved template: " + JsonFormat.printer().print(template)); | ||
} | ||
} | ||
} | ||
|
||
// [END modelarmor_get_template] |
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.