Skip to content

Commit 206512e

Browse files
feat(compute): add compute instance attach regional disk force sample (GoogleCloudPlatform#9730)
* Implemented compute_instance_attach_regional_disk_force sample, created test * Added clean up method * Fixed comments and parameters * Test order deleted * Fixed code * Fixed code * Fixed code * Increased timeout * Increased timeout * Increased timeout * Fixed code * Fixed code * Fixed code * Fixed naming
1 parent 1e9647e commit 206512e

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
// [START compute_instance_attach_regional_disk_force]
20+
import com.google.cloud.compute.v1.AttachDiskInstanceRequest;
21+
import com.google.cloud.compute.v1.AttachedDisk;
22+
import com.google.cloud.compute.v1.InstancesClient;
23+
import com.google.cloud.compute.v1.Operation;
24+
import com.google.cloud.compute.v1.Operation.Status;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.TimeoutException;
29+
30+
public class AttachRegionalDiskForce {
31+
public static void main(String[] args)
32+
throws IOException, InterruptedException, ExecutionException, 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 projectId = "YOUR_PROJECT_ID";
36+
// Name of the zone of your compute instance.
37+
String zone = "us-central1-a";
38+
// The name of the compute instance where you are adding the replicated disk.
39+
String instanceName = "YOUR_INSTANCE_NAME";
40+
// The region where your replicated disk is located.
41+
String region = "us-central1";
42+
// The name of the replicated disk.
43+
String diskName = "YOUR_DISK_NAME";
44+
45+
attachRegionalDiskForce(projectId, zone, instanceName, region, diskName);
46+
}
47+
48+
// Attaches a regional disk to the instance,
49+
// forcing the attachment even if other VMs are using the disk.
50+
public static Status attachRegionalDiskForce(String projectId,
51+
String zone, String instanceName, String region, String diskName)
52+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
53+
String diskLink = String.format("projects/%s/regions/%s/disks/%s",
54+
projectId, region, diskName);
55+
// Initialize client that will be used to send requests. This client only needs to be created
56+
// once, and can be reused for multiple requests.
57+
try (InstancesClient instancesClient = InstancesClient.create()) {
58+
AttachedDisk attachedDisk = AttachedDisk.newBuilder()
59+
.setSource(diskLink)
60+
.setMode(AttachedDisk.Mode.READ_WRITE.toString())
61+
.build();
62+
63+
AttachDiskInstanceRequest attachDiskRequest = AttachDiskInstanceRequest.newBuilder()
64+
.setProject(projectId)
65+
.setZone(zone)
66+
.setInstance(instanceName)
67+
.setAttachedDiskResource(attachedDisk)
68+
.setForceAttach(true) // Force the attachment
69+
.build();
70+
71+
Operation response = instancesClient.attachDiskAsync(attachDiskRequest)
72+
.get(3, TimeUnit.MINUTES);
73+
74+
if (response.hasError()) {
75+
throw new Error("Error attaching regional disk! " + response);
76+
}
77+
return response.getStatus();
78+
}
79+
}
80+
}
81+
// [END compute_instance_attach_regional_disk_force]
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;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.ArgumentMatchers.anyLong;
22+
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.mockStatic;
24+
import static org.mockito.Mockito.times;
25+
import static org.mockito.Mockito.verify;
26+
import static org.mockito.Mockito.when;
27+
28+
import com.google.api.gax.longrunning.OperationFuture;
29+
import com.google.cloud.compute.v1.AttachDiskInstanceRequest;
30+
import com.google.cloud.compute.v1.InstancesClient;
31+
import com.google.cloud.compute.v1.Operation;
32+
import com.google.cloud.compute.v1.Operation.Status;
33+
import java.io.IOException;
34+
import java.util.concurrent.ExecutionException;
35+
import java.util.concurrent.TimeUnit;
36+
import java.util.concurrent.TimeoutException;
37+
import org.junit.jupiter.api.Test;
38+
import org.junit.jupiter.api.Timeout;
39+
import org.junit.runner.RunWith;
40+
import org.junit.runners.JUnit4;
41+
import org.mockito.MockedStatic;
42+
43+
@RunWith(JUnit4.class)
44+
@Timeout(value = 4, unit = TimeUnit.MINUTES)
45+
public class InstanceAttachDiskIT {
46+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
47+
private static final String ZONE = "us-west1-a";
48+
private static final String REGION = "us-west1";
49+
private static final String ATTACHED_DISK = "disk-regional";
50+
private static final String INSTANCE_NAME = "instance";
51+
52+
@Test
53+
public void testAttachRegionalDiskForceAttach()
54+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
55+
try (MockedStatic<InstancesClient> mockedResourcePoliciesClient =
56+
mockStatic(InstancesClient.class)) {
57+
Operation operation = mock(Operation.class);
58+
InstancesClient mockClient = mock(InstancesClient.class);
59+
OperationFuture mockFuture = mock(OperationFuture.class);
60+
61+
mockedResourcePoliciesClient.when(InstancesClient::create).thenReturn(mockClient);
62+
when(mockClient.attachDiskAsync(any(AttachDiskInstanceRequest.class)))
63+
.thenReturn(mockFuture);
64+
when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation);
65+
when(operation.getStatus()).thenReturn(Status.DONE);
66+
67+
Status status = AttachRegionalDiskForce
68+
.attachRegionalDiskForce(PROJECT_ID, ZONE, INSTANCE_NAME, REGION, ATTACHED_DISK);
69+
70+
verify(mockClient, times(1)).attachDiskAsync(any(AttachDiskInstanceRequest.class));
71+
verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class));
72+
assertEquals(Status.DONE, status);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)