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+
117package compute .disks .consistencygroup ;
218
319// [START compute_consistency_group_add_disk]
20+ import com .google .cloud .compute .v1 .AddResourcePoliciesRegionDiskRequest ;
421import com .google .cloud .compute .v1 .Disk ;
5- import com .google .cloud .compute .v1 .DisksAddResourcePoliciesRequest ;
6- import com .google .cloud .compute .v1 .DisksClient ;
22+ // If your disk has zonal location uncomment these lines
23+ //import com.google.cloud.compute.v1.AddResourcePoliciesDiskRequest;
24+ //import com.google.cloud.compute.v1.DisksAddResourcePoliciesRequest;
25+ //import com.google.cloud.compute.v1.DisksClient;
726import com .google .cloud .compute .v1 .Operation ;
827import com .google .cloud .compute .v1 .RegionDisksAddResourcePoliciesRequest ;
928import com .google .cloud .compute .v1 .RegionDisksClient ;
@@ -16,54 +35,59 @@ public class AddDiskToConsistencyGroup {
1635 public static void main (String [] args )
1736 throws IOException , ExecutionException , InterruptedException {
1837 // TODO(developer): Replace these variables before running the sample.
19- String project = "tyaho-softserve-project" ;//"YOUR_PROJECT_ID";
20- String locationFlag = "zone" ;
21- String location = "us-central1-a" ;
22- String diskName = "disk-name" ;//"DISK_NAME";
23- String consistencyGroup = "my-group" ;//"CONSISTENCY_GROUP";
24-
25- addResourcePoliciesToDisk (project , locationFlag , location , diskName , consistencyGroup );
38+ // The project that contains the disk.
39+ String project = "YOUR_PROJECT_ID" ;
40+ // The zone or region of the disk.
41+ String location = "us-central1" ;
42+ // The name of the disk.
43+ String diskName = "DISK_NAME" ;
44+ // The name of the consistency group.
45+ String consistencyGroupName = "CONSISTENCY_GROUP" ;
46+ // The region of the consistency group.
47+ String consistencyGroupLocation = "us-central1" ;
48+ addDiskToConsistencyGroup (
49+ project , location , diskName , consistencyGroupName , consistencyGroupLocation );
2650 }
2751
28- // Adds a resource policy to a disk .
29- public static Disk addResourcePoliciesToDisk (
30- String project , String locationFlag , String location , String diskName ,
31- String consistencyGroup )
52+ // Adds a disk to a Consistent Group .
53+ public static Disk addDiskToConsistencyGroup (
54+ String project , String location , String diskName ,
55+ String consistencyGroupName , String consistencyGroupLocation )
3256 throws IOException , ExecutionException , InterruptedException {
3357 String consistencyGroupUrl = String .format (
3458 "https://www.googleapis.com/compute/v1/projects/%s/regions/%s/resourcePolicies/%s" ,
35- project , location , consistencyGroup );
36- if (locationFlag .equals ("zone" )) {
37- try (DisksClient disksClient = DisksClient .create ()) {
38- DisksAddResourcePoliciesRequest disksRequest =
39- DisksAddResourcePoliciesRequest .newBuilder ()
40- .addAllResourcePolicies (Arrays .asList (consistencyGroupUrl ))
41- .build ();
59+ project , consistencyGroupLocation , consistencyGroupName );
60+ // If your disk has zonal location uncomment these lines
61+ // try (DisksClient disksClient = DisksClient.create()) {
62+ // AddResourcePoliciesDiskRequest request =
63+ // AddResourcePoliciesDiskRequest.newBuilder()
64+ // .setDisk(diskName)
65+ // .setDisksAddResourcePoliciesRequestResource(
66+ // DisksAddResourcePoliciesRequest.newBuilder()
67+ // .addAllResourcePolicies(Arrays.asList(consistencyGroupUrl))
68+ // .build())
69+ // .setProject(project)
70+ // .setZone(location)
71+ // .build();
4272
43- Operation response = disksClient .addResourcePoliciesAsync (
44- project , location , diskName ,disksRequest ).get ();
45- if (response .hasError ()) {
46- return null ;
47- }
48- return disksClient .get (project , location , diskName );
49- }
50- } else if (locationFlag .equals ("region" )) {
51- try (RegionDisksClient regionDisksClient = RegionDisksClient .create ()) {
52- RegionDisksAddResourcePoliciesRequest disksRequest =
53- RegionDisksAddResourcePoliciesRequest .newBuilder ()
73+ try (RegionDisksClient disksClient = RegionDisksClient .create ()) {
74+ AddResourcePoliciesRegionDiskRequest disksRequest =
75+ AddResourcePoliciesRegionDiskRequest .newBuilder ()
76+ .setDisk (diskName )
77+ .setRegion (location )
78+ .setProject (project )
79+ .setRegionDisksAddResourcePoliciesRequestResource (
80+ RegionDisksAddResourcePoliciesRequest .newBuilder ()
5481 .addAllResourcePolicies (Arrays .asList (consistencyGroupUrl ))
55- .build ();
56- Operation response = regionDisksClient .addResourcePoliciesAsync (
57- project , location , diskName ,disksRequest ).get ();
58- if (response .hasError ()) {
59- return null ;
60- }
61- return regionDisksClient .get (project , location , diskName );
82+ .build ())
83+ .build ();
84+
85+ Operation response = disksClient .addResourcePoliciesAsync (disksRequest ).get ();
86+ if (response .hasError ()) {
87+ return null ;
6288 }
63- } else {
64- System .out .println ("Invalid location flag. Use 'zone' or 'region'." );
89+ return disksClient .get (project , location , diskName );
6590 }
66- return null ;
6791 }
6892}
6993// [END compute_consistency_group_add_disk]
0 commit comments