1717package compute .disks .consistencygroup ;
1818
1919// [START compute_consistency_group_clone]
20- import com .google .cloud .compute .v1 .BulkInsertDiskRequest ;
2120import com .google .cloud .compute .v1 .BulkInsertDiskResource ;
2221import com .google .cloud .compute .v1 .BulkInsertRegionDiskRequest ;
23- import com .google .cloud .compute .v1 .DisksClient ;
2422import com .google .cloud .compute .v1 .Operation ;
2523import com .google .cloud .compute .v1 .RegionDisksClient ;
2624import java .io .IOException ;
@@ -35,58 +33,39 @@ public static void main(String[] args)
3533 // TODO(developer): Replace these variables before running the sample.
3634 // Project ID or project number of the Cloud project you want to use.
3735 String project = "YOUR_PROJECT_ID" ;
38- // Name of the region or zone in which your disk is located.
39- String disksLocation = "us-central1" ;
36+ // Region in which your disk and consistency group are located.
37+ String region = "us-central1" ;
4038 // Name of the consistency group you want to clone disks from.
4139 String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME" ;
42- // Name of the region in which your consistency group is located.
43- String consistencyGroupLocation = "us-central1" ;
4440
45- cloneDisksFromConsistencyGroup (
46- project , disksLocation , consistencyGroupName , consistencyGroupLocation );
41+ cloneDisksFromConsistencyGroup (project , region , consistencyGroupName );
4742 }
4843
49- // Clones disks from a consistency group.
50- public static Operation .Status cloneDisksFromConsistencyGroup (String project ,
51- String disksLocation , String consistencyGroupName , String consistencyGroupLocation )
44+ // Clones disks with regional location from a consistency group.
45+ public static Operation .Status cloneDisksFromConsistencyGroup (
46+ String project , String region , String consistencyGroupName )
5247 throws IOException , InterruptedException , ExecutionException , TimeoutException {
5348 String sourceConsistencyGroupPolicy = String .format (
54- "projects/%s/regions/%s/resourcePolicies/%s" , project , consistencyGroupLocation ,
55- consistencyGroupName );
56- Operation response ;
57- if (Character .isDigit (disksLocation .charAt (disksLocation .length () - 1 ))) {
58- // Initialize client that will be used to send requests. This client only needs to be created
59- // once, and can be reused for multiple requests.
60- try (RegionDisksClient disksClient = RegionDisksClient .create ()) {
61- BulkInsertRegionDiskRequest request = BulkInsertRegionDiskRequest .newBuilder ()
62- .setProject (project )
63- .setRegion (disksLocation )
64- .setBulkInsertDiskResourceResource (
65- BulkInsertDiskResource .newBuilder ()
66- .setSourceConsistencyGroupPolicy (sourceConsistencyGroupPolicy )
67- .build ())
68- .build ();
49+ "projects/%s/regions/%s/resourcePolicies/%s" , project , region , consistencyGroupName );
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+ BulkInsertRegionDiskRequest request = BulkInsertRegionDiskRequest .newBuilder ()
54+ .setProject (project )
55+ .setRegion (region )
56+ .setBulkInsertDiskResourceResource (
57+ BulkInsertDiskResource .newBuilder ()
58+ .setSourceConsistencyGroupPolicy (sourceConsistencyGroupPolicy )
59+ .build ())
60+ .build ();
6961
70- response = disksClient .bulkInsertAsync (request ).get (3 , TimeUnit .MINUTES );
71- }
72- } else {
73- try (DisksClient disksClient = DisksClient .create ()) {
74- BulkInsertDiskRequest request = BulkInsertDiskRequest .newBuilder ()
75- .setProject (project )
76- .setZone (disksLocation )
77- .setBulkInsertDiskResourceResource (
78- BulkInsertDiskResource .newBuilder ()
79- .setSourceConsistencyGroupPolicy (sourceConsistencyGroupPolicy )
80- .build ())
81- .build ();
62+ Operation response = disksClient .bulkInsertAsync (request ).get (3 , TimeUnit .MINUTES );
8263
83- response = disksClient .bulkInsertAsync (request ).get (3 , TimeUnit .MINUTES );
64+ if (response .hasError ()) {
65+ throw new Error ("Error cloning disks! " + response .getError ());
8466 }
67+ return response .getStatus ();
8568 }
86- if (response .hasError ()) {
87- throw new Error ("Error cloning disks! " + response .getError ());
88- }
89- return response .getStatus ();
9069 }
9170}
9271// [END compute_consistency_group_clone]
0 commit comments