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 .snapshot ;
18+
19+ // [START compute_snapshot_schedule_create]
20+ import com .google .cloud .compute .v1 .Operation ;
21+ import com .google .cloud .compute .v1 .ResourcePoliciesClient ;
22+ import com .google .cloud .compute .v1 .ResourcePolicy ;
23+ import com .google .cloud .compute .v1 .ResourcePolicyHourlyCycle ;
24+ import com .google .cloud .compute .v1 .ResourcePolicySnapshotSchedulePolicy ;
25+ import com .google .cloud .compute .v1 .ResourcePolicySnapshotSchedulePolicyRetentionPolicy ;
26+ import com .google .cloud .compute .v1 .ResourcePolicySnapshotSchedulePolicySchedule ;
27+ import com .google .cloud .compute .v1 .ResourcePolicySnapshotSchedulePolicySnapshotProperties ;
28+ import java .io .IOException ;
29+ import java .util .concurrent .ExecutionException ;
30+ import java .util .concurrent .TimeUnit ;
31+ import java .util .concurrent .TimeoutException ;
32+
33+ public class CreateSnapshotSchedule {
34+ public static void main (String [] args )
35+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
36+ // TODO(developer): Replace these variables before running the sample.
37+ // Project ID or project number of the Cloud project you want to use.
38+ String projectId = "YOUR_PROJECT_ID" ;
39+ // Name of the region in which you want to create the snapshot schedule.
40+ String region = "us-central1" ;
41+ // Name of the snapshot schedule you want to create.
42+ String scheduleName = "YOUR_SCHEDULE_NAME" ;
43+ // Description of the snapshot schedule.
44+ String scheduleDescription = "YOUR_SCHEDULE_DESCRIPTION" ;
45+ // Maximum number of days to retain snapshots.
46+ int maxRetentionDays = 10 ;
47+ // Storage location for the snapshots.
48+ // More about storage locations:
49+ // https://cloud.google.com/compute/docs/disks/snapshots?authuser=0#selecting_a_storage_location
50+ String storageLocation = "US" ;
51+ // Determines what happens to your snapshots if the source disk is deleted.
52+ String onSourceDiskDelete = "KEEP_AUTO_SNAPSHOTS" ;
53+
54+ createSnapshotSchedule (projectId , region , scheduleName , scheduleDescription , maxRetentionDays ,
55+ storageLocation , onSourceDiskDelete );
56+ }
57+
58+ // Creates a snapshot schedule policy.
59+ public static Operation .Status createSnapshotSchedule (String projectId , String region ,
60+ String scheduleName , String scheduleDescription , int maxRetentionDays ,
61+ String storageLocation , String onSourceDiskDelete )
62+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
63+ String startTime = "08:00" ;
64+ ResourcePolicySnapshotSchedulePolicySnapshotProperties snapshotProperties =
65+ ResourcePolicySnapshotSchedulePolicySnapshotProperties .newBuilder ()
66+ .addStorageLocations (storageLocation )
67+ .build ();
68+
69+ // Define the hourly schedule:
70+ int snapshotInterval = 10 ; // Create a snapshot every 10 hours
71+ ResourcePolicyHourlyCycle hourlyCycle = ResourcePolicyHourlyCycle .newBuilder ()
72+ .setHoursInCycle (snapshotInterval )
73+ .setStartTime (startTime )
74+ .build ();
75+
76+ // Define the daily schedule.
77+ // ResourcePolicyDailyCycle dailySchedule =
78+ // ResourcePolicyDailyCycle.newBuilder()
79+ // .setDaysInCycle(1) // Every day
80+ // .setStartTime(startTime)
81+ // .build();
82+
83+ // Define the weekly schedule.
84+ // List<ResourcePolicyWeeklyCycleDayOfWeek> dayOfWeeks = new ArrayList<>();
85+ // ResourcePolicyWeeklyCycleDayOfWeek tuesdaySchedule =
86+ // ResourcePolicyWeeklyCycleDayOfWeek.newBuilder()
87+ // .setDay(ResourcePolicyWeeklyCycleDayOfWeek.Day.TUESDAY.toString())
88+ // .setStartTime(startTime)
89+ // .build();
90+ // dayOfWeeks.add(tuesdaySchedule);
91+ //
92+ // ResourcePolicyWeeklyCycle weeklyCycle = ResourcePolicyWeeklyCycle.newBuilder()
93+ // .addAllDayOfWeeks(dayOfWeeks)
94+ // .build();
95+
96+ ResourcePolicySnapshotSchedulePolicyRetentionPolicy retentionPolicy =
97+ ResourcePolicySnapshotSchedulePolicyRetentionPolicy .newBuilder ()
98+ .setMaxRetentionDays (maxRetentionDays )
99+ .setOnSourceDiskDelete (onSourceDiskDelete )
100+ .build ();
101+
102+ ResourcePolicySnapshotSchedulePolicy snapshotSchedulePolicy =
103+ ResourcePolicySnapshotSchedulePolicy .newBuilder ()
104+ .setRetentionPolicy (retentionPolicy )
105+ .setSchedule (ResourcePolicySnapshotSchedulePolicySchedule .newBuilder ()
106+ // You can set only one of the following options:
107+ .setHourlySchedule (hourlyCycle ) //Set Hourly Schedule
108+ // .setDailySchedule(dailySchedule) //Set Daily Schedule
109+ // .setWeeklySchedule(weeklyCycle) // Set Weekly Schedule
110+ .build ())
111+ .setSnapshotProperties (snapshotProperties )
112+ .build ();
113+
114+ ResourcePolicy resourcePolicy = ResourcePolicy .newBuilder ()
115+ .setName (scheduleName )
116+ .setDescription (scheduleDescription )
117+ .setSnapshotSchedulePolicy (snapshotSchedulePolicy )
118+ .build ();
119+
120+ // Initialize client that will be used to send requests. This client only needs to be created
121+ // once, and can be reused for multiple requests.
122+ try (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient .create ()) {
123+
124+ Operation response = resourcePoliciesClient .insertAsync (projectId , region , resourcePolicy )
125+ .get (3 , TimeUnit .MINUTES );
126+
127+ if (response .hasError ()) {
128+ System .out .printf ("Snapshot schedule creation failed: %s%n" , response .getError ());
129+ return null ;
130+ }
131+ return response .getStatus ();
132+ }
133+ }
134+ }
135+ // [END compute_snapshot_schedule_create]
0 commit comments