Skip to content

Commit e3625e7

Browse files
Fixed code
1 parent 2bfbb82 commit e3625e7

File tree

3 files changed

+63
-45
lines changed

3 files changed

+63
-45
lines changed

compute/cloud-client/src/main/java/compute/disks/consistencygroup/ListDisksInConsistencyGroup.java

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,68 @@
1717
package compute.disks.consistencygroup;
1818

1919
// [START compute_consistency_group_list_disks]
20+
// If your disk has zonal location uncomment these lines
21+
//import com.google.cloud.compute.v1.ListDisksRequest;
22+
//import com.google.cloud.compute.v1.DisksClient;
2023
import com.google.cloud.compute.v1.Disk;
2124
import com.google.cloud.compute.v1.ListRegionDisksRequest;
2225
import com.google.cloud.compute.v1.RegionDisksClient;
23-
// If your disk has zonal location uncomment these lines
24-
//import com.google.cloud.compute.v1.ListDisksRequest;
2526
import java.io.IOException;
2627
import java.util.ArrayList;
2728
import java.util.List;
29+
import java.util.concurrent.ExecutionException;
2830

2931
public class ListDisksInConsistencyGroup {
30-
31-
public static void main(String[] args) throws IOException {
32+
public static void main(String[] args)
33+
throws IOException, InterruptedException, ExecutionException {
3234
// TODO(developer): Replace these variables before running the sample.
33-
// The project that contains the disk.
34-
String project = "tyaho-softserve-project";//"YOUR_PROJECT_ID";
35-
// The zone or region of the disk.
36-
String location = "us-central1";
35+
// Project ID or project number of the Cloud project you want to use.
36+
String project = "YOUR_PROJECT_ID";
3737
// The name of the consistency group.
38-
String consistencyGroupName = "my-group";//"CONSISTENCY_GROUP";
38+
String consistencyGroupName = "CONSISTENCY_GROUP_ID";
39+
// The zone or region of the disk.
40+
String disksLocation = "us-central1";
3941
// The region of the consistency group.
4042
String consistencyGroupLocation = "us-central1";
41-
listDisksInConsistencyGroup(project, location, consistencyGroupName, consistencyGroupLocation);
43+
44+
listDisksInConsistencyGroup(
45+
project, consistencyGroupName, consistencyGroupLocation, disksLocation);
4246
}
4347

4448
// Lists disks in a consistency group.
45-
public static List<Disk> listDisksInConsistencyGroup(String project, String location,
46-
String consistencyGroupName, String consistencyGroupLocation) throws IOException {
47-
List<Disk> diskList = new ArrayList<>();
48-
String filter = String.format("https://www.googleapis.com/compute/v1/projects/%s/regions/%s/resourcePolicies/%s",
49-
project, consistencyGroupLocation, consistencyGroupName);
50-
// https://www.googleapis/.com/compute/v1/projects/tyaho-softserve-project/regions/us-central1/resourcePolicies/my-group
51-
// If your disk has zonal location uncomment these lines
52-
// try (DisksClient disksClient = DisksClient.create()) {
53-
// ListDisksRequest request =
54-
// ListDisksRequest.newBuilder()
55-
// .setProject(project)
56-
// .setZone(location)
57-
// .build();
58-
// List<Disk> diskList = new ArrayList<>();
59-
// for (Disk disk : disksClient.list(request).iterateAll()) {
60-
// diskList.add(disk);
61-
// }
62-
// }
49+
public static List<Disk> listDisksInConsistencyGroup(String project, String consistencyGroupName,
50+
String consistencyGroupLocation, String disksLocation) throws IOException {
51+
String filter = String
52+
.format("https://www.googleapis.com/compute/v1/projects/%s/regions/%s/resourcePolicies/%s",
53+
project, consistencyGroupLocation, consistencyGroupName);
54+
List<Disk> disksList = new ArrayList<>();
55+
//If your disk has zonal location uncomment these lines
56+
//try (DisksClient disksClient = DisksClient.create()) {
57+
// ListDisksRequest request =
58+
// ListDisksRequest.newBuilder()
59+
// .setProject(project)
60+
// .setZone(disksLocation)
61+
// .build();
62+
// DisksClient.ListPagedResponse response = disksClient.list(request);
6363

6464
// Filtering must be done manually for now, since list filtering
6565
// inside disksClient.list is not supported yet.
6666
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
6767
ListRegionDisksRequest request =
68-
ListRegionDisksRequest.newBuilder()
69-
.setProject(project)
70-
.setRegion(location)
71-
.build();
72-
// RegionDisksClient.ListPagedResponse response = disksClient.list(request);
68+
ListRegionDisksRequest.newBuilder()
69+
.setProject(project)
70+
.setRegion(disksLocation)
71+
.build();
72+
73+
RegionDisksClient.ListPagedResponse response = disksClient.list(request);
7374

74-
for (Disk disk : disksClient.list(request).iterateAll()) { // Use regional disks client and request for regional disks if needed
75+
for (Disk disk : response.iterateAll()) {
7576
if (disk.getResourcePoliciesList().contains(filter)) {
76-
diskList.add(disk);
77+
disksList.add(disk);
7778
}
7879
}
79-
System.out.println(diskList);
80-
return diskList;
80+
return disksList;
8181
}
8282
}
8383
}
84-
// [END compute_consistency_group_list_disks]
84+
// [END compute_consistency_group_list_disks]

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

Lines changed: 19 additions & 2 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;
@@ -57,7 +58,7 @@ public abstract class Util {
5758
// resources
5859
// and delete the listed resources based on the timestamp.
5960

60-
private static final int DELETION_THRESHOLD_TIME_MINUTES = 30;
61+
private static final int DELETION_THRESHOLD_TIME_HOURS = 24;
6162
// comma separate list of zone names
6263
private static final String TEST_ZONES_NAME = "JAVA_DOCS_COMPUTE_TEST_ZONES";
6364
private static final String DEFAULT_ZONES = "us-central1-a,us-west1-a,asia-south1-a";
@@ -126,7 +127,7 @@ && isCreatedBeforeThresholdTime(instance.getCreationTimestamp())) {
126127

127128
public static boolean isCreatedBeforeThresholdTime(String timestamp) {
128129
return OffsetDateTime.parse(timestamp).toInstant()
129-
.isBefore(Instant.now().minus(DELETION_THRESHOLD_TIME_MINUTES, ChronoUnit.MINUTES));
130+
.isBefore(Instant.now().minus(DELETION_THRESHOLD_TIME_HOURS, ChronoUnit.HOURS));
130131
}
131132

132133
public static String getBase64EncodedKey() {
@@ -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/ConsistencyGroupIT.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertTrue;
2323

2424
import com.google.cloud.compute.v1.Disk;
25+
import compute.Util;
2526
import compute.disks.consistencygroup.AddDiskToConsistencyGroup;
2627
import compute.disks.consistencygroup.CreateDiskConsistencyGroup;
2728
import compute.disks.consistencygroup.DeleteDiskConsistencyGroup;
@@ -75,10 +76,13 @@ public static void setUp() throws Exception {
7576
@AfterAll
7677
public static void cleanUp()
7778
throws IOException, ExecutionException, InterruptedException, TimeoutException {
78-
// Delete created consistency group
79+
// Delete created resources
7980
RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, DISK_NAME);
8081
DeleteDiskConsistencyGroup.deleteDiskConsistencyGroup(
8182
PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME);
83+
84+
// Clean up stale disks
85+
Util.cleanUpExistingRegionalDisks("test-disk-for-consistency-", PROJECT_ID, REGION);
8286
}
8387

8488
@Test
@@ -88,7 +92,6 @@ public void testCreateDiskConsistencyGroupResourcePolicy()
8892
String consistencyGroupLink = CreateDiskConsistencyGroup.createDiskConsistencyGroup(
8993
PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME);
9094

91-
// Verify that the consistency group was created
9295
assertNotNull(consistencyGroupLink);
9396
assertThat(consistencyGroupLink.contains(CONSISTENCY_GROUP_NAME));
9497
}
@@ -100,7 +103,6 @@ public void testAddRegionalDiskToConsistencyGroup()
100103
Disk disk = AddDiskToConsistencyGroup.addDiskToConsistencyGroup(
101104
PROJECT_ID, REGION, DISK_NAME, CONSISTENCY_GROUP_NAME, REGION);
102105

103-
// Verify that the disk was added to the consistency group
104106
assertNotNull(disk);
105107
assertThat(disk.getResourcePoliciesList().get(0).contains(CONSISTENCY_GROUP_NAME));
106108
}
@@ -109,9 +111,8 @@ public void testAddRegionalDiskToConsistencyGroup()
109111
@Order(3)
110112
public void testListDisksInConsistencyGroup() throws IOException {
111113
List<Disk> disks = ListDisksInConsistencyGroup.listDisksInConsistencyGroup(
112-
PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME, REGION);
114+
PROJECT_ID, CONSISTENCY_GROUP_NAME, REGION, REGION);
113115

114-
// Verify that the disk list is returned
115116
assertThat(disks).isNotEmpty();
116117
assertTrue(disks.stream().anyMatch(disk -> disk.getName().equals(DISK_NAME)));
117118
}

0 commit comments

Comments
 (0)