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