Skip to content

Commit 594de00

Browse files
Implemented compute_consistency_group_create and compute_consistency_group_delete samples, created test
1 parent 55d96d4 commit 594de00

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.Operation;
21+
import com.google.cloud.compute.v1.ResourcePoliciesClient;
22+
import com.google.cloud.compute.v1.ResourcePolicy;
23+
import java.io.IOException;
24+
import java.util.concurrent.ExecutionException;
25+
26+
public class CreateDiskConsistencyGroup {
27+
28+
public static void main(String[] args)
29+
throws IOException, ExecutionException, InterruptedException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
// Project ID or project number of the Cloud project you want to use.
32+
String project = "YOUR_PROJECT_ID";
33+
// Name of the region in which you want to create the consistency group.
34+
String region = "us-central1";
35+
// Name of the consistency group you want to create.
36+
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";
37+
38+
createDiskConsistencyGroup(project, region, consistencyGroupName);
39+
}
40+
41+
// Creates a new disk consistency group resource policy in the specified project and region.
42+
// Return a link to the consistency group.
43+
public static String createDiskConsistencyGroup(
44+
String project, String region, String consistencyGroupName)
45+
throws IOException, ExecutionException, InterruptedException {
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 regionResourcePoliciesClient = ResourcePoliciesClient.create()) {
49+
ResourcePolicy resourcePolicy =
50+
ResourcePolicy.newBuilder()
51+
.setName(consistencyGroupName)
52+
.setRegion(region)
53+
.setDiskConsistencyGroupPolicy(
54+
ResourcePolicy.newBuilder().getDiskConsistencyGroupPolicy())
55+
.build();
56+
57+
Operation response =
58+
regionResourcePoliciesClient.insertAsync(project, region, resourcePolicy).get();
59+
60+
if (response.hasError()) {
61+
return null;
62+
}
63+
return regionResourcePoliciesClient.get(project, region, consistencyGroupName).getSelfLink();
64+
}
65+
}
66+
}
67+
// [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+
25+
public class DeleteDiskConsistencyGroup {
26+
27+
public static void main(String[] args)
28+
throws IOException, ExecutionException, InterruptedException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
// Project ID or project number of the Cloud project you want to use.
31+
String project = "YOUR_PROJECT_ID";
32+
// Name of the region in which your consistency group is located.
33+
String region = "us-central1";
34+
// Name of the consistency group you want to delete.
35+
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";
36+
37+
deleteDiskConsistencyGroup(project, region, consistencyGroupName);
38+
}
39+
40+
// Deletes a disk consistency group resource policy in the specified project and region.
41+
public static void deleteDiskConsistencyGroup(
42+
String project, String region, String consistencyGroupName)
43+
throws IOException, ExecutionException, InterruptedException {
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests.
46+
try (ResourcePoliciesClient regionResourcePoliciesClient = ResourcePoliciesClient.create()) {
47+
Operation response = regionResourcePoliciesClient
48+
.deleteAsync(project, region, consistencyGroupName).get();
49+
50+
if (response.hasError()) {
51+
return;
52+
}
53+
System.out.println(
54+
"Disk consistency group resource policy deleted successfully: "
55+
+ consistencyGroupName);
56+
}
57+
}
58+
}
59+
// [END compute_consistency_group_delete]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.truth.Truth.assertWithMessage;
21+
import static org.junit.Assert.assertNotNull;
22+
23+
import compute.disks.consistencygroup.CreateDiskConsistencyGroup;
24+
import compute.disks.consistencygroup.DeleteDiskConsistencyGroup;
25+
import java.io.IOException;
26+
import java.util.UUID;
27+
import java.util.concurrent.ExecutionException;
28+
import java.util.concurrent.TimeUnit;
29+
import org.junit.jupiter.api.AfterAll;
30+
import org.junit.jupiter.api.BeforeAll;
31+
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.api.Timeout;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.JUnit4;
35+
36+
@RunWith(JUnit4.class)
37+
@Timeout(value = 3, unit = TimeUnit.MINUTES)
38+
public class ConsistencyGroupIT {
39+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
40+
private static final String REGION = "us-central1";
41+
private static final String CONSISTENCY_GROUP_NAME =
42+
"test-consistency-group-" + UUID.randomUUID();
43+
44+
// Check if the required environment variables are set.
45+
public static void requireEnvVar(String envVarName) {
46+
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
47+
.that(System.getenv(envVarName)).isNotEmpty();
48+
}
49+
50+
@BeforeAll
51+
public static void setUp() throws Exception {
52+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
53+
}
54+
55+
@AfterAll
56+
public static void cleanUp()
57+
throws IOException, ExecutionException, InterruptedException {
58+
// Delete created consistency group
59+
DeleteDiskConsistencyGroup.deleteDiskConsistencyGroup(
60+
PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME);
61+
}
62+
63+
@Test
64+
public void testCreateDiskConsistencyGroupResourcePolicy()
65+
throws IOException, ExecutionException, InterruptedException {
66+
String consistencyGroupLink = CreateDiskConsistencyGroup.createDiskConsistencyGroup(
67+
PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME);
68+
69+
// Verify that the consistency group was created
70+
assertNotNull(consistencyGroupLink);
71+
assertThat(consistencyGroupLink.contains(CONSISTENCY_GROUP_NAME));
72+
}
73+
}

0 commit comments

Comments
 (0)