Skip to content

Commit 4b66ae8

Browse files
Implemented compute_disk_regional_replicated sample, created test
1 parent bcecf8f commit 4b66ae8

File tree

4 files changed

+124
-7
lines changed

4 files changed

+124
-7
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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_regional_replicated]
20+
import com.google.cloud.compute.v1.Disk;
21+
import com.google.cloud.compute.v1.InsertRegionDiskRequest;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.RegionDisksClient;
24+
import java.io.IOException;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import java.util.concurrent.ExecutionException;
28+
import java.util.concurrent.TimeUnit;
29+
import java.util.concurrent.TimeoutException;
30+
31+
public class CreateReplicatedDisk {
32+
33+
public static void main(String[] args)
34+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
// Project ID or project number of the Cloud project you want to use.
37+
String projectId = "YOUR_PROJECT_ID";
38+
// The region for the replicated disk to reside in.
39+
// The disk must be in the same region as the VM that you plan to attach it to.
40+
String region = "us-central1";
41+
// The zones within the region where the two disk replicas are located
42+
List<String> replicaZones = new ArrayList<>();
43+
replicaZones.add(String.format("projects/%s/zones/%s", projectId, "us-central1-a"));
44+
replicaZones.add(String.format("projects/%s/zones/%s", projectId, "us-central1-b"));
45+
// Name of the disk you want to create.
46+
String diskName = "YOUR_DISK_NAME";
47+
// Size of the new disk in gigabytes.
48+
int diskSizeGb = 200;
49+
// The type of replicated disk. This value uses the following format:
50+
// "regions/{region}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
51+
// For example: "regions/us-west3/diskTypes/pd-ssd"
52+
String diskType = String.format("regions/%s/diskTypes/%s", region, "pd-standard");
53+
54+
createReplicatedDisk(projectId, region, replicaZones, diskName, diskSizeGb, diskType);
55+
}
56+
57+
// Create a disk for synchronous data replication between two zones in the same region
58+
public static Disk createReplicatedDisk(String projectId, String region,
59+
List<String> replicaZones, String diskName, int diskSizeGb, String diskType)
60+
throws IOException, InterruptedException, ExecutionException, 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 (RegionDisksClient regionDisksClient = RegionDisksClient.create()) {
64+
Disk disk = Disk.newBuilder()
65+
.setSizeGb(diskSizeGb)
66+
.setName(diskName)
67+
.setType(diskType)
68+
.addAllReplicaZones(replicaZones)
69+
.build();
70+
71+
InsertRegionDiskRequest insertRegionDiskRequest = InsertRegionDiskRequest.newBuilder()
72+
.setProject(projectId)
73+
.setRegion(region)
74+
.setDiskResource(disk)
75+
.build();
76+
77+
Operation response = regionDisksClient.insertAsync(insertRegionDiskRequest)
78+
.get(3, TimeUnit.MINUTES);
79+
80+
if (response.hasError()) {
81+
System.out.println("Failed to create regional replicated disk.");
82+
return null;
83+
}
84+
return regionDisksClient.get(projectId, region, diskName);
85+
}
86+
}
87+
}
88+
// [END compute_disk_regional_replicated]

compute/cloud-client/src/test/java/compute/Util.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import compute.deleteprotection.SetDeleteProtection;
3838
import compute.disks.DeleteDisk;
3939
import compute.disks.DeleteSnapshot;
40+
import compute.disks.RegionalDelete;
4041
import compute.reservation.DeleteReservation;
4142
import java.io.IOException;
4243
import java.nio.charset.StandardCharsets;
@@ -217,6 +218,22 @@ && isCreatedBeforeThresholdTime(disk.getCreationTimestamp())) {
217218
}
218219
}
219220

221+
// Delete regional disks which starts with the given prefixToDelete and
222+
// has creation timestamp >24 hours.
223+
public static void cleanUpExistingRegionalDisks(
224+
String prefixToDelete, String projectId, String region)
225+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
226+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
227+
for (Disk disk : disksClient.list(projectId, region).iterateAll()) {
228+
if (disk.getName().contains(prefixToDelete)
229+
&& disk.getRegion().equals(region)
230+
&& isCreatedBeforeThresholdTime(disk.getCreationTimestamp())) {
231+
RegionalDelete.deleteRegionalDisk(projectId, region, disk.getName());
232+
}
233+
}
234+
}
235+
}
236+
220237
// Delete snapshots which starts with the given prefixToDelete and
221238
// has creation timestamp >24 hours.
222239
public static void cleanUpExistingSnapshots(String prefixToDelete, String projectId)

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ public class DisksIT {
6969
private static String EMPTY_DISK_NAME;
7070
private static String SNAPSHOT_NAME;
7171
private static String DISK_TYPE;
72-
7372
private static String ZONAL_BLANK_DISK;
74-
7573
private static String REGIONAL_BLANK_DISK;
76-
74+
private static String REGIONAL_REPLICATED_DISK;
75+
static List<String> replicaZones = Arrays.asList(
76+
String.format("projects/%s/zones/%s-a", PROJECT_ID, REGION),
77+
String.format("projects/%s/zones/%s-b", PROJECT_ID, REGION));
7778
private ByteArrayOutputStream stdOut;
7879

7980
// Check if the required environment variables are set.
@@ -101,12 +102,13 @@ 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+
REGIONAL_REPLICATED_DISK = "gcloud-test-disk-replicated-" + uuid;
104106

105107
// Cleanup existing stale instances.
106108
Util.cleanUpExistingInstances("test-disks", PROJECT_ID, ZONE);
107109
Util.cleanUpExistingDisks("gcloud-test-", PROJECT_ID, ZONE);
108110
Util.cleanUpExistingSnapshots("gcloud-test-snapshot-", PROJECT_ID);
109-
111+
Util.cleanUpExistingRegionalDisks("gcloud-test-disk-", PROJECT_ID, REGION);
110112
// Create disk from image.
111113
Image debianImage = null;
112114
try (ImagesClient imagesClient = ImagesClient.create()) {
@@ -245,9 +247,7 @@ public static void createZonalDisk()
245247
public static void createRegionalDisk()
246248
throws IOException, ExecutionException, InterruptedException, TimeoutException {
247249
String diskType = String.format("regions/%s/diskTypes/pd-balanced", REGION);
248-
List<String> replicaZones = Arrays.asList(
249-
String.format("projects/%s/zones/%s-a", PROJECT_ID, REGION),
250-
String.format("projects/%s/zones/%s-b", PROJECT_ID, REGION));
250+
251251
RegionalCreateFromSource.createRegionalDisk(PROJECT_ID, REGION, replicaZones,
252252
REGIONAL_BLANK_DISK, diskType, 11, Optional.empty(), Optional.empty());
253253
}
@@ -301,4 +301,14 @@ public void testDiskAttachResize()
301301
Util.getRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK).getSizeGb());
302302
}
303303

304+
@Test
305+
public void testCreateReplicatedDisk()
306+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
307+
Disk disk = CreateReplicatedDisk.createReplicatedDisk(PROJECT_ID, REGION, replicaZones,
308+
REGIONAL_REPLICATED_DISK, 100, DISK_TYPE);
309+
310+
assertThat(disk).isNotNull();
311+
assertThat(disk.getName()).isEqualTo(REGIONAL_REPLICATED_DISK);
312+
assertThat(disk.getReplicaZonesList()).hasSize(2);
313+
}
304314
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public static void setUp()
6262
throws IOException, ExecutionException, InterruptedException, TimeoutException {
6363
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
6464
requireEnvVar("GOOGLE_CLOUD_PROJECT");
65+
Util.cleanUpExistingDisks("test-hyperdisk-enc-", PROJECT_ID, ZONE);
66+
Util.cleanUpExistingStoragePool("test-storage-pool-enc-", PROJECT_ID, ZONE);
6567
}
6668

6769
@AfterAll

0 commit comments

Comments
 (0)