Skip to content

Commit 8aee6bd

Browse files
Implemented compute_snapshot_schedule_edit, created test
1 parent 7df8f9d commit 8aee6bd

File tree

5 files changed

+116
-99
lines changed

5 files changed

+116
-99
lines changed

compute/cloud-client/src/main/java/compute/snapshot/CreateSnapshotSchedule.java renamed to compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.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_create]
2020
import com.google.cloud.compute.v1.Operation;

compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java renamed to compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.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_delete]
2020
import com.google.cloud.compute.v1.Operation;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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_edit]
20+
import com.google.cloud.compute.v1.Operation;
21+
import com.google.cloud.compute.v1.PatchResourcePolicyRequest;
22+
import com.google.cloud.compute.v1.ResourcePoliciesClient;
23+
import com.google.cloud.compute.v1.ResourcePolicy;
24+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy;
25+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySchedule;
26+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySnapshotProperties;
27+
import com.google.cloud.compute.v1.ResourcePolicyWeeklyCycle;
28+
import com.google.cloud.compute.v1.ResourcePolicyWeeklyCycleDayOfWeek;
29+
import java.io.IOException;
30+
import java.util.HashMap;
31+
import java.util.Map;
32+
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.TimeoutException;
35+
36+
public class EditSnapshotSchedule {
37+
38+
public static void main(String[] args) throws Exception {
39+
// TODO(developer): Replace these variables before running the sample.
40+
// Project ID or project number of the Cloud project you want to use.
41+
String projectId = "YOUR_PROJECT_ID";
42+
// Name of the region where your snapshot schedule is located.
43+
String region = "us-central1";
44+
// Name of the snapshot schedule you want to update.
45+
String snapshotScheduleName = "YOUR_SCHEDULE_NAME";
46+
47+
editSnapshotScheduleAsync(projectId, region, snapshotScheduleName);
48+
}
49+
50+
public static Operation.Status editSnapshotScheduleAsync(
51+
String projectId, String region, String snapshotScheduleName)
52+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
53+
String description = "Updated description11";
54+
Map<String, String> snapshotLabels = new HashMap<>();
55+
snapshotLabels.put("key", "value");
56+
ResourcePolicyWeeklyCycleDayOfWeek dayOfWeek = ResourcePolicyWeeklyCycleDayOfWeek.newBuilder()
57+
.setDay("Tuesday")
58+
.setStartTime("09:00")
59+
.build();
60+
ResourcePolicyWeeklyCycle weeklySchedule = ResourcePolicyWeeklyCycle.newBuilder()
61+
.addDayOfWeeks(dayOfWeek)
62+
.build();
63+
String onSourceDiskDelete = "apply-retention-policy";
64+
int maxRetentionDays = 3;
65+
66+
// Initialize client that will be used to send requests. This client only needs to be created
67+
// once, and can be reused for multiple requests.
68+
try (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) {
69+
ResourcePolicy existingSchedule = resourcePoliciesClient.get(projectId, region, snapshotScheduleName);
70+
71+
ResourcePolicySnapshotSchedulePolicySnapshotProperties.Builder snapshotProperties =
72+
existingSchedule.getSnapshotSchedulePolicy().getSnapshotProperties().toBuilder();
73+
snapshotProperties.putAllLabels(snapshotLabels);
74+
75+
ResourcePolicySnapshotSchedulePolicySchedule.Builder scheduler =
76+
existingSchedule.getSnapshotSchedulePolicy().getSchedule().toBuilder();
77+
scheduler.clearDailySchedule().clearHourlySchedule();
78+
scheduler.setWeeklySchedule(weeklySchedule);
79+
80+
ResourcePolicySnapshotSchedulePolicyRetentionPolicy.Builder retentionPolicy =
81+
existingSchedule.getSnapshotSchedulePolicy().getRetentionPolicy().toBuilder();
82+
retentionPolicy.setOnSourceDiskDelete(onSourceDiskDelete);
83+
retentionPolicy.setMaxRetentionDays(maxRetentionDays);
84+
85+
ResourcePolicy updatedSchedule = ResourcePolicy.newBuilder()
86+
.setName(existingSchedule.getName())
87+
.setDescription(description)
88+
.setSnapshotSchedulePolicy(
89+
existingSchedule.getSnapshotSchedulePolicy().toBuilder()
90+
.setSchedule(scheduler)
91+
.setSnapshotProperties(snapshotProperties)
92+
.setRetentionPolicy(retentionPolicy.build())
93+
.build())
94+
.build();
95+
96+
PatchResourcePolicyRequest request = PatchResourcePolicyRequest.newBuilder()
97+
.setProject(projectId)
98+
.setRegion(region)
99+
.setResourcePolicy(snapshotScheduleName)
100+
.setResourcePolicyResource(updatedSchedule)
101+
.build();
102+
103+
Operation response = resourcePoliciesClient.patchAsync(request).get(3, TimeUnit.MINUTES);
104+
105+
if (response.hasError()) {
106+
System.err.printf("Snapshot schedule update failed: %s%n", response.getError());
107+
return null;
108+
}
109+
return response.getStatus();
110+
}
111+
}
112+
}
113+
// [END compute_snapshot_schedule_edit]

compute/cloud-client/src/main/java/compute/snapshot/GetSnapshotSchedule.java renamed to compute/cloud-client/src/main/java/compute/snapshotschedule/GetSnapshotSchedule.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_get]
2020
import com.google.cloud.compute.v1.GetResourcePolicyRequest;

compute/cloud-client/src/test/java/compute/snapshot/SnapshotIT.java

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)