-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(compute): add compute instance attach regional disk force sample #9730
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
Merged
TetyanaYahodska
merged 19 commits into
main
from
compute_instance_attach_regional_disk_force
Dec 30, 2024
+156
−0
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f65d479
Implemented compute_instance_attach_regional_disk_force sample, creat…
TetyanaYahodska e4bc6b3
Added clean up method
TetyanaYahodska b34ae5a
Merge branch 'main' into compute_instance_attach_regional_disk_force
TetyanaYahodska 366d833
Fixed comments and parameters
TetyanaYahodska 5ff07e0
Test order deleted
TetyanaYahodska b59f02a
Resolved conflict
TetyanaYahodska bffce81
Fixed code
TetyanaYahodska bf4f299
Fixed code
TetyanaYahodska d4fc570
Fixed code
TetyanaYahodska 9fe1590
Fixed code
TetyanaYahodska e00d791
Merge branch 'main' into compute_instance_attach_regional_disk_force
TetyanaYahodska 1c0453d
Increased timeout
TetyanaYahodska 7c6ed3c
Increased timeout
TetyanaYahodska 8b55b92
Increased timeout
TetyanaYahodska 95f153e
Fixed code
TetyanaYahodska 10de584
Fixed code
TetyanaYahodska 2eee7b5
Fixed code
TetyanaYahodska 5d8e366
Merge branch 'main' into compute_instance_attach_regional_disk_force
TetyanaYahodska 3db9964
Fixed naming
TetyanaYahodska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
compute/cloud-client/src/main/java/compute/disks/AttachRegionalDiskForce.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| // [START compute_instance_attach_regional_disk_force] | ||
| import com.google.cloud.compute.v1.AttachDiskInstanceRequest; | ||
| import com.google.cloud.compute.v1.AttachedDisk; | ||
| import com.google.cloud.compute.v1.InstancesClient; | ||
| import com.google.cloud.compute.v1.Operation; | ||
| import com.google.cloud.compute.v1.Operation.Status; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
|
|
||
| public class AttachRegionalDiskForce { | ||
| public static void main(String[] args) | ||
| throws IOException, InterruptedException, ExecutionException, TimeoutException { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| // Project ID or project number of the Cloud project you want to use. | ||
| String projectId = "YOUR_PROJECT_ID"; | ||
| // Name of the zone of your compute instance. | ||
| String instanceLocation = "us-central1-a"; | ||
| // The name of the compute instance where you are adding the replicated disk. | ||
| String instanceName = "YOUR_INSTANCE_NAME"; | ||
| // The region where your replicated disk is located. | ||
| String diskLocation = "us-central1"; | ||
| // The name of the replicated disk. | ||
| String diskName = "YOUR_DISK_NAME"; | ||
|
|
||
| attachRegionalDiskForce(projectId, instanceLocation, instanceName, diskLocation, diskName); | ||
| } | ||
|
|
||
| // Attaches a regional disk to the instance, | ||
| // forcing the attachment even if other VMs are using the disk. | ||
| public static Status attachRegionalDiskForce(String projectId, | ||
| String instanceLocation, String instanceName, String diskLocation, String diskName) | ||
| throws IOException, InterruptedException, ExecutionException, TimeoutException { | ||
| String diskLink = String.format("projects/%s/regions/%s/disks/%s", | ||
| projectId, diskLocation, diskName); | ||
| // 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 (InstancesClient instancesClient = InstancesClient.create()) { | ||
| AttachedDisk attachedDisk = AttachedDisk.newBuilder() | ||
| .setSource(diskLink) | ||
| .setMode(AttachedDisk.Mode.READ_WRITE.toString()) | ||
| .build(); | ||
|
|
||
| AttachDiskInstanceRequest attachDiskRequest = AttachDiskInstanceRequest.newBuilder() | ||
| .setProject(projectId) | ||
| .setZone(instanceLocation) | ||
| .setInstance(instanceName) | ||
| .setAttachedDiskResource(attachedDisk) | ||
| .setForceAttach(true) // Force the attachment | ||
| .build(); | ||
|
|
||
| Operation response = instancesClient.attachDiskAsync(attachDiskRequest) | ||
| .get(3, TimeUnit.MINUTES); | ||
|
|
||
| if (response.hasError()) { | ||
| throw new Error("Error attaching regional disk! " + response); | ||
| } | ||
| return response.getStatus(); | ||
| } | ||
| } | ||
| } | ||
| // [END compute_instance_attach_regional_disk_force] | ||
75 changes: 75 additions & 0 deletions
75
compute/cloud-client/src/test/java/compute/disks/InstanceAttachDiskIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * 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 org.junit.Assert.assertEquals; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyLong; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.google.api.gax.longrunning.OperationFuture; | ||
| import com.google.cloud.compute.v1.AttachDiskInstanceRequest; | ||
| import com.google.cloud.compute.v1.InstancesClient; | ||
| import com.google.cloud.compute.v1.Operation; | ||
| import com.google.cloud.compute.v1.Operation.Status; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.Timeout; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
| import org.mockito.MockedStatic; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| @Timeout(value = 4, unit = TimeUnit.MINUTES) | ||
| public class InstanceAttachDiskIT { | ||
| private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
| private static final String ZONE = "us-west1-a"; | ||
| private static final String REGION = "us-west1"; | ||
| private static final String ATTACHED_DISK = "disk-regional"; | ||
| private static final String INSTANCE_NAME = "instance"; | ||
|
|
||
| @Test | ||
| public void testAttachRegionalDiskForceAttach() | ||
| throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
| try (MockedStatic<InstancesClient> mockedResourcePoliciesClient = | ||
| mockStatic(InstancesClient.class)) { | ||
| Operation operation = mock(Operation.class); | ||
| InstancesClient mockClient = mock(InstancesClient.class); | ||
| OperationFuture mockFuture = mock(OperationFuture.class); | ||
|
|
||
| mockedResourcePoliciesClient.when(InstancesClient::create).thenReturn(mockClient); | ||
| when(mockClient.attachDiskAsync(any(AttachDiskInstanceRequest.class))) | ||
| .thenReturn(mockFuture); | ||
| when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation); | ||
| when(operation.getStatus()).thenReturn(Status.DONE); | ||
|
|
||
| Status status = AttachRegionalDiskForce | ||
| .attachRegionalDiskForce(PROJECT_ID, ZONE, INSTANCE_NAME, REGION, ATTACHED_DISK); | ||
|
|
||
| verify(mockClient, times(1)).attachDiskAsync(any(AttachDiskInstanceRequest.class)); | ||
| verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class)); | ||
| assertEquals(Status.DONE, status); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kindly rename the variable to
zone?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!Fixed naming