Skip to content

Commit 8e31c56

Browse files
feat(compute): add compute disk create secondary sample. (GoogleCloudPlatform#9643)
* Implemented compute_disk_create_secondary sample, created test * Fixed code * Fixed variable * Fixed code * Merged changes from main * Fixed lint issue
1 parent 0795de2 commit 8e31c56

File tree

2 files changed

+122
-7
lines changed

2 files changed

+122
-7
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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]
20+
import com.google.cloud.compute.v1.Disk;
21+
import com.google.cloud.compute.v1.DiskAsyncReplication;
22+
import com.google.cloud.compute.v1.DisksClient;
23+
import com.google.cloud.compute.v1.Operation;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.TimeoutException;
28+
29+
public class CreateDiskSecondaryZonal {
30+
public static void main(String[] args)
31+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
// The project that contains the primary disk.
34+
String primaryProjectId = "PRIMARY_PROJECT_ID";
35+
// The project that contains the secondary disk.
36+
String secondaryProjectId = "SECONDARY_PROJECT_ID";
37+
// Name of the primary disk you want to use.
38+
String primaryDiskName = "PRIMARY_DISK_NAME";
39+
// Name of the zone in which your primary disk is located.
40+
// Learn more about zones and regions:
41+
// https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs
42+
String primaryDiskZone = "us-central1-a";
43+
// Name of the disk you want to create.
44+
String secondaryDiskName = "SECONDARY_DISK_NAME";
45+
// Name of the zone in which you want to create the secondary disk.
46+
String secondaryDiskZone = "us-east1-c";
47+
// Size of the new disk in gigabytes.
48+
long diskSizeGb = 30L;
49+
// The type of the disk you want to create. This value uses the following format:
50+
// "projects/{projectId}/zones/{zone}/diskTypes/
51+
// (pd-standard|pd-ssd|pd-balanced|pd-extreme)".
52+
String diskType = String.format(
53+
"projects/%s/zones/%s/diskTypes/pd-balanced", secondaryProjectId, secondaryDiskZone);
54+
55+
createDiskSecondaryZonal(primaryProjectId, secondaryProjectId, primaryDiskName,
56+
secondaryDiskName, primaryDiskZone, secondaryDiskZone, diskSizeGb, diskType);
57+
}
58+
59+
// Creates a secondary disk in a specified zone.
60+
public static Operation.Status createDiskSecondaryZonal(String primaryProjectId,
61+
String secondaryProjectId, String primaryDiskName, String secondaryDiskName,
62+
String primaryDiskZone, String secondaryDiskZone, long diskSizeGb, String diskType)
63+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
64+
// Initialize client that will be used to send requests. This client only needs to be created
65+
// once, and can be reused for multiple requests.
66+
try (DisksClient disksClient = DisksClient.create()) {
67+
String primaryDiskSource = String.format("projects/%s/zones/%s/disks/%s",
68+
primaryProjectId, primaryDiskZone, primaryDiskName);
69+
70+
DiskAsyncReplication asyncReplication = DiskAsyncReplication.newBuilder()
71+
.setDisk(primaryDiskSource)
72+
.build();
73+
Disk disk = Disk.newBuilder()
74+
.setName(secondaryDiskName)
75+
.setZone(secondaryDiskZone)
76+
.setSizeGb(diskSizeGb)
77+
.setType(diskType)
78+
.setAsyncPrimaryDisk(asyncReplication)
79+
.build();
80+
81+
Operation response = disksClient.insertAsync(secondaryProjectId, secondaryDiskZone, disk)
82+
.get(3, TimeUnit.MINUTES);
83+
84+
if (response.hasError()) {
85+
throw new Error("Error creating secondary disks! " + response.getError());
86+
}
87+
return response.getStatus();
88+
}
89+
}
90+
}
91+
// [END compute_disk_create_secondary]
92+

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

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

1919
import static com.google.common.truth.Truth.assertThat;
2020
import static com.google.common.truth.Truth.assertWithMessage;
21+
import static org.junit.Assert.assertEquals;
2122

2223
import com.google.cloud.compute.v1.AttachedDisk;
2324
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
@@ -46,7 +47,6 @@
4647
import java.util.concurrent.ExecutionException;
4748
import java.util.concurrent.TimeUnit;
4849
import java.util.concurrent.TimeoutException;
49-
import org.junit.Assert;
5050
import org.junit.jupiter.api.AfterAll;
5151
import org.junit.jupiter.api.AfterEach;
5252
import org.junit.jupiter.api.BeforeAll;
@@ -77,6 +77,7 @@ public class DisksIT {
7777
String.format("projects/%s/zones/%s-a", PROJECT_ID, REGION),
7878
String.format("projects/%s/zones/%s-b", PROJECT_ID, REGION));
7979
private static String SECONDARY_REGIONAL_DISK;
80+
private static String SECONDARY_DISK;
8081
private static final long DISK_SIZE = 10L;
8182
private ByteArrayOutputStream stdOut;
8283

@@ -107,14 +108,17 @@ public static void setup()
107108
REGIONAL_BLANK_DISK = "gcloud-test-disk-rattach-" + uuid;
108109
REGIONAL_REPLICATED_DISK = "gcloud-test-disk-replicated-" + uuid;
109110
SECONDARY_REGIONAL_DISK = "gcloud-test-disk-secondary-regional-" + uuid;
111+
SECONDARY_DISK = "gcloud-test-disk-secondary-" + uuid;
110112

111113
// Cleanup existing stale resources.
112114
Util.cleanUpExistingInstances("test-disks", PROJECT_ID, ZONE);
113115
Util.cleanUpExistingDisks("gcloud-test-", PROJECT_ID, ZONE);
114-
Util.cleanUpExistingRegionalDisks("gcloud-test-disk-secondary-regional-", PROJECT_ID, REGION);
115-
Util.cleanUpExistingRegionalDisks("gcloud-test-disk-rattach-", PROJECT_ID, REGION);
116-
Util.cleanUpExistingSnapshots("gcloud-test-snapshot-", PROJECT_ID);
116+
Util.cleanUpExistingDisks("gcloud-test-", PROJECT_ID, "us-central1-c");
117+
Util.cleanUpExistingRegionalDisks(
118+
"gcloud-test-disk-secondary-regional-", PROJECT_ID, "us-central1");
117119
Util.cleanUpExistingRegionalDisks("gcloud-test-disk-", PROJECT_ID, REGION);
120+
Util.cleanUpExistingSnapshots("gcloud-test-snapshot-", PROJECT_ID);
121+
118122
// Create disk from image.
119123
Image debianImage = null;
120124
try (ImagesClient imagesClient = ImagesClient.create()) {
@@ -181,6 +185,7 @@ public static void cleanUp()
181185
RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK);
182186
RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, REGIONAL_REPLICATED_DISK);
183187
RegionalDelete.deleteRegionalDisk(PROJECT_ID, "us-central1", SECONDARY_REGIONAL_DISK);
188+
DeleteDisk.deleteDisk(PROJECT_ID, "us-central1-c", SECONDARY_DISK);
184189

185190
stdOut.close();
186191
System.setOut(out);
@@ -285,7 +290,7 @@ public void testDiskAttachResize()
285290
throws IOException, ExecutionException, InterruptedException, TimeoutException {
286291
// Test disk attach.
287292
Instance instance = Util.getInstance(PROJECT_ID, ZONE, INSTANCE_NAME);
288-
Assert.assertEquals(1, instance.getDisksCount());
293+
assertEquals(1, instance.getDisksCount());
289294

290295
Disk zonalDisk = Util.getDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK);
291296
Disk regionalDisk = Util.getRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK);
@@ -305,8 +310,8 @@ public void testDiskAttachResize()
305310
ResizeRegionalDisk.resizeRegionalDisk(PROJECT_ID, regionalDisk.getRegion().split("regions/")[1],
306311
regionalDisk.getName(), 23);
307312

308-
Assert.assertEquals(22, Util.getDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK).getSizeGb());
309-
Assert.assertEquals(23,
313+
assertEquals(22, Util.getDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK).getSizeGb());
314+
assertEquals(23,
310315
Util.getRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK).getSizeGb());
311316
}
312317

@@ -315,8 +320,10 @@ public void testCreateReplicatedDisk()
315320
throws IOException, ExecutionException, InterruptedException, TimeoutException {
316321
Status status = CreateReplicatedDisk.createReplicatedDisk(PROJECT_ID, REGION,
317322
replicaZones, REGIONAL_REPLICATED_DISK, 100, DISK_TYPE);
323+
Disk disk = Util.getRegionalDisk(PROJECT_ID, REGION, REGIONAL_REPLICATED_DISK);
318324

319325
assertThat(status).isEqualTo(Status.DONE);
326+
assertEquals(REGIONAL_REPLICATED_DISK, disk.getName());
320327
}
321328

322329
@Test
@@ -327,7 +334,23 @@ public void testCreateDiskSecondaryRegional()
327334
Status status = CreateDiskSecondaryRegional.createDiskSecondaryRegional(
328335
PROJECT_ID, PROJECT_ID, REGIONAL_BLANK_DISK, SECONDARY_REGIONAL_DISK,
329336
REGION, "us-central1", DISK_SIZE, diskType);
337+
Disk disk = Util.getRegionalDisk(PROJECT_ID, "us-central1", SECONDARY_REGIONAL_DISK);
338+
339+
assertThat(status).isEqualTo(Status.DONE);
340+
assertEquals(SECONDARY_REGIONAL_DISK, disk.getName());
341+
}
342+
343+
@Test
344+
public void testCreateDiskSecondaryZonal()
345+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
346+
String diskType = String.format(
347+
"projects/%s/zones/%s/diskTypes/pd-ssd", PROJECT_ID, ZONE);
348+
Status status = CreateDiskSecondaryZonal.createDiskSecondaryZonal(
349+
PROJECT_ID, PROJECT_ID, EMPTY_DISK_NAME, SECONDARY_DISK, ZONE,
350+
"us-central1-c", DISK_SIZE, diskType);
351+
Disk disk = Util.getDisk(PROJECT_ID, "us-central1-c", SECONDARY_DISK);
330352

331353
assertThat(status).isEqualTo(Status.DONE);
354+
assertEquals(SECONDARY_DISK, disk.getName());
332355
}
333356
}

0 commit comments

Comments
 (0)