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]
0 commit comments