Skip to content

Commit 540b943

Browse files
Fixed code
1 parent 874b877 commit 540b943

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

compute/cloud-client/src/main/java/compute/disks/CreateDiskSecondaryCustom.java

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
//[START compute_disk_create_secondary_custom]
2020
import com.google.cloud.compute.v1.Disk;
21+
import com.google.cloud.compute.v1.DiskAsyncReplication;
2122
import com.google.cloud.compute.v1.DisksClient;
2223
import com.google.cloud.compute.v1.GuestOsFeature;
2324
import com.google.cloud.compute.v1.Operation;
@@ -36,63 +37,72 @@ public static void main(String[] args)
3637
// TODO(developer): Replace these variables before running the sample.
3738
// The project that contains the primary disk.
3839
String projectId = "YOUR_PROJECT_ID";
39-
// Name of the zone in which you want to create the secondary disk.
40-
String disksZone = "us-central1-a";
40+
// Name of the primary disk you want to use.
41+
String primaryDiskName = "PRIMARY_DISK_NAME";
42+
// Name of the zone in which your primary disk is located.
43+
// Learn more about zones and regions:
44+
// https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs
45+
String primaryDiskZone = "us-central1-a";
4146
// Name of the disk you want to create.
4247
String secondaryDiskName = "SECONDARY_DISK_NAME";
48+
// Name of the zone in which you want to create the secondary disk.
49+
String secondaryDiskZone = "us-east1-c";
4350
// Size of the new disk in gigabytes.
4451
long diskSizeGb = 100;
45-
// Name of the primary disk you want to use as a source for the new disk.
46-
String primaryDiskName = "PRIMARY_DISK_NAME";
4752
// The type of the disk you want to create. This value uses the following format:
4853
// "projects/{projectId}/zones/{zone}/diskTypes/
4954
// (pd-standard|pd-ssd|pd-balanced|pd-extreme)".
5055
String diskType = String.format(
51-
"projects/%s/zones/%s/diskTypes/pd-balanced", projectId, disksZone);
56+
"projects/%s/zones/%s/diskTypes/pd-balanced", projectId, secondaryDiskZone);
5257

53-
createDiskSecondaryCustom(projectId, secondaryDiskName, disksZone,
54-
diskSizeGb, primaryDiskName, diskType);
58+
createDiskSecondaryCustom(projectId, primaryDiskName, secondaryDiskName,
59+
primaryDiskZone, secondaryDiskZone, diskSizeGb, diskType);
5560
}
5661

5762
// Creates a secondary disk with specified custom parameters.
58-
public static Disk createDiskSecondaryCustom(String projectId, String secondaryDiskName,
59-
String disksZone, long diskSizeGb, String primaryDiskName, String diskType)
63+
public static Disk createDiskSecondaryCustom(String projectId, String primaryDiskName,
64+
String secondaryDiskName, String primaryDiskZone, String secondaryDiskZone,
65+
long diskSizeGb, String diskType)
6066
throws IOException, ExecutionException, InterruptedException, TimeoutException {
67+
String primaryDiskSource = String.format("projects/%s/zones/%s/disks/%s",
68+
projectId, primaryDiskZone, primaryDiskName);
69+
70+
DiskAsyncReplication asyncReplication = DiskAsyncReplication.newBuilder()
71+
.setDisk(primaryDiskSource)
72+
.build();
73+
74+
// Define the guest OS features.
75+
List<GuestOsFeature> guestOsFeatures = Arrays.asList(
76+
GuestOsFeature.newBuilder().setType("UEFI_COMPATIBLE").build(),
77+
GuestOsFeature.newBuilder().setType("GVNIC").build(),
78+
GuestOsFeature.newBuilder().setType("MULTI_IP_SUBNET").build());
79+
80+
// Define the labels.
81+
Map<String, String> labels = new HashMap<>();
82+
labels.put("secondary-disk-for-replication", "yes");
83+
6184
// Initialize client that will be used to send requests. This client only needs to be created
6285
// once, and can be reused for multiple requests.
6386
try (DisksClient disksClient = DisksClient.create()) {
64-
String primaryDiskSource = String.format("projects/%s/zones/%s/disks/%s",
65-
projectId, disksZone, primaryDiskName);
66-
67-
// Define the guest OS features.
68-
List<GuestOsFeature> guestOsFeatures = Arrays.asList(
69-
GuestOsFeature.newBuilder().setType("UEFI_COMPATIBLE").build(),
70-
GuestOsFeature.newBuilder().setType("GVNIC").build(),
71-
GuestOsFeature.newBuilder().setType("MULTI_IP_SUBNET").build()
72-
);
73-
// Define the labels.
74-
Map<String, String> labels = new HashMap<>();
75-
labels.put("secondary-disk-for-replication", "yes");
7687

77-
// Create the disk object with the source disk information.
7888
Disk disk = Disk.newBuilder()
7989
.setName(secondaryDiskName)
8090
.setSizeGb(diskSizeGb)
8191
.setType(diskType)
82-
.setZone(disksZone)
92+
.setZone(secondaryDiskZone)
8393
.addAllGuestOsFeatures(guestOsFeatures)
8494
.putAllLabels(labels)
85-
.setSourceDisk(primaryDiskSource)
95+
.setAsyncPrimaryDisk(asyncReplication)
8696
.build();
8797

8898
// Wait for the create disk operation to complete.
89-
Operation response = disksClient.insertAsync(projectId, disksZone, disk)
99+
Operation response = disksClient.insertAsync(projectId, secondaryDiskZone, disk)
90100
.get(3, TimeUnit.MINUTES);
91101

92102
if (response.hasError()) {
93103
return null;
94104
}
95-
return disksClient.get(projectId, disksZone, secondaryDiskName);
105+
return disksClient.get(projectId, secondaryDiskZone, secondaryDiskName);
96106
}
97107
}
98108
}

compute/cloud-client/src/test/java/compute/disks/DisksIT.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class DisksIT {
7272
private static String DISK_TYPE;
7373
private static String ZONAL_BLANK_DISK;
7474
private static String REGIONAL_BLANK_DISK;
75-
private static String SECONDARY_DISK_CUSTOM_NAME;
75+
private static String SECONDARY_DISK_CUSTOM;
7676
private static final long DISK_SIZE = 10;
7777

7878
private ByteArrayOutputStream stdOut;
@@ -102,7 +102,7 @@ public static void setup()
102102
DISK_TYPE = String.format("zones/%s/diskTypes/pd-ssd", ZONE);
103103
ZONAL_BLANK_DISK = "gcloud-test-disk-zattach-" + uuid;
104104
REGIONAL_BLANK_DISK = "gcloud-test-disk-rattach-" + uuid;
105-
SECONDARY_DISK_CUSTOM_NAME = "gcloud-test-disk-custom-" + uuid;
105+
SECONDARY_DISK_CUSTOM = "gcloud-test-disk-custom-" + uuid;
106106

107107
// Cleanup existing stale instances.
108108
Util.cleanUpExistingInstances("test-disks", PROJECT_ID, ZONE);
@@ -172,7 +172,6 @@ public static void cleanUp()
172172
DeleteDisk.deleteDisk(PROJECT_ID, ZONE, EMPTY_DISK_NAME);
173173
DeleteDisk.deleteDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK);
174174
RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK);
175-
DeleteDisk.deleteDisk(PROJECT_ID, ZONE, SECONDARY_DISK_CUSTOM_NAME);
176175

177176
stdOut.close();
178177
System.setOut(out);
@@ -310,16 +309,15 @@ public void testCreateDiskSecondaryCustom()
310309
String diskType = String.format(
311310
"projects/%s/zones/%s/diskTypes/pd-ssd", PROJECT_ID, ZONE);
312311
Disk disk = CreateDiskSecondaryCustom.createDiskSecondaryCustom(
313-
PROJECT_ID, SECONDARY_DISK_CUSTOM_NAME, ZONE,
314-
DISK_SIZE, EMPTY_DISK_NAME, diskType);
312+
PROJECT_ID, EMPTY_DISK_NAME, SECONDARY_DISK_CUSTOM, ZONE,
313+
"us-central1-c", DISK_SIZE, diskType);
315314

316315
// Verify that the secondary disk was created.
317316
assertNotNull(disk);
318-
assertThat(disk.getSizeGb()).isEqualTo(DISK_SIZE);
319-
assertThat(disk.getSourceDisk()).isEqualTo(
320-
String.format("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/disks/%s",
321-
PROJECT_ID, ZONE, EMPTY_DISK_NAME));
317+
assertThat(disk.getAsyncPrimaryDisk().getDisk().contains(EMPTY_DISK_NAME));
322318
assertThat(disk.getLabelsMap().get("secondary-disk-for-replication")).isEqualTo("yes");
323319
assertThat(disk.getGuestOsFeaturesCount()).isEqualTo(3);
320+
321+
DeleteDisk.deleteDisk(PROJECT_ID, "us-central1-c", SECONDARY_DISK_CUSTOM);
324322
}
325323
}

0 commit comments

Comments
 (0)