Skip to content

Commit 7e05b08

Browse files
Implemented tpu_vm_list sample, created test
1 parent ec13f4d commit 7e05b08

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 com.google.cloud.tpu.v2.TpuClient.ListNodesPagedResponse;
23+
import java.io.IOException;
24+
25+
public class ListTpuVms {
26+
27+
public static void main(String[] args) throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
// Project ID or project number of the Google Cloud project you want to create a node.
30+
String projectId = "YOUR_PROJECT_ID";
31+
// The zone in which to create the TPU.
32+
// For more information about supported TPU types for specific zones,
33+
// see https://cloud.google.com/tpu/docs/regions-zones
34+
String zone = "europe-west4-a";
35+
36+
listTpuVms(projectId, zone);
37+
}
38+
39+
// Lists TPU VMs in the specified zone.
40+
public static ListNodesPagedResponse listTpuVms(String projectId, String zone)
41+
throws IOException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests.
44+
try (TpuClient tpuClient = TpuClient.create()) {
45+
String parent = String.format("projects/%s/locations/%s", projectId, zone);
46+
47+
ListNodesRequest request = ListNodesRequest.newBuilder().setParent(parent).build();
48+
return tpuClient.listNodes(request);
49+
}
50+
}
51+
}
52+
//[END tpu_vm_list]

tpu/src/test/java/tpu/TpuVmIT.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222

2323
import com.google.api.gax.rpc.NotFoundException;
2424
import com.google.cloud.tpu.v2.Node;
25+
import com.google.cloud.tpu.v2.TpuClient;
2526
import java.io.IOException;
2627
import java.util.UUID;
2728
import java.util.concurrent.ExecutionException;
2829
import java.util.concurrent.TimeUnit;
30+
import org.junit.Assert;
2931
import org.junit.jupiter.api.AfterAll;
3032
import org.junit.jupiter.api.Assertions;
3133
import org.junit.jupiter.api.BeforeAll;
@@ -96,4 +98,15 @@ public void testGetTpuVm() throws IOException {
9698
assertNotNull(node);
9799
assertThat(node.getName()).isEqualTo(NODE_PATH_NAME);
98100
}
101+
102+
@Test
103+
@Order(2)
104+
public void testListTpuVm() throws IOException {
105+
TpuClient.ListNodesPagedResponse nodesList = ListTpuVms.listTpuVms(PROJECT_ID, ZONE);
106+
107+
assertNotNull(nodesList);
108+
for (Node node : nodesList.iterateAll()) {
109+
Assert.assertTrue(node.getName().contains("test-tpu"));
110+
}
111+
}
99112
}

0 commit comments

Comments
 (0)