Skip to content

Commit f328675

Browse files
feat(compute): add compute snapshot schedule create/get/edit/list/delete samples (GoogleCloudPlatform#9742)
* Implemented compute_snapshot_schedule_delete and compute_snapshot_schedule_create samples, created test * Fixed test * Added compute_snapshot_schedule_get sample, created test * Fixed naming * Implemented compute_snapshot_schedule_edit, created test * Fixed naming * Implemented compute_snapshot_schedule_list sample, created test * Cleaned resources * Cleaned resources * Cleaned resources * Cleaned resources * Fixed test * Added comment * Fixed tests * Fixed code * Fixed code as requested in the comments
1 parent 673b387 commit f328675

File tree

6 files changed

+548
-5
lines changed

6 files changed

+548
-5
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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_create]
20+
import com.google.cloud.compute.v1.InsertResourcePolicyRequest;
21+
import com.google.cloud.compute.v1.Operation;
22+
import com.google.cloud.compute.v1.Operation.Status;
23+
import com.google.cloud.compute.v1.ResourcePoliciesClient;
24+
import com.google.cloud.compute.v1.ResourcePolicy;
25+
import com.google.cloud.compute.v1.ResourcePolicyHourlyCycle;
26+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicy;
27+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy;
28+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy.OnSourceDiskDelete;
29+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySchedule;
30+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySnapshotProperties;
31+
import java.io.IOException;
32+
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.TimeoutException;
35+
36+
public class CreateSnapshotSchedule {
37+
public static void main(String[] args)
38+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
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 in which you want to create the snapshot schedule.
43+
String region = "us-central1";
44+
// Name of the snapshot schedule you want to create.
45+
String snapshotScheduleName = "YOUR_SCHEDULE_NAME";
46+
// Description of the snapshot schedule.
47+
String scheduleDescription = "YOUR_SCHEDULE_DESCRIPTION";
48+
// Maximum number of days to retain snapshots.
49+
int maxRetentionDays = 10;
50+
// Storage location for the snapshots.
51+
// More about storage locations:
52+
// https://cloud.google.com/compute/docs/disks/snapshots?authuser=0#selecting_a_storage_location
53+
String storageLocation = "US";
54+
55+
createSnapshotSchedule(projectId, region, snapshotScheduleName, scheduleDescription,
56+
maxRetentionDays, storageLocation);
57+
}
58+
59+
// Creates a snapshot schedule policy.
60+
public static Status createSnapshotSchedule(String projectId, String region,
61+
String snapshotScheduleName, String scheduleDescription, int maxRetentionDays,
62+
String storageLocation)
63+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
64+
// Initialize client that will be used to send requests. This client only needs to be created
65+
// once, and can be reused for multiple requests.
66+
try (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) {
67+
int snapshotInterval = 10; // Create a snapshot every 10 hours
68+
String startTime = "08:00"; // Define the hourly schedule
69+
70+
ResourcePolicyHourlyCycle hourlyCycle = ResourcePolicyHourlyCycle.newBuilder()
71+
.setHoursInCycle(snapshotInterval)
72+
.setStartTime(startTime)
73+
.build();
74+
75+
ResourcePolicySnapshotSchedulePolicyRetentionPolicy retentionPolicy =
76+
ResourcePolicySnapshotSchedulePolicyRetentionPolicy.newBuilder()
77+
.setMaxRetentionDays(maxRetentionDays)
78+
.setOnSourceDiskDelete(OnSourceDiskDelete.KEEP_AUTO_SNAPSHOTS.toString())
79+
.build();
80+
81+
ResourcePolicySnapshotSchedulePolicySnapshotProperties snapshotProperties =
82+
ResourcePolicySnapshotSchedulePolicySnapshotProperties.newBuilder()
83+
.addStorageLocations(storageLocation)
84+
.build();
85+
86+
ResourcePolicySnapshotSchedulePolicy snapshotSchedulePolicy =
87+
ResourcePolicySnapshotSchedulePolicy.newBuilder()
88+
.setRetentionPolicy(retentionPolicy)
89+
.setSchedule(ResourcePolicySnapshotSchedulePolicySchedule.newBuilder()
90+
.setHourlySchedule(hourlyCycle)
91+
.build())
92+
.setSnapshotProperties(snapshotProperties)
93+
.build();
94+
95+
ResourcePolicy resourcePolicy = ResourcePolicy.newBuilder()
96+
.setName(snapshotScheduleName)
97+
.setDescription(scheduleDescription)
98+
.setSnapshotSchedulePolicy(snapshotSchedulePolicy)
99+
.build();
100+
InsertResourcePolicyRequest request = InsertResourcePolicyRequest.newBuilder()
101+
.setProject(projectId)
102+
.setRegion(region)
103+
.setResourcePolicyResource(resourcePolicy)
104+
.build();
105+
106+
Operation response = resourcePoliciesClient.insertAsync(request)
107+
.get(3, TimeUnit.MINUTES);
108+
109+
if (response.hasError()) {
110+
throw new Error("Snapshot schedule creation failed! " + response.getError());
111+
}
112+
return response.getStatus();
113+
}
114+
}
115+
}
116+
// [END compute_snapshot_schedule_create]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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_delete]
20+
import com.google.cloud.compute.v1.DeleteResourcePolicyRequest;
21+
import com.google.cloud.compute.v1.Operation;
22+
import com.google.cloud.compute.v1.Operation.Status;
23+
import com.google.cloud.compute.v1.ResourcePoliciesClient;
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 DeleteSnapshotSchedule {
30+
public static void main(String[] args)
31+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
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 region where your snapshot schedule is located.
36+
String region = "us-central1";
37+
// Name of the snapshot schedule you want to delete.
38+
String snapshotScheduleName = "YOUR_SCHEDULE_NAME";
39+
40+
deleteSnapshotSchedule(projectId, region, snapshotScheduleName);
41+
}
42+
43+
// Deletes a snapshot schedule policy.
44+
public static Status deleteSnapshotSchedule(
45+
String projectId, String region, String snapshotScheduleName)
46+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// once, and can be reused for multiple requests.
49+
try (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) {
50+
DeleteResourcePolicyRequest request = DeleteResourcePolicyRequest.newBuilder()
51+
.setProject(projectId)
52+
.setRegion(region)
53+
.setResourcePolicy(snapshotScheduleName)
54+
.build();
55+
Operation response = resourcePoliciesClient.deleteAsync(request).get(3, TimeUnit.MINUTES);
56+
57+
if (response.hasError()) {
58+
throw new Error("Snapshot schedule deletion failed! " + response.getError());
59+
}
60+
return response.getStatus();
61+
}
62+
}
63+
}
64+
// [END compute_snapshot_schedule_delete]
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.Operation.Status;
22+
import com.google.cloud.compute.v1.PatchResourcePolicyRequest;
23+
import com.google.cloud.compute.v1.ResourcePoliciesClient;
24+
import com.google.cloud.compute.v1.ResourcePolicy;
25+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicy;
26+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy;
27+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy.OnSourceDiskDelete;
28+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySchedule;
29+
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySnapshotProperties;
30+
import com.google.cloud.compute.v1.ResourcePolicyWeeklyCycle;
31+
import com.google.cloud.compute.v1.ResourcePolicyWeeklyCycleDayOfWeek;
32+
import java.io.IOException;
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
import java.util.concurrent.ExecutionException;
36+
import java.util.concurrent.TimeUnit;
37+
import java.util.concurrent.TimeoutException;
38+
39+
public class EditSnapshotSchedule {
40+
41+
public static void main(String[] args) throws Exception {
42+
// TODO(developer): Replace these variables before running the sample.
43+
// Project ID or project number of the Cloud project you want to use.
44+
String projectId = "YOUR_PROJECT_ID";
45+
// Name of the region where your snapshot schedule is located.
46+
String region = "us-central1";
47+
// Name of the snapshot schedule you want to update.
48+
String snapshotScheduleName = "YOUR_SCHEDULE_NAME";
49+
50+
editSnapshotSchedule(projectId, region, snapshotScheduleName);
51+
}
52+
53+
// Edits a snapshot schedule.
54+
public static Status editSnapshotSchedule(
55+
String projectId, String region, String snapshotScheduleName)
56+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
57+
58+
// Initialize client that will be used to send requests. This client only needs to be created
59+
// once, and can be reused for multiple requests.
60+
try (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) {
61+
Map<String, String> snapshotLabels = new HashMap<>();
62+
snapshotLabels.put("key", "value");
63+
64+
ResourcePolicySnapshotSchedulePolicySnapshotProperties.Builder snapshotProperties =
65+
ResourcePolicySnapshotSchedulePolicySnapshotProperties.newBuilder();
66+
snapshotProperties.putAllLabels(snapshotLabels);
67+
68+
ResourcePolicyWeeklyCycleDayOfWeek dayOfWeek = ResourcePolicyWeeklyCycleDayOfWeek.newBuilder()
69+
.setDay("Tuesday")
70+
.setStartTime("09:00")
71+
.build();
72+
ResourcePolicyWeeklyCycle weeklySchedule = ResourcePolicyWeeklyCycle.newBuilder()
73+
.addDayOfWeeks(dayOfWeek)
74+
.build();
75+
76+
int maxRetentionDays = 3;
77+
78+
ResourcePolicySnapshotSchedulePolicyRetentionPolicy.Builder retentionPolicy =
79+
ResourcePolicySnapshotSchedulePolicyRetentionPolicy.newBuilder();
80+
retentionPolicy.setOnSourceDiskDelete(OnSourceDiskDelete.APPLY_RETENTION_POLICY.toString());
81+
retentionPolicy.setMaxRetentionDays(maxRetentionDays);
82+
83+
String description = "Updated description";
84+
85+
ResourcePolicy updatedSchedule = ResourcePolicy.newBuilder()
86+
.setName(snapshotScheduleName)
87+
.setDescription(description)
88+
.setSnapshotSchedulePolicy(
89+
ResourcePolicySnapshotSchedulePolicy.newBuilder()
90+
.setSchedule(ResourcePolicySnapshotSchedulePolicySchedule.newBuilder()
91+
.setWeeklySchedule(weeklySchedule))
92+
.setSnapshotProperties(snapshotProperties)
93+
.setRetentionPolicy(retentionPolicy.build())
94+
.build())
95+
.build();
96+
97+
PatchResourcePolicyRequest request = PatchResourcePolicyRequest.newBuilder()
98+
.setProject(projectId)
99+
.setRegion(region)
100+
.setResourcePolicy(snapshotScheduleName)
101+
.setResourcePolicyResource(updatedSchedule)
102+
.build();
103+
104+
Operation response = resourcePoliciesClient.patchAsync(request).get(3, TimeUnit.MINUTES);
105+
106+
if (response.hasError()) {
107+
throw new Error("Failed to update snapshot schedule! " + response.getError());
108+
}
109+
return response.getStatus();
110+
}
111+
}
112+
}
113+
// [END compute_snapshot_schedule_edit]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_get]
20+
import com.google.cloud.compute.v1.GetResourcePolicyRequest;
21+
import com.google.cloud.compute.v1.ResourcePoliciesClient;
22+
import com.google.cloud.compute.v1.ResourcePolicy;
23+
import java.io.IOException;
24+
25+
public class GetSnapshotSchedule {
26+
27+
public static void main(String[] args) throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
// Project ID or project number of the Cloud project you want to use.
30+
String projectId = "YOUR_PROJECT_ID";
31+
// Name of the region in which your snapshot schedule is located.
32+
String region = "us-central1";
33+
// Name of your snapshot schedule.
34+
String snapshotScheduleName = "YOUR_SCHEDULE_NAME";
35+
36+
getSnapshotSchedule(projectId, region, snapshotScheduleName);
37+
}
38+
39+
// Retrieves the details of a snapshot schedule.
40+
public static ResourcePolicy getSnapshotSchedule(
41+
String projectId, String region, String snapshotScheduleName) throws IOException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests.
44+
try (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) {
45+
GetResourcePolicyRequest request = GetResourcePolicyRequest.newBuilder()
46+
.setProject(projectId)
47+
.setRegion(region)
48+
.setResourcePolicy(snapshotScheduleName)
49+
.build();
50+
ResourcePolicy resourcePolicy = resourcePoliciesClient.get(request);
51+
System.out.println(resourcePolicy);
52+
53+
return resourcePolicy;
54+
}
55+
}
56+
}
57+
// [END compute_snapshot_schedule_get]

0 commit comments

Comments
 (0)