|
| 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_list_templates_with_filter] |
| 20 | + |
| 21 | +import com.google.cloud.modelarmor.v1.ListTemplatesRequest; |
| 22 | +import com.google.cloud.modelarmor.v1.LocationName; |
| 23 | +import com.google.cloud.modelarmor.v1.ModelArmorClient; |
| 24 | +import com.google.cloud.modelarmor.v1.ModelArmorClient.ListTemplatesPagedResponse; |
| 25 | +import com.google.cloud.modelarmor.v1.ModelArmorSettings; |
| 26 | +import java.io.IOException; |
| 27 | + |
| 28 | +public class ListTemplatesWithFilter { |
| 29 | + |
| 30 | + public static void main(String[] args) throws IOException { |
| 31 | + // TODO(developer): Replace these variables before running the sample. |
| 32 | + |
| 33 | + String projectId = "your-project-id"; |
| 34 | + String locationId = "your-location-id"; |
| 35 | + // Filter to applied. |
| 36 | + // Example: "name=\"projects/your-project-id/locations/us-central1/your-template-id\"" |
| 37 | + String filter = "your-filter-condition"; |
| 38 | + |
| 39 | + listTemplatesWithFilter(projectId, locationId, filter); |
| 40 | + } |
| 41 | + |
| 42 | + public static ListTemplatesPagedResponse listTemplatesWithFilter(String projectId, |
| 43 | + String locationId, String filter) throws IOException { |
| 44 | + // Construct the API endpoint URL. |
| 45 | + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); |
| 46 | + |
| 47 | + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) |
| 48 | + .build(); |
| 49 | + |
| 50 | + // Initialize the client that will be used to send requests. This client |
| 51 | + // only needs to be created once, and can be reused for multiple requests. |
| 52 | + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { |
| 53 | + // Build the parent name. |
| 54 | + String parent = LocationName.of(projectId, locationId).toString(); |
| 55 | + |
| 56 | + ListTemplatesRequest request = ListTemplatesRequest.newBuilder() |
| 57 | + .setParent(parent) |
| 58 | + .setFilter(filter) |
| 59 | + .build(); |
| 60 | + |
| 61 | + // List all templates. |
| 62 | + ListTemplatesPagedResponse pagedResponse = client.listTemplates(request); |
| 63 | + pagedResponse.iterateAll().forEach(template -> { |
| 64 | + System.out.printf("Template %s\n", template.getName()); |
| 65 | + }); |
| 66 | + |
| 67 | + return pagedResponse; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// [END modelarmor_list_templates_with_filter] |
0 commit comments