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 compute .disks .consistencygroup ;
18+
19+ // [START compute_consistency_group_clone]
20+ // If your disk has zonal location uncomment these lines
21+ //import com.google.cloud.compute.v1.DisksClient;
22+ //import com.google.cloud.compute.v1.BulkInsertDiskRequest;
23+ import com .google .cloud .compute .v1 .BulkInsertDiskResource ;
24+ import com .google .cloud .compute .v1 .BulkInsertRegionDiskRequest ;
25+ import com .google .cloud .compute .v1 .Operation ;
26+ import com .google .cloud .compute .v1 .RegionDisksClient ;
27+ import java .io .IOException ;
28+ import java .util .concurrent .ExecutionException ;
29+ import java .util .concurrent .TimeUnit ;
30+ import java .util .concurrent .TimeoutException ;
31+
32+ public class CloneDisksFromConsistencyGroup {
33+
34+ public static void main (String [] args )
35+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
36+ // TODO(developer): Replace these variables before running the sample.
37+ // Project ID or project number of the Cloud project you want to use.
38+ String project = "YOUR_PROJECT_ID" ;
39+ // Name of the region or zone in which your disk is located.
40+ String disksLocation = "us-central1" ;
41+ // Name of the consistency group you want to clone disks from.
42+ String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME" ;
43+ // Name of the region in which your consistency group is located.
44+ String consistencyGroupLocation = "us-central1" ;
45+
46+ cloneDisksFromConsistencyGroup (
47+ project , disksLocation , consistencyGroupName , consistencyGroupLocation );
48+ }
49+
50+ // Clones disks from a consistency group.
51+ public static void cloneDisksFromConsistencyGroup (String project , String disksLocation ,
52+ String consistencyGroupName , String consistencyGroupLocation )
53+ throws IOException , InterruptedException , ExecutionException , TimeoutException {
54+ String sourceConsistencyGroupPolicy = String .format (
55+ "projects/%s/regions/%s/resourcePolicies/%s" , project , consistencyGroupLocation ,
56+ consistencyGroupName );
57+
58+ //try (DisksClient disksClient = DisksClient.create()){
59+ // BulkInsertDiskRequest request = BulkInsertDiskRequest.newBuilder()
60+ // .setProject(project)
61+ // .setZone(disksLocation)
62+ // .setBulkInsertDiskResourceResource(
63+ // BulkInsertDiskResource.newBuilder()
64+ // .setSourceConsistencyGroupPolicy(sourceConsistencyGroupPolicy)
65+ // .build())
66+ // .build();
67+ //Operation response = disksClient.bulkInsertAsync(request).get(3, TimeUnit.MINUTES);
68+
69+
70+ try (RegionDisksClient regionDisksClient = RegionDisksClient .create ()) {
71+ BulkInsertRegionDiskRequest request = BulkInsertRegionDiskRequest .newBuilder ()
72+ .setProject (project )
73+ .setRegion (disksLocation )
74+ .setBulkInsertDiskResourceResource (
75+ BulkInsertDiskResource .newBuilder ()
76+ .setSourceConsistencyGroupPolicy (sourceConsistencyGroupPolicy )
77+ .build ())
78+ .build ();
79+
80+ Operation response = regionDisksClient .bulkInsertAsync (request ).get (3 , TimeUnit .MINUTES );
81+
82+ if (response .hasError ()) {
83+ System .out .println (String .format ("Error cloning disks: %s" , response .getError ()));
84+ return ;
85+ }
86+ System .out .printf ("Disks cloned from consistency group: %s\n " , consistencyGroupName );
87+ }
88+ }
89+ }
90+ // [END compute_consistency_group_clone]
0 commit comments