Skip to content

Commit

Permalink
feat(compute): attach/ remove snapshot schedule to disk (#9791)
Browse files Browse the repository at this point in the history
* Implemented compute_snapshot_schedule_attach sample, created test

* Implemented compute_snapshot_schedule_remove sample, created test

* Fixed code

* Fixed code as requested in the comments
  • Loading branch information
TetyanaYahodska authored Dec 30, 2024
1 parent c917ab6 commit db76fdc
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.snapshotschedule;

// [START compute_snapshot_schedule_attach]
import com.google.cloud.compute.v1.AddResourcePoliciesDiskRequest;
import com.google.cloud.compute.v1.DisksAddResourcePoliciesRequest;
import com.google.cloud.compute.v1.DisksClient;
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 AttachSnapshotScheduleToDisk {
public static void main(String[] args) throws Exception {
// 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 where your disk is located.
String zone = "us-central1-a";
// Name of the disk you want to attach the snapshot schedule to.
String diskName = "YOUR_DISK_NAME";
// Name of the snapshot schedule you want to attach.
String snapshotScheduleName = "YOUR_SNAPSHOT_SCHEDULE_NAME";
// Name of the region where your snapshot schedule is located.
String region = "us-central1";

attachSnapshotScheduleToDisk(projectId, zone, diskName, snapshotScheduleName, region);
}

// Attaches a snapshot schedule to a disk.
public static Status attachSnapshotScheduleToDisk(
String projectId, String zone, String diskName, String snapshotScheduleName, String region)
throws IOException, ExecutionException, InterruptedException, TimeoutException {

String resourcePolicyLink = String.format(
"projects/%s/regions/%s/resourcePolicies/%s", projectId, region, snapshotScheduleName);
// 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 (DisksClient disksClient = DisksClient.create()) {

AddResourcePoliciesDiskRequest request = AddResourcePoliciesDiskRequest.newBuilder()
.setProject(projectId)
.setZone(zone)
.setDisk(diskName)
.setDisksAddResourcePoliciesRequestResource(
DisksAddResourcePoliciesRequest.newBuilder()
.addResourcePolicies(resourcePolicyLink)
.build())
.build();

Operation response = disksClient.addResourcePoliciesAsync(request).get(3, TimeUnit.MINUTES);

if (response.hasError()) {
throw new Error("Error attaching snapshot schedule to disk: " + response.getError());
}
return response.getStatus();
}
}
}
// [END compute_snapshot_schedule_attach]
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.snapshotschedule;

// [START compute_snapshot_schedule_remove]
import com.google.cloud.compute.v1.DisksClient;
import com.google.cloud.compute.v1.DisksRemoveResourcePoliciesRequest;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import com.google.cloud.compute.v1.RemoveResourcePoliciesDiskRequest;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class RemoveSnapshotScheduleFromDisk {

public static void main(String[] args) throws Exception {
// 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 where your disk is located.
String zone = "us-central1-a";
// Name of the disk you want to remove the snapshot schedule from.
String diskName = "YOUR_DISK_NAME";
// Name of the region where your snapshot schedule is located.
String region = "us-central1";
// Name of the snapshot schedule you want to remove.
String snapshotScheduleName = "YOUR_SNAPSHOT_SCHEDULE_NAME";

removeSnapshotScheduleFromDisk(projectId, zone, diskName, region, snapshotScheduleName);
}

// Removes snapshot schedule from a zonal disk.
public static Status removeSnapshotScheduleFromDisk(
String project, String zone, String diskName, String region, String snapshotScheduleName)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
String snapshotSchedulePath = String.format("projects/%s/regions/%s/resourcePolicies/%s",
project, region, snapshotScheduleName);

// 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 (DisksClient disksClient = DisksClient.create()) {
DisksRemoveResourcePoliciesRequest disksRequest =
DisksRemoveResourcePoliciesRequest.newBuilder()
.addResourcePolicies(snapshotSchedulePath)
.build();

RemoveResourcePoliciesDiskRequest request =
RemoveResourcePoliciesDiskRequest.newBuilder()
.setProject(project)
.setZone(zone)
.setDisk(diskName)
.setDisksRemoveResourcePoliciesRequestResource(disksRequest)
.build();

Operation response = disksClient.removeResourcePoliciesAsync(request)
.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
throw new Error("Failed to remove resource policies from disk!" + response.getError());
}
return response.getStatus();
}
}
}
// [END compute_snapshot_schedule_remove]
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.snapshotschedule;

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.AddResourcePoliciesDiskRequest;
import com.google.cloud.compute.v1.DisksClient;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import com.google.cloud.compute.v1.RemoveResourcePoliciesDiskRequest;
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 = 6, unit = TimeUnit.MINUTES)
public class SnapshotScheduleIT {
private static final String PROJECT_ID = "GOOGLE_CLOUD_PROJECT";
private static final String ZONE = "asia-south1-a";
private static final String REGION = ZONE.substring(0, ZONE.lastIndexOf('-'));
private static final String SCHEDULE_FOR_DISK = "test-schedule-for-disk";
private static final String DISK_NAME = "gcloud-test-disk";

@Test
public void testAttachSnapshotScheduleToDisk()
throws IOException, ExecutionException, InterruptedException, TimeoutException {
try (MockedStatic<DisksClient> mockedDisksClient = mockStatic(DisksClient.class)) {
DisksClient mockClient = mock(DisksClient.class);
Operation operation = mock(Operation.class);
OperationFuture mockFuture = mock(OperationFuture.class);

mockedDisksClient.when(DisksClient::create).thenReturn(mockClient);
when(mockClient.addResourcePoliciesAsync(any(AddResourcePoliciesDiskRequest.class)))
.thenReturn(mockFuture);
when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation);
when(operation.getStatus()).thenReturn(Status.DONE);

Status actualStatus = AttachSnapshotScheduleToDisk.attachSnapshotScheduleToDisk(
PROJECT_ID, ZONE, DISK_NAME, SCHEDULE_FOR_DISK, REGION);

verify(mockClient, times(1)).addResourcePoliciesAsync(any());
assertEquals(Status.DONE, actualStatus);
}
}

@Test
public void testRemoveSnapshotScheduleFromDisk()
throws IOException, ExecutionException, InterruptedException, TimeoutException {
try (MockedStatic<DisksClient> mockedDisksClient = mockStatic(DisksClient.class)) {
DisksClient mockClient = mock(DisksClient.class);
Operation operation = mock(Operation.class);
OperationFuture mockFuture = mock(OperationFuture.class);

mockedDisksClient.when(DisksClient::create).thenReturn(mockClient);
when(mockClient.removeResourcePoliciesAsync(any(RemoveResourcePoliciesDiskRequest.class)))
.thenReturn(mockFuture);
when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation);
when(operation.getStatus()).thenReturn(Status.DONE);

Status actualStatus = RemoveSnapshotScheduleFromDisk.removeSnapshotScheduleFromDisk(
PROJECT_ID, ZONE, DISK_NAME, REGION, SCHEDULE_FOR_DISK);

verify(mockClient, times(1)).removeResourcePoliciesAsync(any());
assertEquals(Status.DONE, actualStatus);
}
}
}

0 comments on commit db76fdc

Please sign in to comment.