Skip to content

Commit 0b5e7fd

Browse files
feat(compute): add compute consistency group remove disk (#9682)
* Implemented compute_consistency_group_create and compute_consistency_group_delete samples, created test * Implemented compute_consistency_group_add_disk sample * Implemented compute_consistency_group_add_disk sample, created test * Implemented compute_consistency_group_remove_disk sample, created test * Fixed code, created test with mocked client * Increased timeout * Fixed test * Fixed code * Increased timeout * Created test for Hyperdisk with mocked client * Fixed tests * Fixed comments * Fixed naming
1 parent f82460a commit 0b5e7fd

File tree

8 files changed

+691
-141
lines changed

8 files changed

+691
-141
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
17+
package compute.disks.consistencygroup;
18+
19+
// [START compute_consistency_group_add_disk]
20+
import com.google.cloud.compute.v1.AddResourcePoliciesDiskRequest;
21+
import com.google.cloud.compute.v1.AddResourcePoliciesRegionDiskRequest;
22+
import com.google.cloud.compute.v1.DisksAddResourcePoliciesRequest;
23+
import com.google.cloud.compute.v1.DisksClient;
24+
import com.google.cloud.compute.v1.Operation;
25+
import com.google.cloud.compute.v1.RegionDisksAddResourcePoliciesRequest;
26+
import com.google.cloud.compute.v1.RegionDisksClient;
27+
import java.io.IOException;
28+
import java.util.Arrays;
29+
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.TimeoutException;
32+
33+
public class AddDiskToConsistencyGroup {
34+
public static void main(String[] args)
35+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
// Project ID or project number of the Cloud project that contains the disk.
38+
String project = "YOUR_PROJECT_ID";
39+
// Zone or region of the disk.
40+
String location = "us-central1";
41+
// Name of the disk.
42+
String diskName = "DISK_NAME";
43+
// Name of the consistency group.
44+
String consistencyGroupName = "CONSISTENCY_GROUP";
45+
// Region of the consistency group.
46+
String consistencyGroupLocation = "us-central1";
47+
48+
addDiskToConsistencyGroup(
49+
project, location, diskName, consistencyGroupName, consistencyGroupLocation);
50+
}
51+
52+
// Adds a disk to a consistency group.
53+
public static Operation.Status addDiskToConsistencyGroup(
54+
String project, String location, String diskName,
55+
String consistencyGroupName, String consistencyGroupLocation)
56+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
57+
String consistencyGroupUrl = String.format(
58+
"https://www.googleapis.com/compute/v1/projects/%s/regions/%s/resourcePolicies/%s",
59+
project, consistencyGroupLocation, consistencyGroupName);
60+
Operation response;
61+
if (Character.isDigit(location.charAt(location.length() - 1))) {
62+
// Initialize client that will be used to send requests. This client only needs to be created
63+
// once, and can be reused for multiple requests.
64+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
65+
AddResourcePoliciesRegionDiskRequest request =
66+
AddResourcePoliciesRegionDiskRequest.newBuilder()
67+
.setDisk(diskName)
68+
.setRegion(location)
69+
.setProject(project)
70+
.setRegionDisksAddResourcePoliciesRequestResource(
71+
RegionDisksAddResourcePoliciesRequest.newBuilder()
72+
.addAllResourcePolicies(Arrays.asList(consistencyGroupUrl))
73+
.build())
74+
.build();
75+
response = disksClient.addResourcePoliciesAsync(request).get(1, TimeUnit.MINUTES);
76+
}
77+
} else {
78+
try (DisksClient disksClient = DisksClient.create()) {
79+
AddResourcePoliciesDiskRequest request =
80+
AddResourcePoliciesDiskRequest.newBuilder()
81+
.setDisk(diskName)
82+
.setZone(location)
83+
.setProject(project)
84+
.setDisksAddResourcePoliciesRequestResource(
85+
DisksAddResourcePoliciesRequest.newBuilder()
86+
.addAllResourcePolicies(Arrays.asList(consistencyGroupUrl))
87+
.build())
88+
.build();
89+
response = disksClient.addResourcePoliciesAsync(request).get(1, TimeUnit.MINUTES);
90+
}
91+
}
92+
if (response.hasError()) {
93+
throw new Error("Error adding disk to consistency group! " + response.getError());
94+
}
95+
return response.getStatus();
96+
}
97+
}
98+
// [END compute_consistency_group_add_disk]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
17+
package compute.disks.consistencygroup;
18+
19+
// [START compute_consistency_group_create]
20+
import com.google.cloud.compute.v1.InsertResourcePolicyRequest;
21+
import com.google.cloud.compute.v1.Operation;
22+
import com.google.cloud.compute.v1.ResourcePoliciesClient;
23+
import com.google.cloud.compute.v1.ResourcePolicy;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.TimeoutException;
28+
29+
public class CreateConsistencyGroup {
30+
31+
public static void main(String[] args)
32+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
// Project ID or project number of the Cloud project you want to use.
35+
String project = "YOUR_PROJECT_ID";
36+
// Name of the region in which you want to create the consistency group.
37+
String region = "us-central1";
38+
// Name of the consistency group you want to create.
39+
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";
40+
41+
createConsistencyGroup(project, region, consistencyGroupName);
42+
}
43+
44+
// Creates a new consistency group resource policy in the specified project and region.
45+
public static Operation.Status createConsistencyGroup(
46+
String project, String region, String consistencyGroupName)
47+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
48+
// Initialize client that will be used to send requests. This client only needs to be created
49+
// once, and can be reused for multiple requests.
50+
try (ResourcePoliciesClient regionResourcePoliciesClient = ResourcePoliciesClient.create()) {
51+
ResourcePolicy resourcePolicy =
52+
ResourcePolicy.newBuilder()
53+
.setName(consistencyGroupName)
54+
.setRegion(region)
55+
.setDiskConsistencyGroupPolicy(
56+
ResourcePolicy.newBuilder().getDiskConsistencyGroupPolicy())
57+
.build();
58+
59+
InsertResourcePolicyRequest request = InsertResourcePolicyRequest.newBuilder()
60+
.setProject(project)
61+
.setRegion(region)
62+
.setResourcePolicyResource(resourcePolicy)
63+
.build();
64+
65+
Operation response =
66+
regionResourcePoliciesClient.insertAsync(request).get(1, TimeUnit.MINUTES);
67+
68+
if (response.hasError()) {
69+
throw new Error("Error creating consistency group! " + response.getError());
70+
}
71+
return response.getStatus();
72+
}
73+
}
74+
}
75+
// [END compute_consistency_group_create]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
17+
package compute.disks.consistencygroup;
18+
19+
// [START compute_consistency_group_delete]
20+
import com.google.cloud.compute.v1.Operation;
21+
import com.google.cloud.compute.v1.ResourcePoliciesClient;
22+
import java.io.IOException;
23+
import java.util.concurrent.ExecutionException;
24+
import java.util.concurrent.TimeUnit;
25+
import java.util.concurrent.TimeoutException;
26+
27+
public class DeleteConsistencyGroup {
28+
29+
public static void main(String[] args)
30+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
// Project ID or project number of the Cloud project you want to use.
33+
String project = "YOUR_PROJECT_ID";
34+
// Region in which your consistency group is located.
35+
String region = "us-central1";
36+
// Name of the consistency group you want to delete.
37+
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";
38+
39+
deleteConsistencyGroup(project, region, consistencyGroupName);
40+
}
41+
42+
// Deletes a consistency group resource policy in the specified project and region.
43+
public static Operation.Status deleteConsistencyGroup(
44+
String project, String region, String consistencyGroupName)
45+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests.
48+
try (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) {
49+
Operation response = resourcePoliciesClient
50+
.deleteAsync(project, region, consistencyGroupName).get(1, TimeUnit.MINUTES);
51+
52+
if (response.hasError()) {
53+
throw new Error("Error deleting disk! " + response.getError());
54+
}
55+
return response.getStatus();
56+
}
57+
}
58+
}
59+
// [END compute_consistency_group_delete]
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
17+
package compute.disks.consistencygroup;
18+
19+
// [START compute_consistency_group_remove_disk]
20+
import com.google.cloud.compute.v1.DisksClient;
21+
import com.google.cloud.compute.v1.DisksRemoveResourcePoliciesRequest;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.RegionDisksClient;
24+
import com.google.cloud.compute.v1.RegionDisksRemoveResourcePoliciesRequest;
25+
import com.google.cloud.compute.v1.RemoveResourcePoliciesDiskRequest;
26+
import com.google.cloud.compute.v1.RemoveResourcePoliciesRegionDiskRequest;
27+
import java.io.IOException;
28+
import java.util.Arrays;
29+
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.TimeoutException;
32+
33+
public class RemoveDiskFromConsistencyGroup {
34+
35+
public static void main(String[] args)
36+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
// Project ID or project number of the Cloud project that contains the disk.
39+
String project = "YOUR_PROJECT_ID";
40+
// Zone or region of the disk.
41+
String location = "us-central1";
42+
// Name of the disk.
43+
String diskName = "DISK_NAME";
44+
// Name of the consistency group.
45+
String consistencyGroupName = "CONSISTENCY_GROUP";
46+
// Region of the consistency group.
47+
String consistencyGroupLocation = "us-central1";
48+
49+
removeDiskFromConsistencyGroup(
50+
project, location, diskName, consistencyGroupName, consistencyGroupLocation);
51+
}
52+
53+
// Removes a disk from a consistency group.
54+
public static Operation.Status removeDiskFromConsistencyGroup(
55+
String project, String location, String diskName,
56+
String consistencyGroupName, String consistencyGroupLocation)
57+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
58+
String consistencyGroupUrl = String.format(
59+
"https://www.googleapis.com/compute/v1/projects/%s/regions/%s/resourcePolicies/%s",
60+
project, consistencyGroupLocation, consistencyGroupName);
61+
Operation response;
62+
if (Character.isDigit(location.charAt(location.length() - 1))) {
63+
// Initialize client that will be used to send requests. This client only needs to be created
64+
// once, and can be reused for multiple requests.
65+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
66+
RemoveResourcePoliciesRegionDiskRequest request =
67+
RemoveResourcePoliciesRegionDiskRequest.newBuilder()
68+
.setDisk(diskName)
69+
.setRegion(location)
70+
.setProject(project)
71+
.setRegionDisksRemoveResourcePoliciesRequestResource(
72+
RegionDisksRemoveResourcePoliciesRequest.newBuilder()
73+
.addAllResourcePolicies(Arrays.asList(consistencyGroupUrl))
74+
.build())
75+
.build();
76+
77+
response = disksClient.removeResourcePoliciesAsync(request).get(1, TimeUnit.MINUTES);
78+
}
79+
} else {
80+
try (DisksClient disksClient = DisksClient.create()) {
81+
RemoveResourcePoliciesDiskRequest request =
82+
RemoveResourcePoliciesDiskRequest.newBuilder()
83+
.setDisk(diskName)
84+
.setZone(location)
85+
.setProject(project)
86+
.setDisksRemoveResourcePoliciesRequestResource(
87+
DisksRemoveResourcePoliciesRequest.newBuilder()
88+
.addAllResourcePolicies(Arrays.asList(consistencyGroupUrl))
89+
.build())
90+
.build();
91+
response = disksClient.removeResourcePoliciesAsync(request).get(1, TimeUnit.MINUTES);
92+
}
93+
}
94+
if (response.hasError()) {
95+
throw new Error("Error removing disk from consistency group! " + response.getError());
96+
}
97+
return response.getStatus();
98+
}
99+
}
100+
// [END compute_consistency_group_remove_disk]

0 commit comments

Comments
 (0)