Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compute): add compute consistency group create/delete samples #9646

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package compute.disks.consistencygroup;

// [START compute_consistency_group_create]
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.ResourcePoliciesClient;
import com.google.cloud.compute.v1.ResourcePolicy;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class CreateDiskConsistencyGroup {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project you want to use.
String project = "YOUR_PROJECT_ID";
// Name of the region in which you want to create the consistency group.
String region = "us-central1";
// Name of the consistency group you want to create.
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";

createDiskConsistencyGroup(project, region, consistencyGroupName);
}

// Creates a new disk consistency group resource policy in the specified project and region.
// Return a link to the consistency group.
public static String createDiskConsistencyGroup(
String project, String region, String consistencyGroupName)
throws IOException, ExecutionException, InterruptedException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (ResourcePoliciesClient regionResourcePoliciesClient = ResourcePoliciesClient.create()) {
ResourcePolicy resourcePolicy =
ResourcePolicy.newBuilder()
.setName(consistencyGroupName)
.setRegion(region)
.setDiskConsistencyGroupPolicy(
ResourcePolicy.newBuilder().getDiskConsistencyGroupPolicy())
.build();

Operation response =
regionResourcePoliciesClient.insertAsync(project, region, resourcePolicy).get();

if (response.hasError()) {
return null;
}
return regionResourcePoliciesClient.get(project, region, consistencyGroupName).getSelfLink();
}
}
}
// [END compute_consistency_group_create]
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package compute.disks.consistencygroup;

// [START compute_consistency_group_delete]
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.ResourcePoliciesClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class DeleteDiskConsistencyGroup {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project you want to use.
String project = "YOUR_PROJECT_ID";
// Name of the region in which your consistency group is located.
String region = "us-central1";
// Name of the consistency group you want to delete.
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";

deleteDiskConsistencyGroup(project, region, consistencyGroupName);
}

// Deletes a disk consistency group resource policy in the specified project and region.
public static void deleteDiskConsistencyGroup(
String project, String region, String consistencyGroupName)
throws IOException, ExecutionException, InterruptedException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (ResourcePoliciesClient regionResourcePoliciesClient = ResourcePoliciesClient.create()) {
Operation response = regionResourcePoliciesClient
.deleteAsync(project, region, consistencyGroupName).get();

if (response.hasError()) {
return;
}
System.out.println(
"Disk consistency group resource policy deleted successfully: "
+ consistencyGroupName);
}
}
}
// [END compute_consistency_group_delete]
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package compute.disks;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.assertNotNull;

import compute.disks.consistencygroup.CreateDiskConsistencyGroup;
import compute.disks.consistencygroup.DeleteDiskConsistencyGroup;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
@Timeout(value = 3, unit = TimeUnit.MINUTES)
public class ConsistencyGroupIT {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String REGION = "us-central1";
private static final String CONSISTENCY_GROUP_NAME =
"test-consistency-group-" + UUID.randomUUID();

// Check if the required environment variables are set.
public static void requireEnvVar(String envVarName) {
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
.that(System.getenv(envVarName)).isNotEmpty();
}

@BeforeAll
public static void setUp() throws Exception {
requireEnvVar("GOOGLE_CLOUD_PROJECT");
}

@AfterAll
public static void cleanUp()
throws IOException, ExecutionException, InterruptedException {
// Delete created consistency group
DeleteDiskConsistencyGroup.deleteDiskConsistencyGroup(
PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME);
}

@Test
public void testCreateDiskConsistencyGroupResourcePolicy()
throws IOException, ExecutionException, InterruptedException {
String consistencyGroupLink = CreateDiskConsistencyGroup.createDiskConsistencyGroup(
PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME);

// Verify that the consistency group was created
assertNotNull(consistencyGroupLink);
assertThat(consistencyGroupLink.contains(CONSISTENCY_GROUP_NAME));
}
}