1717package 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;
2023import com .google .cloud .compute .v1 .Disk ;
2124import com .google .cloud .compute .v1 .ListRegionDisksRequest ;
2225import com .google .cloud .compute .v1 .RegionDisksClient ;
23- // If your disk has zonal location uncomment these lines
24- //import com.google.cloud.compute.v1.ListDisksRequest;
2526import java .io .IOException ;
2627import java .util .ArrayList ;
2728import java .util .List ;
29+ import java .util .concurrent .ExecutionException ;
2830
2931public 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]
0 commit comments