Skip to content

Commit 874b877

Browse files
Implemented compute_disk_create_secondary_custom sample, created test
1 parent 55d96d4 commit 874b877

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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_custom]
20+
import com.google.cloud.compute.v1.Disk;
21+
import com.google.cloud.compute.v1.DisksClient;
22+
import com.google.cloud.compute.v1.GuestOsFeature;
23+
import com.google.cloud.compute.v1.Operation;
24+
import java.io.IOException;
25+
import java.util.Arrays;
26+
import java.util.HashMap;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.TimeoutException;
32+
33+
public class CreateDiskSecondaryCustom {
34+
public static void main(String[] args)
35+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
// The project that contains the primary disk.
38+
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";
41+
// Name of the disk you want to create.
42+
String secondaryDiskName = "SECONDARY_DISK_NAME";
43+
// Size of the new disk in gigabytes.
44+
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";
47+
// The type of the disk you want to create. This value uses the following format:
48+
// "projects/{projectId}/zones/{zone}/diskTypes/
49+
// (pd-standard|pd-ssd|pd-balanced|pd-extreme)".
50+
String diskType = String.format(
51+
"projects/%s/zones/%s/diskTypes/pd-balanced", projectId, disksZone);
52+
53+
createDiskSecondaryCustom(projectId, secondaryDiskName, disksZone,
54+
diskSizeGb, primaryDiskName, diskType);
55+
}
56+
57+
// 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)
60+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
61+
// Initialize client that will be used to send requests. This client only needs to be created
62+
// once, and can be reused for multiple requests.
63+
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");
76+
77+
// Create the disk object with the source disk information.
78+
Disk disk = Disk.newBuilder()
79+
.setName(secondaryDiskName)
80+
.setSizeGb(diskSizeGb)
81+
.setType(diskType)
82+
.setZone(disksZone)
83+
.addAllGuestOsFeatures(guestOsFeatures)
84+
.putAllLabels(labels)
85+
.setSourceDisk(primaryDiskSource)
86+
.build();
87+
88+
// Wait for the create disk operation to complete.
89+
Operation response = disksClient.insertAsync(projectId, disksZone, disk)
90+
.get(3, TimeUnit.MINUTES);
91+
92+
if (response.hasError()) {
93+
return null;
94+
}
95+
return disksClient.get(projectId, disksZone, secondaryDiskName);
96+
}
97+
}
98+
}
99+
// [END compute_disk_create_secondary_custom]

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

Lines changed: 23 additions & 2 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.assertNotNull;
2122

2223
import com.google.cloud.compute.v1.AttachedDisk;
2324
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
@@ -69,10 +70,10 @@ public class DisksIT {
6970
private static String EMPTY_DISK_NAME;
7071
private static String SNAPSHOT_NAME;
7172
private static String DISK_TYPE;
72-
7373
private static String ZONAL_BLANK_DISK;
74-
7574
private static String REGIONAL_BLANK_DISK;
75+
private static String SECONDARY_DISK_CUSTOM_NAME;
76+
private static final long DISK_SIZE = 10;
7677

7778
private ByteArrayOutputStream stdOut;
7879

@@ -101,6 +102,7 @@ public static void setup()
101102
DISK_TYPE = String.format("zones/%s/diskTypes/pd-ssd", ZONE);
102103
ZONAL_BLANK_DISK = "gcloud-test-disk-zattach-" + uuid;
103104
REGIONAL_BLANK_DISK = "gcloud-test-disk-rattach-" + uuid;
105+
SECONDARY_DISK_CUSTOM_NAME = "gcloud-test-disk-custom-" + uuid;
104106

105107
// Cleanup existing stale instances.
106108
Util.cleanUpExistingInstances("test-disks", PROJECT_ID, ZONE);
@@ -170,6 +172,7 @@ public static void cleanUp()
170172
DeleteDisk.deleteDisk(PROJECT_ID, ZONE, EMPTY_DISK_NAME);
171173
DeleteDisk.deleteDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK);
172174
RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK);
175+
DeleteDisk.deleteDisk(PROJECT_ID, ZONE, SECONDARY_DISK_CUSTOM_NAME);
173176

174177
stdOut.close();
175178
System.setOut(out);
@@ -301,4 +304,22 @@ public void testDiskAttachResize()
301304
Util.getRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK).getSizeGb());
302305
}
303306

307+
@Test
308+
public void testCreateDiskSecondaryCustom()
309+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
310+
String diskType = String.format(
311+
"projects/%s/zones/%s/diskTypes/pd-ssd", PROJECT_ID, ZONE);
312+
Disk disk = CreateDiskSecondaryCustom.createDiskSecondaryCustom(
313+
PROJECT_ID, SECONDARY_DISK_CUSTOM_NAME, ZONE,
314+
DISK_SIZE, EMPTY_DISK_NAME, diskType);
315+
316+
// Verify that the secondary disk was created.
317+
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));
322+
assertThat(disk.getLabelsMap().get("secondary-disk-for-replication")).isEqualTo("yes");
323+
assertThat(disk.getGuestOsFeaturesCount()).isEqualTo(3);
324+
}
304325
}

0 commit comments

Comments
 (0)