Skip to content

Commit a1b2dfe

Browse files
Implemented compute_snapshot_schedule_remove sample, created test
1 parent 07c0c57 commit a1b2dfe

File tree

5 files changed

+103
-15
lines changed

5 files changed

+103
-15
lines changed

compute/cloud-client/src/main/java/compute/snapshot/AttachSnapshotScheduleToDisk.java renamed to compute/cloud-client/src/main/java/compute/snapshotschedule/AttachSnapshotScheduleToDisk.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package compute.snapshot;
17+
package compute.snapshotschedule;
1818

1919
// [START compute_snapshot_schedule_attach]
2020
import com.google.cloud.compute.v1.AddResourcePoliciesDiskRequest;

compute/cloud-client/src/main/java/compute/snapshot/CreateSnapshotSchedule.java renamed to compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package compute.snapshot;
17+
package compute.snapshotschedule;
1818

1919
// [START compute_snapshot_schedule_create]
2020
import com.google.cloud.compute.v1.Operation;
@@ -39,7 +39,7 @@ public static void main(String[] args)
3939
// Name of the region in which you want to create the snapshot schedule.
4040
String region = "us-central1";
4141
// Name of the snapshot schedule you want to create.
42-
String scheduleName = "YOUR_SCHEDULE_NAME";
42+
String snapshotScheduleName = "YOUR_SCHEDULE_NAME";
4343
// Description of the snapshot schedule.
4444
String scheduleDescription = "YOUR_SCHEDULE_DESCRIPTION";
4545
// Maximum number of days to retain snapshots.
@@ -51,13 +51,13 @@ public static void main(String[] args)
5151
// Determines what happens to your snapshots if the source disk is deleted.
5252
String onSourceDiskDelete = "KEEP_AUTO_SNAPSHOTS";
5353

54-
createSnapshotSchedule(projectId, region, scheduleName, scheduleDescription, maxRetentionDays,
55-
storageLocation, onSourceDiskDelete);
54+
createSnapshotSchedule(projectId, region, snapshotScheduleName, scheduleDescription,
55+
maxRetentionDays, storageLocation, onSourceDiskDelete);
5656
}
5757

5858
// Creates a snapshot schedule policy.
5959
public static Operation.Status createSnapshotSchedule(String projectId, String region,
60-
String scheduleName, String scheduleDescription, int maxRetentionDays,
60+
String snapshotScheduleName, String scheduleDescription, int maxRetentionDays,
6161
String storageLocation, String onSourceDiskDelete)
6262
throws IOException, ExecutionException, InterruptedException, TimeoutException {
6363
String startTime = "08:00";
@@ -112,7 +112,7 @@ public static Operation.Status createSnapshotSchedule(String projectId, String r
112112
.build();
113113

114114
ResourcePolicy resourcePolicy = ResourcePolicy.newBuilder()
115-
.setName(scheduleName)
115+
.setName(snapshotScheduleName)
116116
.setDescription(scheduleDescription)
117117
.setSnapshotSchedulePolicy(snapshotSchedulePolicy)
118118
.build();

compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java renamed to compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package compute.snapshot;
17+
package compute.snapshotschedule;
1818

1919
// [START compute_snapshot_schedule_delete]
2020
import com.google.cloud.compute.v1.Operation;
@@ -33,19 +33,20 @@ public static void main(String[] args)
3333
// Name of the region where your snapshot schedule is located.
3434
String region = "us-central1";
3535
// Name of the snapshot schedule you want to delete.
36-
String scheduleName = "YOUR_SCHEDULE_NAME";
36+
String snapshotScheduleName = "YOUR_SCHEDULE_NAME";
3737

38-
deleteSnapshotSchedule(projectId, region, scheduleName);
38+
deleteSnapshotSchedule(projectId, region, snapshotScheduleName);
3939
}
4040

4141
// Deletes a snapshot schedule policy.
4242
public static Operation.Status deleteSnapshotSchedule(
43-
String projectId, String region, String scheduleName)
43+
String projectId, String region, String snapshotScheduleName)
4444
throws IOException, ExecutionException, InterruptedException, TimeoutException {
4545
// Initialize client that will be used to send requests. This client only needs to be created
4646
// once, and can be reused for multiple requests.
4747
try (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) {
48-
Operation response = resourcePoliciesClient.deleteAsync(projectId, region, scheduleName)
48+
Operation response = resourcePoliciesClient
49+
.deleteAsync(projectId, region, snapshotScheduleName)
4950
.get(3, TimeUnit.MINUTES);
5051

5152
if (response.hasError()) {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.RemoveResourcePoliciesDiskRequest;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.TimeoutException;
28+
29+
public class RemoveSnapshotScheduleFromDisk {
30+
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 remove the snapshot schedule from.
38+
String diskName = "YOUR_DISK_NAME";
39+
// Name of the region where your snapshot schedule is located.
40+
String region = "us-central1";
41+
// Name of the snapshot schedule you want to attach.
42+
String snapshotScheduleName = "YOUR_SNAPSHOT_SCHEDULE_NAME";
43+
44+
removeSnapshotScheduleFromDisk(projectId, zone, diskName, region, snapshotScheduleName);
45+
}
46+
47+
// Removes snapshot schedule from a zonal disk.
48+
public static Operation.Status removeSnapshotScheduleFromDisk(
49+
String project, String zone, String diskName, String region, String snapshotScheduleName)
50+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
51+
String snapshotSchedulePath = String.format("projects/%s/regions/%s/resourcePolicies/%s",
52+
project, region, snapshotScheduleName);
53+
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+
DisksRemoveResourcePoliciesRequest disksRequest =
58+
DisksRemoveResourcePoliciesRequest.newBuilder()
59+
.addResourcePolicies(snapshotSchedulePath)
60+
.build();
61+
62+
RemoveResourcePoliciesDiskRequest request =
63+
RemoveResourcePoliciesDiskRequest.newBuilder()
64+
.setDisk(diskName)
65+
.setProject(project)
66+
.setDisksRemoveResourcePoliciesRequestResource(disksRequest)
67+
.setZone(zone)
68+
.build();
69+
70+
Operation response = disksClient.removeResourcePoliciesAsync(request)
71+
.get(3, TimeUnit.MINUTES);
72+
73+
if (response.hasError()) {
74+
throw new Error("Failed to remove resource policies from disk!" + response.getError());
75+
}
76+
return response.getStatus();
77+
}
78+
}
79+
}
80+
// [END compute_snapshot_schedule_remove]

compute/cloud-client/src/test/java/compute/snapshotschedule/SnapshotScheduleIT.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
import com.google.cloud.compute.v1.Operation;
2323
import compute.disks.CreateEmptyDisk;
2424
import compute.disks.DeleteDisk;
25-
import compute.snapshot.AttachSnapshotScheduleToDisk;
26-
import compute.snapshot.CreateSnapshotSchedule;
27-
import compute.snapshot.DeleteSnapshotSchedule;
2825
import java.io.IOException;
2926
import java.util.UUID;
3027
import java.util.concurrent.ExecutionException;
@@ -105,6 +102,16 @@ public void testAttachSnapshotScheduleToDisk()
105102

106103
@Test
107104
@Order(3)
105+
public void testRemoveSnapshotScheduleFromDisk()
106+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
107+
Operation.Status status = RemoveSnapshotScheduleFromDisk.removeSnapshotScheduleFromDisk(
108+
PROJECT_ID, ZONE, DISK_NAME, REGION, SCHEDULE_FOR_DISK);
109+
110+
assertThat(status).isEqualTo(Operation.Status.DONE);
111+
}
112+
113+
@Test
114+
@Order(4)
108115
public void testDeleteSnapshotSchedule()
109116
throws IOException, ExecutionException, InterruptedException, TimeoutException {
110117
Operation.Status status = DeleteSnapshotSchedule

0 commit comments

Comments
 (0)