Skip to content

Commit db76fdc

Browse files
feat(compute): attach/ remove snapshot schedule to disk (GoogleCloudPlatform#9791)
* 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
1 parent c917ab6 commit db76fdc

File tree

3 files changed

+254
-0
lines changed

3 files changed

+254
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.snapshotschedule;
18+
19+
// [START compute_snapshot_schedule_attach]
20+
import com.google.cloud.compute.v1.AddResourcePoliciesDiskRequest;
21+
import com.google.cloud.compute.v1.DisksAddResourcePoliciesRequest;
22+
import com.google.cloud.compute.v1.DisksClient;
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 AttachSnapshotScheduleToDisk {
31+
public static void main(String[] args) throws Exception {
32+
// TODO(developer): Replace these variables before running the sample.
33+
// Project ID or project number of the Cloud project you want to use.
34+
String projectId = "YOUR_PROJECT_ID";
35+
// Name of the zone where your disk is located.
36+
String zone = "us-central1-a";
37+
// Name of the disk you want to attach the snapshot schedule to.
38+
String diskName = "YOUR_DISK_NAME";
39+
// Name of the snapshot schedule you want to attach.
40+
String snapshotScheduleName = "YOUR_SNAPSHOT_SCHEDULE_NAME";
41+
// Name of the region where your snapshot schedule is located.
42+
String region = "us-central1";
43+
44+
attachSnapshotScheduleToDisk(projectId, zone, diskName, snapshotScheduleName, region);
45+
}
46+
47+
// Attaches a snapshot schedule to a disk.
48+
public static Status attachSnapshotScheduleToDisk(
49+
String projectId, String zone, String diskName, String snapshotScheduleName, String region)
50+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
51+
52+
String resourcePolicyLink = String.format(
53+
"projects/%s/regions/%s/resourcePolicies/%s", projectId, region, snapshotScheduleName);
54+
// Initialize client that will be used to send requests. This client only needs to be created
55+
// once, and can be reused for multiple requests.
56+
try (DisksClient disksClient = DisksClient.create()) {
57+
58+
AddResourcePoliciesDiskRequest request = AddResourcePoliciesDiskRequest.newBuilder()
59+
.setProject(projectId)
60+
.setZone(zone)
61+
.setDisk(diskName)
62+
.setDisksAddResourcePoliciesRequestResource(
63+
DisksAddResourcePoliciesRequest.newBuilder()
64+
.addResourcePolicies(resourcePolicyLink)
65+
.build())
66+
.build();
67+
68+
Operation response = disksClient.addResourcePoliciesAsync(request).get(3, TimeUnit.MINUTES);
69+
70+
if (response.hasError()) {
71+
throw new Error("Error attaching snapshot schedule to disk: " + response.getError());
72+
}
73+
return response.getStatus();
74+
}
75+
}
76+
}
77+
// [END compute_snapshot_schedule_attach]
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.snapshotschedule;
18+
19+
// [START compute_snapshot_schedule_remove]
20+
import com.google.cloud.compute.v1.DisksClient;
21+
import com.google.cloud.compute.v1.DisksRemoveResourcePoliciesRequest;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.Operation.Status;
24+
import com.google.cloud.compute.v1.RemoveResourcePoliciesDiskRequest;
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 RemoveSnapshotScheduleFromDisk {
31+
32+
public static void main(String[] args) throws Exception {
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 where your disk is located.
37+
String zone = "us-central1-a";
38+
// Name of the disk you want to remove the snapshot schedule from.
39+
String diskName = "YOUR_DISK_NAME";
40+
// Name of the region where your snapshot schedule is located.
41+
String region = "us-central1";
42+
// Name of the snapshot schedule you want to remove.
43+
String snapshotScheduleName = "YOUR_SNAPSHOT_SCHEDULE_NAME";
44+
45+
removeSnapshotScheduleFromDisk(projectId, zone, diskName, region, snapshotScheduleName);
46+
}
47+
48+
// Removes snapshot schedule from a zonal disk.
49+
public static Status removeSnapshotScheduleFromDisk(
50+
String project, String zone, String diskName, String region, String snapshotScheduleName)
51+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
52+
String snapshotSchedulePath = String.format("projects/%s/regions/%s/resourcePolicies/%s",
53+
project, region, snapshotScheduleName);
54+
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 (DisksClient disksClient = DisksClient.create()) {
58+
DisksRemoveResourcePoliciesRequest disksRequest =
59+
DisksRemoveResourcePoliciesRequest.newBuilder()
60+
.addResourcePolicies(snapshotSchedulePath)
61+
.build();
62+
63+
RemoveResourcePoliciesDiskRequest request =
64+
RemoveResourcePoliciesDiskRequest.newBuilder()
65+
.setProject(project)
66+
.setZone(zone)
67+
.setDisk(diskName)
68+
.setDisksRemoveResourcePoliciesRequestResource(disksRequest)
69+
.build();
70+
71+
Operation response = disksClient.removeResourcePoliciesAsync(request)
72+
.get(3, TimeUnit.MINUTES);
73+
74+
if (response.hasError()) {
75+
throw new Error("Failed to remove resource policies from disk!" + response.getError());
76+
}
77+
return response.getStatus();
78+
}
79+
}
80+
}
81+
// [END compute_snapshot_schedule_remove]
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.snapshotschedule;
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.AddResourcePoliciesDiskRequest;
30+
import com.google.cloud.compute.v1.DisksClient;
31+
import com.google.cloud.compute.v1.Operation;
32+
import com.google.cloud.compute.v1.Operation.Status;
33+
import com.google.cloud.compute.v1.RemoveResourcePoliciesDiskRequest;
34+
import java.io.IOException;
35+
import java.util.concurrent.ExecutionException;
36+
import java.util.concurrent.TimeUnit;
37+
import java.util.concurrent.TimeoutException;
38+
import org.junit.jupiter.api.Test;
39+
import org.junit.jupiter.api.Timeout;
40+
import org.junit.runner.RunWith;
41+
import org.junit.runners.JUnit4;
42+
import org.mockito.MockedStatic;
43+
44+
@RunWith(JUnit4.class)
45+
@Timeout(value = 6, unit = TimeUnit.MINUTES)
46+
public class SnapshotScheduleIT {
47+
private static final String PROJECT_ID = "GOOGLE_CLOUD_PROJECT";
48+
private static final String ZONE = "asia-south1-a";
49+
private static final String REGION = ZONE.substring(0, ZONE.lastIndexOf('-'));
50+
private static final String SCHEDULE_FOR_DISK = "test-schedule-for-disk";
51+
private static final String DISK_NAME = "gcloud-test-disk";
52+
53+
@Test
54+
public void testAttachSnapshotScheduleToDisk()
55+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
56+
try (MockedStatic<DisksClient> mockedDisksClient = mockStatic(DisksClient.class)) {
57+
DisksClient mockClient = mock(DisksClient.class);
58+
Operation operation = mock(Operation.class);
59+
OperationFuture mockFuture = mock(OperationFuture.class);
60+
61+
mockedDisksClient.when(DisksClient::create).thenReturn(mockClient);
62+
when(mockClient.addResourcePoliciesAsync(any(AddResourcePoliciesDiskRequest.class)))
63+
.thenReturn(mockFuture);
64+
when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation);
65+
when(operation.getStatus()).thenReturn(Status.DONE);
66+
67+
Status actualStatus = AttachSnapshotScheduleToDisk.attachSnapshotScheduleToDisk(
68+
PROJECT_ID, ZONE, DISK_NAME, SCHEDULE_FOR_DISK, REGION);
69+
70+
verify(mockClient, times(1)).addResourcePoliciesAsync(any());
71+
assertEquals(Status.DONE, actualStatus);
72+
}
73+
}
74+
75+
@Test
76+
public void testRemoveSnapshotScheduleFromDisk()
77+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
78+
try (MockedStatic<DisksClient> mockedDisksClient = mockStatic(DisksClient.class)) {
79+
DisksClient mockClient = mock(DisksClient.class);
80+
Operation operation = mock(Operation.class);
81+
OperationFuture mockFuture = mock(OperationFuture.class);
82+
83+
mockedDisksClient.when(DisksClient::create).thenReturn(mockClient);
84+
when(mockClient.removeResourcePoliciesAsync(any(RemoveResourcePoliciesDiskRequest.class)))
85+
.thenReturn(mockFuture);
86+
when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation);
87+
when(operation.getStatus()).thenReturn(Status.DONE);
88+
89+
Status actualStatus = RemoveSnapshotScheduleFromDisk.removeSnapshotScheduleFromDisk(
90+
PROJECT_ID, ZONE, DISK_NAME, REGION, SCHEDULE_FOR_DISK);
91+
92+
verify(mockClient, times(1)).removeResourcePoliciesAsync(any());
93+
assertEquals(Status.DONE, actualStatus);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)