-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(compute): add compute snapshot schedule create/get/edit/list/delete samples #9742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 22 commits
77d892e
8a5b2f0
9520be3
dbf5596
361f09f
7df8f9d
8973264
4f97e7e
569c77a
cddbab4
8f5fd16
91669ab
84fc307
aee7e52
3fa52d8
a0558f5
195d67f
26ce8b8
6e2f4f3
7df09a5
d9657a0
4e633e8
a34e97f
1f80e08
9e093f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* 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_create] | ||
import com.google.cloud.compute.v1.InsertResourcePolicyRequest; | ||
import com.google.cloud.compute.v1.Operation; | ||
import com.google.cloud.compute.v1.Operation.Status; | ||
import com.google.cloud.compute.v1.ResourcePoliciesClient; | ||
import com.google.cloud.compute.v1.ResourcePolicy; | ||
import com.google.cloud.compute.v1.ResourcePolicyHourlyCycle; | ||
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicy; | ||
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy; | ||
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySchedule; | ||
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySnapshotProperties; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class CreateSnapshotSchedule { | ||
public static void main(String[] args) | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
// 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 region in which you want to create the snapshot schedule. | ||
String region = "us-central1"; | ||
// Name of the snapshot schedule you want to create. | ||
String snapshotScheduleName = "YOUR_SCHEDULE_NAME"; | ||
// Description of the snapshot schedule. | ||
String scheduleDescription = "YOUR_SCHEDULE_DESCRIPTION"; | ||
// Maximum number of days to retain snapshots. | ||
int maxRetentionDays = 10; | ||
// Storage location for the snapshots. | ||
// More about storage locations: | ||
// https://cloud.google.com/compute/docs/disks/snapshots?authuser=0#selecting_a_storage_location | ||
String storageLocation = "US"; | ||
// Determines what happens to your snapshots if the source disk is deleted. | ||
String onSourceDiskDelete = "KEEP_AUTO_SNAPSHOTS"; | ||
|
||
createSnapshotSchedule(projectId, region, snapshotScheduleName, scheduleDescription, | ||
maxRetentionDays, storageLocation, onSourceDiskDelete); | ||
} | ||
|
||
// Creates a snapshot schedule policy. | ||
public static Status createSnapshotSchedule(String projectId, String region, | ||
String snapshotScheduleName, String scheduleDescription, int maxRetentionDays, | ||
String storageLocation, String onSourceDiskDelete) | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
String startTime = "08:00"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems odd. Kindly add a relevant documentation on the time specification format. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. START_TIME: the start time in the UTC time zone. The time must start on the hour. For example: 2:00 PM PST must be specified as 22:00. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kindly move all the code into the try catch block. And adopt this across samples in all PRs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! Fixed |
||
ResourcePolicySnapshotSchedulePolicySnapshotProperties snapshotProperties = | ||
ResourcePolicySnapshotSchedulePolicySnapshotProperties.newBuilder() | ||
.addStorageLocations(storageLocation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kindly match the properties with the REST example in the documentation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the REST example in the documentation we have storageLocations parameters from snapshotProperties. https://cloud.google.com/compute/docs/disks/scheduled-snapshots?authuser=0#rest There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the snapshot properties, we also have the |
||
.build(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this to after line 102 to match the order with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! Moved |
||
|
||
// Define the hourly schedule: | ||
int snapshotInterval = 10; // Create a snapshot every 10 hours | ||
ResourcePolicyHourlyCycle hourlyCycle = ResourcePolicyHourlyCycle.newBuilder() | ||
.setHoursInCycle(snapshotInterval) | ||
.setStartTime(startTime) | ||
.build(); | ||
|
||
// Define the daily schedule. | ||
// ResourcePolicyDailyCycle dailySchedule = | ||
// ResourcePolicyDailyCycle.newBuilder() | ||
// .setDaysInCycle(1) // Every day | ||
// .setStartTime(startTime) | ||
// .build(); | ||
|
||
// Define the weekly schedule. | ||
// List<ResourcePolicyWeeklyCycleDayOfWeek> dayOfWeeks = new ArrayList<>(); | ||
// ResourcePolicyWeeklyCycleDayOfWeek tuesdaySchedule = | ||
// ResourcePolicyWeeklyCycleDayOfWeek.newBuilder() | ||
// .setDay(ResourcePolicyWeeklyCycleDayOfWeek.Day.TUESDAY.toString()) | ||
// .setStartTime(startTime) | ||
// .build(); | ||
// dayOfWeeks.add(tuesdaySchedule); | ||
// | ||
// ResourcePolicyWeeklyCycle weeklyCycle = ResourcePolicyWeeklyCycle.newBuilder() | ||
// .addAllDayOfWeeks(dayOfWeeks) | ||
// .build(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to show this in the sample? Having commented out code is not the best practice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! Deleted |
||
|
||
ResourcePolicySnapshotSchedulePolicyRetentionPolicy retentionPolicy = | ||
ResourcePolicySnapshotSchedulePolicyRetentionPolicy.newBuilder() | ||
.setMaxRetentionDays(maxRetentionDays) | ||
.setOnSourceDiskDelete(onSourceDiskDelete) | ||
.build(); | ||
|
||
ResourcePolicySnapshotSchedulePolicy snapshotSchedulePolicy = | ||
ResourcePolicySnapshotSchedulePolicy.newBuilder() | ||
.setRetentionPolicy(retentionPolicy) | ||
.setSchedule(ResourcePolicySnapshotSchedulePolicySchedule.newBuilder() | ||
// You can set only one of the following options: | ||
.setHourlySchedule(hourlyCycle) //Set Hourly Schedule | ||
// .setDailySchedule(dailySchedule) //Set Daily Schedule | ||
// .setWeeklySchedule(weeklyCycle) // Set Weekly Schedule | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question here. I think we should remove this. Makes the code more complicated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! Deleted |
||
.build()) | ||
.setSnapshotProperties(snapshotProperties) | ||
.build(); | ||
|
||
ResourcePolicy resourcePolicy = ResourcePolicy.newBuilder() | ||
.setName(snapshotScheduleName) | ||
.setDescription(scheduleDescription) | ||
.setSnapshotSchedulePolicy(snapshotSchedulePolicy) | ||
.build(); | ||
|
||
// 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { | ||
InsertResourcePolicyRequest request = InsertResourcePolicyRequest.newBuilder() | ||
TetyanaYahodska marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.setProject(projectId) | ||
.setRegion(region) | ||
.setResourcePolicyResource(resourcePolicy) | ||
.build(); | ||
|
||
Operation response = resourcePoliciesClient.insertAsync(request) | ||
.get(3, TimeUnit.MINUTES); | ||
|
||
if (response.hasError()) { | ||
throw new Error("Snapshot schedule creation failed! " + response.getError()); | ||
} | ||
return response.getStatus(); | ||
} | ||
} | ||
} | ||
// [END compute_snapshot_schedule_create] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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_delete] | ||
import com.google.cloud.compute.v1.DeleteResourcePolicyRequest; | ||
import com.google.cloud.compute.v1.Operation; | ||
import com.google.cloud.compute.v1.Operation.Status; | ||
import com.google.cloud.compute.v1.ResourcePoliciesClient; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class DeleteSnapshotSchedule { | ||
public static void main(String[] args) | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
// 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 region where your snapshot schedule is located. | ||
String region = "us-central1"; | ||
// Name of the snapshot schedule you want to delete. | ||
String snapshotScheduleName = "YOUR_SCHEDULE_NAME"; | ||
|
||
deleteSnapshotSchedule(projectId, region, snapshotScheduleName); | ||
} | ||
|
||
// Deletes a snapshot schedule policy. | ||
public static Status deleteSnapshotSchedule( | ||
String projectId, String region, String snapshotScheduleName) | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
// 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { | ||
DeleteResourcePolicyRequest request = DeleteResourcePolicyRequest.newBuilder() | ||
.setProject(projectId) | ||
.setRegion(region) | ||
.setResourcePolicy(snapshotScheduleName) | ||
.build(); | ||
Operation response = resourcePoliciesClient.deleteAsync(request).get(3, TimeUnit.MINUTES); | ||
Sita04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (response.hasError()) { | ||
throw new Error("Snapshot schedule deletion failed! " + response.getError()); | ||
} | ||
return response.getStatus(); | ||
} | ||
} | ||
} | ||
// [END compute_snapshot_schedule_delete] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* 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_edit] | ||
import com.google.cloud.compute.v1.Operation; | ||
import com.google.cloud.compute.v1.Operation.Status; | ||
import com.google.cloud.compute.v1.PatchResourcePolicyRequest; | ||
import com.google.cloud.compute.v1.ResourcePoliciesClient; | ||
import com.google.cloud.compute.v1.ResourcePolicy; | ||
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy; | ||
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySchedule; | ||
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySnapshotProperties; | ||
import com.google.cloud.compute.v1.ResourcePolicyWeeklyCycle; | ||
import com.google.cloud.compute.v1.ResourcePolicyWeeklyCycleDayOfWeek; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class EditSnapshotSchedule { | ||
|
||
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 region where your snapshot schedule is located. | ||
String region = "us-central1"; | ||
// Name of the snapshot schedule you want to update. | ||
String snapshotScheduleName = "YOUR_SCHEDULE_NAME"; | ||
|
||
editSnapshotSchedule(projectId, region, snapshotScheduleName); | ||
} | ||
|
||
public static Status editSnapshotSchedule( | ||
Sita04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
String projectId, String region, String snapshotScheduleName) | ||
throws IOException, InterruptedException, ExecutionException, TimeoutException { | ||
String description = "Updated description11"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please modify the value to be more descriptive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! Some characters remained after testing. Extra characters have been removed. |
||
Map<String, String> snapshotLabels = new HashMap<>(); | ||
snapshotLabels.put("key", "value"); | ||
ResourcePolicyWeeklyCycleDayOfWeek dayOfWeek = ResourcePolicyWeeklyCycleDayOfWeek.newBuilder() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. Please move all Compute API related code within the try catch block. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
.setDay("Tuesday") | ||
.setStartTime("09:00") | ||
.build(); | ||
ResourcePolicyWeeklyCycle weeklySchedule = ResourcePolicyWeeklyCycle.newBuilder() | ||
.addDayOfWeeks(dayOfWeek) | ||
.build(); | ||
String onSourceDiskDelete = "apply-retention-policy"; | ||
int maxRetentionDays = 3; | ||
|
||
// 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { | ||
ResourcePolicy existingSchedule = resourcePoliciesClient | ||
.get(projectId, region, snapshotScheduleName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to get the existing schedule to set the new properties? The REST example in the docs looks like they override the old properties. Have you tried constructing the patch request directly by setting the new properties, instead of retrieving the old ones and clearing them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes we can construct the patch request directly. Thank you for the suggestion! Fixed code. |
||
|
||
ResourcePolicySnapshotSchedulePolicySnapshotProperties.Builder snapshotProperties = | ||
existingSchedule.getSnapshotSchedulePolicy().getSnapshotProperties().toBuilder(); | ||
snapshotProperties.putAllLabels(snapshotLabels); | ||
|
||
ResourcePolicySnapshotSchedulePolicySchedule.Builder scheduler = | ||
existingSchedule.getSnapshotSchedulePolicy().getSchedule().toBuilder(); | ||
scheduler.clearDailySchedule().clearHourlySchedule(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed as the scheduler is constructed from a new builder. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! Fixed |
||
scheduler.setWeeklySchedule(weeklySchedule); | ||
|
||
ResourcePolicySnapshotSchedulePolicyRetentionPolicy.Builder retentionPolicy = | ||
existingSchedule.getSnapshotSchedulePolicy().getRetentionPolicy().toBuilder(); | ||
retentionPolicy.setOnSourceDiskDelete(onSourceDiskDelete); | ||
retentionPolicy.setMaxRetentionDays(maxRetentionDays); | ||
|
||
ResourcePolicy updatedSchedule = ResourcePolicy.newBuilder() | ||
.setName(existingSchedule.getName()) | ||
.setDescription(description) | ||
.setSnapshotSchedulePolicy( | ||
existingSchedule.getSnapshotSchedulePolicy().toBuilder() | ||
.setSchedule(scheduler) | ||
.setSnapshotProperties(snapshotProperties) | ||
.setRetentionPolicy(retentionPolicy.build()) | ||
.build()) | ||
.build(); | ||
|
||
PatchResourcePolicyRequest request = PatchResourcePolicyRequest.newBuilder() | ||
.setProject(projectId) | ||
.setRegion(region) | ||
.setResourcePolicy(snapshotScheduleName) | ||
.setResourcePolicyResource(updatedSchedule) | ||
.build(); | ||
|
||
Operation response = resourcePoliciesClient.patchAsync(request).get(3, TimeUnit.MINUTES); | ||
|
||
if (response.hasError()) { | ||
throw new Error("Failed to update snapshot schedule! " + response.getError()); | ||
} | ||
return response.getStatus(); | ||
} | ||
} | ||
} | ||
// [END compute_snapshot_schedule_edit] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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_get] | ||
import com.google.cloud.compute.v1.GetResourcePolicyRequest; | ||
import com.google.cloud.compute.v1.ResourcePoliciesClient; | ||
import com.google.cloud.compute.v1.ResourcePolicy; | ||
import java.io.IOException; | ||
|
||
public class GetSnapshotSchedule { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// 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 region in which your snapshot schedule is located. | ||
String region = "us-central1"; | ||
// Name of your snapshot schedule. | ||
String snapshotScheduleName = "YOUR_SCHEDULE_NAME"; | ||
|
||
getSnapshotSchedule(projectId, region, snapshotScheduleName); | ||
} | ||
|
||
// Retrieves the details of a snapshot schedule. | ||
public static ResourcePolicy getSnapshotSchedule( | ||
String projectId, String region, String snapshotScheduleName) throws IOException { | ||
// 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 (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) { | ||
GetResourcePolicyRequest request = GetResourcePolicyRequest.newBuilder() | ||
.setProject(projectId) | ||
.setRegion(region) | ||
.setResourcePolicy(snapshotScheduleName) | ||
.build(); | ||
|
||
return resourcePoliciesClient.get(request); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kindly print before returning the response. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! Added printing |
||
} | ||
} | ||
} | ||
// [END compute_snapshot_schedule_get] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there an enum that can be used here instead of the string literal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we have this enum. Thank you! Fixed