1717package compute .disks ;
1818
1919// [START compute_disk_stop_replication]
20- // Uncomment this line if your disk has zonal location.
21- // import com.google.cloud.compute.v1.DisksClient;
20+ import com .google .cloud .compute .v1 .DisksClient ;
2221import com .google .cloud .compute .v1 .Operation ;
2322import com .google .cloud .compute .v1 .RegionDisksClient ;
2423import java .io .IOException ;
@@ -43,21 +42,24 @@ public static void main(String[] args)
4342 public static Operation .Status stopDiskAsyncReplication (
4443 String project , String diskLocation , String diskName )
4544 throws IOException , ExecutionException , InterruptedException {
46-
47- // Uncomment this line if your disk has zonal location.
48- //try (DisksClient disksClient = DisksClient.create()) {
49-
50- // Initialize client that will be used to send requests. This client only needs to be created
51- // once, and can be reused for multiple requests.
52- try (RegionDisksClient disksClient = RegionDisksClient .create ()) {
53- Operation response = disksClient .stopAsyncReplicationAsync (
54- project , diskLocation , diskName ).get ();
55-
56- if (response .hasError ()) {
57- throw new Error ("Error stopping disk replication! " + response .getError ());
45+ Operation response ;
46+ if (Character .isDigit (diskLocation .charAt (diskLocation .length () - 1 ))) {
47+ // Initialize client that will be used to send requests. This client only needs to be created
48+ // once, and can be reused for multiple requests.
49+ try (RegionDisksClient disksClient = RegionDisksClient .create ()) {
50+ response = disksClient .stopAsyncReplicationAsync (
51+ project , diskLocation , diskName ).get ();
5852 }
59- return response .getStatus ();
53+ } else {
54+ try (DisksClient disksClient = DisksClient .create ()) {
55+ response = disksClient .stopAsyncReplicationAsync (
56+ project , diskLocation , diskName ).get ();
57+ }
58+ }
59+ if (response .hasError ()) {
60+ throw new Error ("Error stopping disk replication! " + response .getError ());
6061 }
62+ return response .getStatus ();
6163 }
6264}
6365// [END compute_disk_stop_replication]
0 commit comments