8000 feat(compute): add compute disk create secondary regional sample (#9641) · invertase/java-docs-samples@0795de2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0795de2

Browse files
feat(compute): add compute disk create secondary regional sample (GoogleCloudPlatform#9641)
* Implemented compute_disk_create_secondary_regional. created test * Fixed test * Fixed test * Fixed test * Fixed zone * Fixed naming * Fixed spaces * Fixed code * Fixed indentations * Fixed variable * Fixed code * Added cleanup methods * Fixed lint issue * Fixed lint issue * Fixed test * Fixed code * Fixed code * Fixed code * Deleted duplicated assertion
1 parent 29ccf1c commit 0795de2

File tree

2 files changed

+130
-8
lines changed

2 files changed

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

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public class DisksIT {
7676
private static final List<String> replicaZones = Arrays.asList(
7777
String.format("projects/%s/zones/%s-a", PROJECT_ID, REGION),
7878
String.format("projects/%s/zones/%s-b", PROJECT_ID, REGION));
79+
private static String SECONDARY_REGIONAL_DISK;
80+
private static final long DISK_SIZE = 10L;
7981
private ByteArrayOutputStream stdOut;
8082

8183
// Check if the required environment variables are set.
@@ -104,35 +106,39 @@ public static void setup()
104106
ZONAL_BLANK_DISK = "gcloud-test-disk-zattach-" + uuid;
105107
REGIONAL_BLANK_DISK = "gcloud-test-disk-rattach-" + uuid;
106108
REGIONAL_REPLICATED_DISK = "gcloud-test-disk-replicated-" + uuid;
109+
SECONDARY_REGIONAL_DISK = "gcloud-test-disk-secondary-regional-" + uuid;
107110

108-
// Cleanup existing stale instances.
111+
// Cleanup existing stale resources.
109112
Util.cleanUpExistingInstances("test-disks", PROJECT_ID, ZONE);
110113
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);
111116
Util.cleanUpExistingSnapshots("gcloud-test-snapshot-", PROJECT_ID);
112117
Util.cleanUpExistingRegionalDisks("gcloud-test-disk-", PROJECT_ID, REGION);
113118
// Create disk from image.
114119
Image debianImage = null;
115120
try (ImagesClient imagesClient = ImagesClient.create()) {
116121
debianImage = imagesClient.getFromFamily("debian-cloud", "debian-11");
117122
}
118-
CreateDiskFromImage.createDiskFromImage(PROJECT_ID, ZONE, DISK_NAME, DISK_TYPE, 10,
123+
CreateDiskFromImage.createDiskFromImage(PROJECT_ID, ZONE, DISK_NAME, DISK_TYPE, DISK_SIZE,
119124
debianImage.getSelfLink());
120125
assertThat(stdOut.toString()).contains("Disk created from image.");
121126

122127
// Create disk from snapshot.
123-
CreateDiskFromImage.createDiskFromImage(PROJECT_ID, ZONE, DISK_NAME_DUMMY, DISK_TYPE, 10,
128+
CreateDiskFromImage.createDiskFromImage(PROJECT_ID, ZONE, DISK_NAME_DUMMY, DISK_TYPE, DISK_SIZE,
124129
debianImage.getSelfLink());
125130
TimeUnit.SECONDS.sleep(10);
126131
createDiskSnapshot(PROJECT_ID, ZONE, DISK_NAME_DUMMY, SNAPSHOT_NAME);
127132
String diskSnapshotLink = String.format("projects/%s/global/snapshots/%s", PROJECT_ID,
128133
SNAPSHOT_NAME);
129134
TimeUnit.SECONDS.sleep(5);
130-
CreateDiskFromSnapshot.createDiskFromSnapshot(PROJECT_ID, ZONE, DISK_NAME_2, DISK_TYPE, 10,
135+
CreateDiskFromSnapshot.createDiskFromSnapshot(
136+
PROJECT_ID, ZONE, DISK_NAME_2, DISK_TYPE, DISK_SIZE,
131137
diskSnapshotLink);
132138
assertThat(stdOut.toString()).contains("Disk created.");
133139

134140
// Create empty disk.
135-
CreateEmptyDisk.createEmptyDisk(PROJECT_ID, ZONE, EMPTY_DISK_NAME, DISK_TYPE, 10);
141+
CreateEmptyDisk.createEmptyDisk(PROJECT_ID, ZONE, EMPTY_DISK_NAME, DISK_TYPE, DISK_SIZE);
136142
assertThat(stdOut.toString()).contains("Empty disk created.");
137143

138144
// Set Disk autodelete.
@@ -174,6 +180,7 @@ public static void cleanUp()
174180
DeleteDisk.deleteDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK);
175181
RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK);
176182
RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, REGIONAL_REPLICATED_DISK);
183+
RegionalDelete.deleteRegionalDisk(PROJECT_ID, "us-central1", SECONDARY_REGIONAL_DISK);
177184

178185
stdOut.close();
179186
System.setOut(out);
@@ -214,7 +221,7 @@ public static void createInstance(String projectId, String zone, String instance
214221
.setAutoDelete(false)
215222
.setBoot(true)
216223
.setInitializeParams(AttachedDiskInitializeParams.newBuilder()
217-
.setDiskSizeGb(10)
224+
.setDiskSizeGb(DISK_SIZE)
218225
.setSourceImage(sourceImage)
219226
.setDiskName(diskName)
220227
.build())
@@ -243,15 +250,15 @@ public static void createInstance(String projectId, String zone, String instance
243250
public static void createZonalDisk()
244251
throws IOException, ExecutionException, InterruptedException, TimeoutException {
245252
String diskType = String.format("zones/%s/diskTypes/pd-standard", ZONE);
246-
CreateEmptyDisk.createEmptyDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK, diskType, 12);
253+
CreateEmptyDisk.createEmptyDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK, diskType, DISK_SIZE);
247254
}
248255

249256
public static void createRegionalDisk()
250257
throws IOException, ExecutionException, InterruptedException, TimeoutException {
251258
String diskType = String.format("regions/%s/diskTypes/pd-balanced", REGION);
252259

253260
RegionalCreateFromSource.createRegionalDisk(PROJECT_ID, REGION, replicaZones,
254-
REGIONAL_BLANK_DISK, diskType, 11, Optional.empty(), Optional.empty());
261+
REGIONAL_BLANK_DISK, diskType, 10, Optional.empty(), Optional.empty());
255262
}
256263

257264
@BeforeEach
@@ -311,4 +318,16 @@ public void testCreateReplicatedDisk()
311318

312319
assertThat(status).isEqualTo(Status.DONE);
313320
}
321+
322+
@Test
323+
public void testCreateDiskSecondaryRegional()
324+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
325+
String diskType = String.format(
326+
"projects/%s/regions/%s/diskTypes/pd-balanced", PROJECT_ID, REGION);
327+
Status status = CreateDiskSecondaryRegional.createDiskSecondaryRegional(
328+
PROJECT_ID, PROJECT_ID, REGIONAL_BLANK_DISK, SECONDARY_REGIONAL_DISK,
329+
REGION, "us-central1", DISK_SIZE, diskType);
330+
331+
assertThat(status).isEqualTo(Status.DONE);
332+
}
314333
}

0 commit comments

Comments
 (0)
0