Skip to content

Commit 825b042

Browse files
Fixed code following the comments
1 parent 71ec736 commit 825b042

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

compute/cloud-client/src/main/java/compute/disks/CreateDiskSecondaryRegional.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,16 @@ public static void main(String[] args)
4949
// Size of the new disk in gigabytes.
5050
// Learn more about disk requirements:
5151
// https://cloud.google.com/compute/docs/disks/async-pd/configure?authuser=0#disk_requirements
52-
long diskSizeGb = 30L;
53-
// The type of the disk you want to create. This value uses the following format:
54-
// "projects/{projectId}/zones/{zone}/diskTypes/
55-
// (pd-standard|pd-ssd|pd-balanced|pd-extreme)".
56-
String diskType = String.format(
57-
"projects/%s/regions/%s/diskTypes/pd-balanced", secondaryProjectId, secondaryDiskRegion);
52+
int diskSizeGb = 10;
5853

5954
createDiskSecondaryRegional(primaryProjectId, secondaryProjectId, primaryDiskName,
60-
secondaryDiskName, primaryDiskRegion, secondaryDiskRegion, diskSizeGb, diskType);
55+
secondaryDiskName, primaryDiskRegion, secondaryDiskRegion, diskSizeGb);
6156
}
6257

6358
// Creates a secondary disk in a specified region.
6459
public static Disk createDiskSecondaryRegional(String projectId, String secondaryProjectId,
6560
String primaryDiskName, String secondaryDiskName, String primaryDiskRegion,
66-
String secondaryDiskRegion, long diskSizeGb, String diskType)
61+
String secondaryDiskRegion, int diskSizeGb)
6762
throws IOException, ExecutionException, InterruptedException, TimeoutException {
6863
// An iterable collection of zone names in which you want to keep
6964
// the new disks' replicas. One of the replica zones of the clone must match
@@ -72,6 +67,9 @@ public static Disk createDiskSecondaryRegional(String projectId, String secondar
7267
String.format("projects/%s/zones/%s-c", secondaryProjectId, secondaryDiskRegion),
7368
String.format("projects/%s/zones/%s-b", secondaryProjectId, secondaryDiskRegion));
7469

70+
String diskType = String.format("projects/%s/regions/%s/diskTypes/pd-balanced",
71+
secondaryProjectId, secondaryDiskRegion);
72+
7573
String primaryDiskSource = String.format("projects/%s/regions/%s/disks/%s",
7674
projectId, primaryDiskRegion, primaryDiskName);
7775

@@ -97,6 +95,7 @@ public static Disk createDiskSecondaryRegional(String projectId, String secondar
9795
.get(3, TimeUnit.MINUTES);
9896

9997
if (response.hasError()) {
98+
System.out.println(response.getError());
10099
return null;
101100
}
102101
return disksClient.get(secondaryProjectId, secondaryDiskRegion, secondaryDiskName);

compute/cloud-client/src/main/java/compute/disks/StartDiskReplication.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,24 @@ public static void main(String[] args)
3939
// Learn more about zones and regions:
4040
// https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs
4141
String primaryDiskLocation = "YOUR_PRIMARY_DISK_LOCATION";
42-
// Path to your secondary disk.
43-
// This value uses the following format for zonal location:
44-
// "projects/%sYOUR_PROJECT_ID/zones/YOUR_PRIMARY_DISK_LOCATION/disks/PRIMARY_DISK_NAME"
45-
String secondaryDiskPath =
46-
"projects/%s/regions/YOUR_SECONDARY_DISK_LOCATION/disks/SECONDARY_DISK_NAME";
42+
// The project that contains the secondary disk.
43+
String secondaryProjectId = "SECONDARY_PROJECT_ID";
44+
// Name of the region in which your secondary disk is located.
45+
String secondaryDiskLocation = "YOUR_SECONDARY_DISK_LOCATION";
46+
// Name of the secondary disk.
47+
String secondaryDiskName = "SECONDARY_DISK_NAME";
4748

48-
startDiskAsyncReplication(
49-
primaryProjectId, primaryDiskName, primaryDiskLocation, secondaryDiskPath);
49+
startDiskAsyncReplication(primaryProjectId, primaryDiskName,
50+
primaryDiskLocation, secondaryProjectId, secondaryDiskLocation, secondaryDiskName);
5051
}
5152

5253
// Starts asynchronous replication for the specified disk.
53-
public static void startDiskAsyncReplication(String primaryProjectId, String primaryDiskName,
54-
String primaryDiskLocation, String secondaryDiskPath)
54+
public static Operation.Status startDiskAsyncReplication(String primaryProjectId,
55+
String primaryDiskName, String primaryDiskLocation, String secondaryProjectId,
56+
String secondaryDiskLocation, String secondaryDiskName)
5557
throws IOException, ExecutionException, InterruptedException {
58+
String secondaryDiskPath = String.format("projects/%s/regions/%s/disks/%s",
59+
secondaryProjectId, secondaryDiskLocation, secondaryDiskName);
5660
// Initialize client that will be used to send requests. This client only needs to be created
5761
// once, and can be reused for multiple requests.
5862

@@ -74,9 +78,10 @@ public static void startDiskAsyncReplication(String primaryProjectId, String pri
7478
primaryProjectId, primaryDiskLocation, primaryDiskName, replicationRequest).get();
7579

7680
if (response.hasError()) {
77-
return;
81+
System.out.println(response.getError());
82+
return null;
7883
}
79-
System.out.println("Async replication started successfully.");
84+
return response.getStatus();
8085
}
8186
}
8287
}

compute/cloud-client/src/main/java/compute/disks/StopDiskReplication.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void main(String[] args)
4040
}
4141

4242
// Stops asynchronous replication for the specified disk.
43-
public static void stopDiskAsyncReplication(
43+
public static Operation.Status stopDiskAsyncReplication(
4444
String project, String diskLocation, String diskName)
4545
throws IOException, ExecutionException, InterruptedException {
4646
// Initialize client that will be used to send requests. This client only needs to be created
@@ -54,9 +54,10 @@ public static void stopDiskAsyncReplication(
5454
project, diskLocation, diskName).get();
5555

5656
if (response.hasError()) {
57-
return;
57+
System.out.println(response.getError());
58+
return null;
5859
}
59-
System.out.println("Async replication stopped successfully.");
60+
return response.getStatus();
6061
}
6162
}
6263
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class ConsistencyGroupIT {
6161
private static final String DISK_TYPE = String.format("regions/%s/diskTypes/pd-balanced", REGION);
6262
private static final String SECONDARY_REGIONAL_DISK =
6363
"gcloud-test-disk-secondary-regional-" + randomUUID;
64-
private static final long DISK_SIZE = 10L;
64+
private static final int DISK_SIZE = 10;
6565
private static final List<String> replicaZones = Arrays.asList(
6666
String.format("projects/%s/zones/%s-a", PROJECT_ID, REGION),
6767
String.format("projects/%s/zones/%s-b", PROJECT_ID, REGION));
@@ -86,7 +86,6 @@ public static void cleanUp()
8686
StopDiskReplication.stopDiskAsyncReplication(PROJECT_ID, REGION, DISK_NAME);
8787
StopDiskReplication.stopDiskAsyncReplication(
8888
PROJECT_ID, REGION_SECONDARY, SECONDARY_REGIONAL_DISK);
89-
9089
RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, DISK_NAME);
9190
RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION_SECONDARY, SECONDARY_REGIONAL_DISK);
9291
DeleteDiskConsistencyGroup.deleteDiskConsistencyGroup(
@@ -128,14 +127,13 @@ public void testCloneDisksFromConsistencyGroup()
128127
throws IOException, ExecutionException, InterruptedException, TimeoutException {
129128
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
130129
System.setOut(new PrintStream(stdOut));
131-
Disk disk = CreateDiskSecondaryRegional.createDiskSecondaryRegional(
130+
CreateDiskSecondaryRegional.createDiskSecondaryRegional(
132131
PROJECT_ID, PROJECT_ID, DISK_NAME, SECONDARY_REGIONAL_DISK,
133-
REGION, REGION_SECONDARY, DISK_SIZE, DISK_TYPE);
132+
REGION, REGION_SECONDARY, DISK_SIZE);
134133
CreateDiskConsistencyGroup.createDiskConsistencyGroup(
135134
PROJECT_ID, REGION_SECONDARY, CONSISTENCY_GROUP_SECONDARY);
136-
assert disk != null;
137135
StartDiskReplication.startDiskAsyncReplication(
138-
PROJECT_ID, DISK_NAME, REGION, disk.getSelfLink());
136+
PROJECT_ID, DISK_NAME, REGION, PROJECT_ID, REGION_SECONDARY, SECONDARY_REGIONAL_DISK);
139137
AddDiskToConsistencyGroup.addDiskToConsistencyGroup(PROJECT_ID, REGION_SECONDARY,
140138
SECONDARY_REGIONAL_DISK, CONSISTENCY_GROUP_SECONDARY, REGION_SECONDARY);
141139
TimeUnit.MINUTES.sleep(1);

0 commit comments

Comments
 (0)