-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathCreateDiskSecondaryZonal.java
93 lines (83 loc) · 3.96 KB
/
CreateDiskSecondaryZonal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package compute.disks;
// [START compute_disk_create_secondary]
import com.google.cloud.compute.v1.Disk;
import com.google.cloud.compute.v1.DiskAsyncReplication;
import com.google.cloud.compute.v1.DisksClient;
import com.google.cloud.compute.v1.Operation;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class CreateDiskSecondaryZonal {
public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// The project that contains the primary disk.
String primaryProjectId = "PRIMARY_PROJECT_ID";
// The project that contains the secondary disk.
String secondaryProjectId = "SECONDARY_PROJECT_ID";
// Name of the primary disk you want to use.
String primaryDiskName = "PRIMARY_DISK_NAME";
// Name of the zone in which your primary disk is located.
// Learn more about zones and regions:
// https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs
String primaryDiskZone = "us-central1-a";
// Name of the disk you want to create.
String secondaryDiskName = "SECONDARY_DISK_NAME";
// Name of the zone in which you want to create the secondary disk.
String secondaryDiskZone = "us-east1-c";
// Size of the new disk in gigabytes.
long diskSizeGb = 30L;
// The type of the disk you want to create. This value uses the following format:
// "projects/{projectId}/zones/{zone}/diskTypes/
// (pd-standard|pd-ssd|pd-balanced|pd-extreme)".
String diskType = String.format(
"projects/%s/zones/%s/diskTypes/pd-balanced", secondaryProjectId, secondaryDiskZone);
createDiskSecondaryZonal(primaryProjectId, secondaryProjectId, primaryDiskName,
secondaryDiskName, primaryDiskZone, secondaryDiskZone, diskSizeGb, diskType);
}
// Creates a secondary disk in a specified zone.
public static Operation.Status createDiskSecondaryZonal(String primaryProjectId,
String secondaryProjectId, String primaryDiskName, String secondaryDiskName,
String primaryDiskZone, String secondaryDiskZone, long diskSizeGb, String diskType)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
String primaryDiskSource = String.format("projects/%s/zones/%s/disks/%s",
primaryProjectId, primaryDiskZone, primaryDiskName);
DiskAsyncReplication asyncReplication = DiskAsyncReplication.newBuilder()
.setDisk(primaryDiskSource)
.build();
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (DisksClient disksClient = DisksClient.create()) {
Disk disk = Disk.newBuilder()
.setName(secondaryDiskName)
.setSizeGb(diskSizeGb)
.setType(diskType)
.setZone(secondaryDiskZone)
.setAsyncPrimaryDisk(asyncReplication)
.build();
Operation response = disksClient.insertAsync(secondaryProjectId, secondaryDiskZone, disk)
.get(3, TimeUnit.MINUTES);
if (response.hasError()) {
throw new Error("Error creating secondary disks! " + response.getError());
}
return response.getStatus();
}
}
}
// [END compute_disk_create_secondary]