8000 feat(compute): add compute disk create secondary custom sample (#9644) · invertase/java-docs-samples@673b387 · GitHub
[go: up one dir, main page]

Skip to content

Commit 673b387

Browse files
feat(compute): add compute disk create secondary custom sample (GoogleCloudPlatform#9644)
* Implemented compute_disk_create_secondary_custom sample, created test * Fixed code * Fixed variable * Fixed code * Fixed whitespace * Fixed whitespace
1 parent 206512e commit 673b387

File tree

2 files changed

+132
-2
lines changed

2 files changed

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

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.io.ByteArrayOutputStream;
4141
import java.io.IOException;
4242
import java.io.PrintStream;
43+
import java.lang.Error;
4344
import java.util.Arrays;
4445
import java.util.List;
4546
import java.util.Optional;
@@ -79,6 +80,8 @@ public class DisksIT {
7980
private static String SECONDARY_REGIONAL_DISK;
8081
private static String SECONDARY_DISK;
8182
private static final long DISK_SIZE = 10L;
83+
private static String SECONDARY_CUSTOM_DISK;
84+
8285
private ByteArrayOutputStream stdOut;
8386

8487
// Check if the required environment variables are set.
@@ -109,6 +112,7 @@ public static void setup()
109112
REGIONAL_REPLICATED_DISK = "gcloud-test-disk-replicated-" + uuid;
110113
SECONDARY_REGIONAL_DISK = "gcloud-test-disk-secondary-regional-" + uuid;
111114
SECONDARY_DISK = "gcloud-test-disk-secondary-" + uuid;
115+
SECONDARY_CUSTOM_DISK = "gcloud-test-disk-custom-" + uuid;
112116

113117
// Cleanup existing stale resources.
114118
Util.cleanUpExistingInstances("test-disks", PROJECT_ID, ZONE);
@@ -186,6 +190,7 @@ public static void cleanUp()
186190
RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, REGIONAL_REPLICATED_DISK);
187191
RegionalDelete.deleteRegionalDisk(PROJECT_ID, "us-central1", SECONDARY_REGIONAL_DISK);
188192
DeleteDisk.deleteDisk(PROJECT_ID, "us-central1-c", SECONDARY_DISK);
193+
DeleteDisk.deleteDisk(PROJECT_ID, "us-central1-c", SECONDARY_CUSTOM_DISK);
189194

190195
stdOut.close();
191196
System.setOut(out);
@@ -343,14 +348,28 @@ public void testCreateDiskSecondaryRegional()
343348
@Test
344349
public void testCreateDiskSecondaryZonal()
345350
throws IOException, ExecutionException, InterruptedException, TimeoutException {
346-
String diskType = String.format(
351+
String diskType = String.format(
347352
"projects/%s/zones/%s/diskTypes/pd-ssd", PROJECT_ID, ZONE);
348353
Status status = CreateDiskSecondaryZonal.createDiskSecondaryZonal(
349354
PROJECT_ID, PROJECT_ID, EMPTY_DISK_NAME, SECONDARY_DISK, ZONE,
350-
"us-central1-c", DISK_SIZE, diskType);
355+
"us-central1-c", DISK_SIZE, diskType);
351356
Disk disk = Util.getDisk(PROJECT_ID, "us-central1-c", SECONDARY_DISK);
352357

353358
assertThat(status).isEqualTo(Status.DONE);
354359
assertEquals(SECONDARY_DISK, disk.getName());
355360
}
361+
362+
@Test
363+
public void testCreateSecondaryCustomDisk()
364+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
365+
String diskType = String.format(
366+
"projects/%s/zones/%s/diskTypes/pd-ssd", PROJECT_ID, ZONE);
367+
Status status = CreateSecondaryCustomDisk.createSecondaryCustomDisk(
368+
PROJECT_ID, PROJECT_ID, EMPTY_DISK_NAME, SECONDARY_CUSTOM_DISK, ZONE,
369+
"us-central1-c", DISK_SIZE, diskType);
370+
Disk disk = Util.getDisk(PROJECT_ID, "us-central1-c", SECONDARY_CUSTOM_DISK);
371+
372+
assertThat(status).isEqualTo(Status.DONE);
373+
assertEquals(SECONDARY_CUSTO 3D58 M_DISK, disk.getName());
374+
}
356375
}

0 commit comments

Comments
 (0)
0