Skip to content

Commit 11eedd1

Browse files
fix: update compute sample to account for region-specific operation resources (GoogleCloudPlatform#5505)
* add region-specific operations check * mvn lint change
1 parent 84f54c1 commit 11eedd1

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

compute/cmdline/src/main/java/ComputeEngineSample.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ private static Operation deleteInstance(Compute compute, String instanceName) th
235235
return delete.execute();
236236
}
237237

238+
public static String getLastWordFromUrl(String url) {
239+
if (url != null) {
240+
String[] bits = url.split("/");
241+
url = bits[bits.length - 1];
242+
}
243+
return url;
244+
}
245+
238246
// [START wait_until_complete]
239247
/**
240248
* Wait until {@code operation} is completed.
@@ -250,11 +258,8 @@ public static Operation.Error blockUntilComplete(
250258
Compute compute, Operation operation, long timeout) throws Exception {
251259
long start = System.currentTimeMillis();
252260
final long pollInterval = 5 * 1000;
253-
String zone = operation.getZone(); // null for global/regional operations
254-
if (zone != null) {
255-
String[] bits = zone.split("/");
256-
zone = bits[bits.length - 1];
257-
}
261+
String zone = getLastWordFromUrl(operation.getZone()); // null for global/regional operations
262+
String region = getLastWordFromUrl(operation.getRegion());
258263
String status = operation.getStatus();
259264
String opId = operation.getName();
260265
while (operation != null && !status.equals("DONE")) {
@@ -267,6 +272,9 @@ public static Operation.Error blockUntilComplete(
267272
if (zone != null) {
268273
Compute.ZoneOperations.Get get = compute.zoneOperations().get(PROJECT_ID, zone, opId);
269274
operation = get.execute();
275+
} else if (region != null) {
276+
Compute.RegionOperations.Get get = compute.regionOperations().get(PROJECT_ID, region, opId);
277+
operation = get.execute();
270278
} else {
271279
Compute.GlobalOperations.Get get = compute.globalOperations().get(PROJECT_ID, opId);
272280
operation = get.execute();

0 commit comments

Comments
 (0)