-
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 1 commit
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,45 @@ | ||
/* | ||
* 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; | ||
|
||
import com.google.cloud.modelarmor.v1.ModelArmorClient; | ||
import com.google.cloud.modelarmor.v1.ModelArmorSettings; | ||
import com.google.cloud.modelarmor.v1.TemplateName; | ||
|
||
public class DeleteTemplate { | ||
|
||
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); | ||
} | ||
|
||
public static void deleteTemplate(String projectId, String locationId, String templateId) | ||
throws Exception { | ||
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); | ||
ModelArmorSettings modelArmorSettings = | ||
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); | ||
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { | ||
String name = TemplateName.of(projectId, locationId, templateId).toString(); | ||
client.deleteTemplate(name); | ||
System.out.println("Deleted template: " + name); | ||
} | ||
} | ||
} |
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,48 @@ | ||
/* | ||
* 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; | ||
|
||
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; | ||
|
||
public class GetTemplate { | ||
|
||
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); | ||
} | ||
|
||
public static void getTemplate(String projectId, String locationId, String templateId) | ||
throws Exception { | ||
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ModelArmorSettings modelArmorSettings = | ||
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); | ||
|
||
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { | ||
String name = TemplateName.of(projectId, locationId, templateId).toString(); | ||
Template template = client.getTemplate(name); | ||
System.out.println("Retrieved template: " + JsonFormat.printer().print(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,49 @@ | ||
/* | ||
* 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; | ||
|
||
import com.google.cloud.modelarmor.v1.ListTemplatesRequest; | ||
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.Template; | ||
import com.google.protobuf.util.JsonFormat; | ||
|
||
public class ListTemplates { | ||
|
||
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"; | ||
|
||
listTemplates(projectId, locationId); | ||
} | ||
|
||
public static void listTemplates(String projectId, String locationId) throws Exception { | ||
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ModelArmorSettings modelArmorSettings = | ||
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); | ||
|
||
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { | ||
String parent = LocationName.of(projectId, locationId).toString(); | ||
ListTemplatesRequest request = ListTemplatesRequest.newBuilder().setParent(parent).build(); | ||
for (Template template : client.listTemplates(request).iterateAll()) { | ||
System.out.println("Retrived Templates: " + JsonFormat.printer().print(template)); | ||
} | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
modelarmor/main/java/modelarmor/ListTemplatesWithFilter.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; | ||
|
||
import com.google.cloud.modelarmor.v1.ListTemplatesRequest; | ||
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.Template; | ||
import com.google.protobuf.util.JsonFormat; | ||
|
||
public class ListTemplatesWithFilter { | ||
|
||
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"; | ||
|
||
listTemplatesWithFilter(projectId, locationId, templateId); | ||
} | ||
|
||
public static void listTemplatesWithFilter(String projectId, String locationId, String templateId) | ||
throws Exception { | ||
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ModelArmorSettings modelArmorSettings = | ||
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); | ||
|
||
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { | ||
String parent = LocationName.of(projectId, locationId).toString(); | ||
String filter = String.format("name=\"%s/templates/%s\"", parent, templateId); | ||
ListTemplatesRequest request = | ||
ListTemplatesRequest.newBuilder().setParent(parent).setFilter(filter).build(); | ||
for (Template template : client.listTemplates(request).iterateAll()) { | ||
System.out.println("Template with filter: " + JsonFormat.printer().print(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,111 @@ | ||
/* | ||
* 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; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.cloud.modelarmor.v1.Template; | ||
import com.google.common.base.Strings; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Integration (system) tests for {@link Snippets}. */ | ||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:AbbreviationAsWordInName") | ||
public class SnippetsIT { | ||
|
||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private static final String LOCATION = "us-central1"; | ||
private static final String MA_REGIONAL_ENDPOINT = | ||
String.format("modelarmor.%s.rep.googleapis.com:443", LOCATION); | ||
private static final String DLP_REGIONAL_ENDPOINT = | ||
String.format("dlp.%s.rep.googleapis.com:443", LOCATION); | ||
private static final String INSPECT_TEMPLATE_ID = | ||
"model-armour-inspect-template-" + UUID.randomUUID().toString(); | ||
private static final String DEIDENTIFY_TEMPLATE_ID = | ||
"model-armour-deidentify-template-" + UUID.randomUUID().toString(); | ||
private static Template TEST_MODELARMOR_TEMPLATE; | ||
private static Template TEST_MODELARMOR_TEMPLATE_NAME; | ||
private static String TEMPLATE_ID; | ||
|
||
private ByteArrayOutputStream stdOut; | ||
|
||
@BeforeClass | ||
public static void beforeAll() throws Exception { | ||
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); | ||
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION)); | ||
} | ||
|
||
@AfterClass | ||
public static void afterAll() throws Exception { | ||
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); | ||
} | ||
|
||
@Before | ||
public void beforeEach() { | ||
stdOut = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(stdOut)); | ||
|
||
TEMPLATE_ID = "test-model-armor-" + UUID.randomUUID().toString(); | ||
} | ||
|
||
@After | ||
public void afterEach() throws Exception { | ||
stdOut = null; | ||
System.setOut(null); | ||
} | ||
|
||
@Test | ||
public void testDeleteModelArmorTemplate() throws Exception { | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); | ||
DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); | ||
assertThat(stdOut.toString()).contains("Deleted template"); | ||
} | ||
|
||
@Test | ||
public void testGetModelArmorTemplate() throws Exception { | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); | ||
GetTemplate.getTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); | ||
assertThat(stdOut.toString()).contains("Retrieved template"); | ||
DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); | ||
} | ||
|
||
@Test | ||
public void testListModelArmorTemplates() throws Exception { | ||
CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); | ||
ListTemplates.listTemplates(PROJECT_ID, LOCATION); | ||
assertThat(stdOut.toString()).contains("Retrived Templates"); | ||
DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); | ||
} | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Test | ||
public void testListTemplatesWithFilter() throws Exception { | ||
CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); | ||
ListTemplatesWithFilter.listTemplatesWithFilter(PROJECT_ID, LOCATION, TEMPLATE_ID); | ||
assertThat(stdOut.toString()).contains("Template with filter"); | ||
DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); | ||
} | ||
} |
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.