|
| 1 | +/* |
| 2 | + * Copyright 2024 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 tpu; |
| 18 | + |
| 19 | +//[START tpu_vm_list] |
| 20 | +import com.google.cloud.tpu.v2.ListNodesRequest; |
| 21 | +import com.google.cloud.tpu.v2.TpuClient; |
| 22 | +import java.io.IOException; |
| 23 | + |
| 24 | +public class ListTpuVms { |
| 25 | + |
| 26 | + public static void main(String[] args) throws IOException { |
| 27 | + // TODO(developer): Replace these variables before running the sample. |
| 28 | + // Project ID or project number of the Google Cloud project you want to use. |
| 29 | + String projectId = "YOUR_PROJECT_ID"; |
| 30 | + // The zone where the TPUs are located. |
| 31 | + // For more information about supported TPU types for specific zones, |
| 32 | + // see https://cloud.google.com/tpu/docs/regions-zones |
| 33 | + String zone = "us-central1-f"; |
| 34 | + |
| 35 | + listTpuVms(projectId, zone); |
| 36 | + } |
| 37 | + |
| 38 | + // Lists TPU VMs in the specified zone. |
| 39 | + public static TpuClient.ListNodesPage listTpuVms(String projectId, String zone) |
| 40 | + throws IOException { |
| 41 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 42 | + // once, and can be reused for multiple requests. |
| 43 | + try (TpuClient tpuClient = TpuClient.create()) { |
| 44 | + String parent = String.format("projects/%s/locations/%s", projectId, zone); |
| 45 | + |
| 46 | + ListNodesRequest request = ListNodesRequest.newBuilder().setParent(parent).build(); |
| 47 | + |
| 48 | + return tpuClient.listNodes(request).getPage(); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | +//[END tpu_vm_list] |
0 commit comments