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 ;
18+
19+ // [START compute_disk_create_secondary_regional]
20+ import com .google .cloud .compute .v1 .Disk ;
21+ import com .google .cloud .compute .v1 .DiskAsyncReplication ;
22+ import com .google .cloud .compute .v1 .Operation ;
23+ import com .google .cloud .compute .v1 .RegionDisksClient ;
24+ import java .io .IOException ;
25+ import java .util .Arrays ;
26+ import java .util .List ;
27+ import java .util .concurrent .ExecutionException ;
28+ import java .util .concurrent .TimeUnit ;
29+ import java .util .concurrent .TimeoutException ;
30+
31+ public class CreateDiskSecondaryRegional {
32+ public static void main (String [] args )
33+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
34+ // TODO(developer): Replace these variables before running the sample.
35+ // The project that contains the primary disk.
36+ String primaryProjectId = "YOUR_PROJECT_ID" ;
37+ // The project that contains the secondary disk.
38+ String secondaryProjectId = "YOUR_PROJECT_ID" ;
39+ // Name of the primary disk you want to use.
40+ String primaryDiskName = "PRIMARY_DISK_NAME" ;
41+ // Name of the disk you want to create.
42+ String secondaryDiskName = "SECONDARY_DISK_NAME" ;
43+ // Name of the region in which your primary disk is located.
44+ // Learn more about zones and regions:
45+ // https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs
46+ String primaryDiskRegion = "us-central1" ;
47+ // Name of the region in which you want to create the secondary disk.
48+ String secondaryDiskRegion = "us-east1" ;
49+ // Size of the new disk in gigabytes.
50+ // Learn more about disk requirements:
51+ // https://cloud.google.com/compute/docs/disks/async-pd/configure?authuser=0#disk_requirements
52+ long diskSizeGb = 30L ;
53+ // The type of the disk you want to create. This value uses the following format:
54+ // "projects/{projectId}/zones/{zone}/diskTypes/
55+ // (pd-standard|pd-ssd|pd-balanced|pd-extreme)".
56+ String diskType = String .format (
57+ "projects/%s/regions/%s/diskTypes/pd-balanced" , secondaryProjectId , secondaryDiskRegion );
58+
59+ createDiskSecondaryRegional (primaryProjectId , secondaryProjectId , primaryDiskName ,
60+ secondaryDiskName , primaryDiskRegion , secondaryDiskRegion , diskSizeGb , diskType );
61+ }
62+
63+ // Creates a secondary disk in a specified region.
64+ public static Disk createDiskSecondaryRegional (String projectId , String secondaryProjectId ,
65+ String primaryDiskName , String secondaryDiskName , String primaryDiskRegion ,
66+ String secondaryDiskRegion , long diskSizeGb , String diskType )
67+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
68+ // An iterable collection of zone names in which you want to keep
69+ // the new disks' replicas. One of the replica zones of the clone must match
70+ // the zone of the source disk.
71+ List <String > replicaZones = Arrays .asList (
72+ String .format ("projects/%s/zones/%s-c" , secondaryProjectId , secondaryDiskRegion ),
73+ String .format ("projects/%s/zones/%s-b" , secondaryProjectId , secondaryDiskRegion ));
74+
75+ String primaryDiskSource = String .format ("projects/%s/regions/%s/disks/%s" ,
76+ projectId , primaryDiskRegion , primaryDiskName );
77+
78+ DiskAsyncReplication asyncReplication = DiskAsyncReplication .newBuilder ()
79+ .setDisk (primaryDiskSource )
80+ .build ();
81+
82+ // Initialize client that will be used to send requests. This client only needs to be created
83+ // once, and can be reused for multiple requests.
84+ try (RegionDisksClient disksClient = RegionDisksClient .create ()) {
85+
86+ Disk disk = Disk .newBuilder ()
87+ .addAllReplicaZones (replicaZones )
88+ .setName (secondaryDiskName )
89+ .setSizeGb (diskSizeGb )
90+ .setType (diskType )
91+ .setRegion (secondaryDiskRegion )
92+ .setAsyncPrimaryDisk (asyncReplication )
93+ .build ();
94+
95+ // Wait for the create disk operation to complete.
96+ Operation response = disksClient .insertAsync (secondaryProjectId , secondaryDiskRegion , disk )
97+ .get (3 , TimeUnit .MINUTES );
98+
99+ if (response .hasError ()) {
100+ return null ;
101+ }
102+ return disksClient .get (secondaryProjectId , secondaryDiskRegion , secondaryDiskName );
103+ }
104+ }
105+ }
106+ // [END compute_disk_create_secondary_regional]
0 commit comments