'How to get list of all compute disks of GCP for all the regions using java sdk
I am able to fetch list of disks by using gcloud command
gcloud compute disks list
But I am not able to do the same thing using java sdk. Here is my sample code:
DiskList execute = compute.disks().list("ProjectID", "us-ZoneId")
.execute();
In the above code it is always expecting the zone as a input parameter. I have used "aggregatedList" API also but no luck.
Is there anyway to fetch all the disks on the project using java sdk?
Solution 1:[1]
Here a code sample that worked for me
DisksClient disksClient = DisksClient.create();
DisksClient.AggregatedListPagedResponse aggregatedListPagedResponse = disksClient.aggregatedList("ProjectID");
for (Map.Entry<String, DisksScopedList> entry : aggregatedListPagedResponse.iterateAll()) {
System.out.println(entry.getKey());
for (Disk disk:entry.getValue().getDisksList()){
System.out.println(disk);
}
}
EDIT 1
To filter on a specific label that have any value (here label test), you can do that
DisksClient disksClient = DisksClient.create();
AggregatedListDisksRequest request = AggregatedListDisksRequest.newBuilder().setFilter("labels.test:*").setProject("ProjectID").build();
DisksClient.AggregatedListPagedResponse aggregatedListPagedResponse = disksClient.aggregatedList(request);
for (Map.Entry<String, DisksScopedList> entry : aggregatedListPagedResponse.iterateAll()) {
System.out.println(entry.getKey());
for (Disk disk:entry.getValue().getDisksList()){
System.out.println(disk);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
