Skip to content

Commit 26dddb7

Browse files
Fixed code
1 parent 64d9494 commit 26dddb7

File tree

5 files changed

+31
-26
lines changed

5 files changed

+31
-26
lines changed

compute/cloud-client/src/main/java/compute/disks/CreateDiskWithSnapshotSchedule.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ public static void main(String[] args)
3737
// Name of the disk you want to create.
3838
String diskName = "YOUR_DISK_NAME";
3939
// Name of the schedule you want to link to the disk.
40-
String scheduleName = "YOUR_SCHEDULE_NAME";
40+
String snapshotScheduleName = "YOUR_SCHEDULE_NAME";
4141

42-
createDiskWithSnapshotSchedule(projectId, zone, diskName, scheduleName);
42+
createDiskWithSnapshotSchedule(projectId, zone, diskName, snapshotScheduleName);
4343
}
4444

4545
// Creates disk with linked snapshot schedule.
4646
public static Operation.Status createDiskWithSnapshotSchedule(
47-
String projectId, String zone, String diskName, String scheduleName)
47+
String projectId, String zone, String diskName, String snapshotScheduleName)
4848
throws IOException, ExecutionException, InterruptedException, TimeoutException {
4949
try (DisksClient disksClient = DisksClient.create()) {
5050

5151
// Get the resource policy to link to the disk
5252
String resourcePolicyLink = String.format("projects/%s/regions/%s/resourcePolicies/%s",
53-
projectId, zone.substring(0, zone.lastIndexOf('-')), scheduleName);
53+
projectId, zone.substring(0, zone.lastIndexOf('-')), snapshotScheduleName);
5454

5555
Disk disk = Disk.newBuilder()
5656
.setName(diskName)
@@ -61,8 +61,7 @@ public static Operation.Status createDiskWithSnapshotSchedule(
6161
Operation response = disksClient.insertAsync(projectId, zone, disk).get(3, TimeUnit.MINUTES);
6262

6363
if (response.hasError()) {
64-
System.out.printf("Disk creation failed: %s%n", response.getError());
65-
return null;
64+
throw new Error("Disk creation failed! " + response.getError());
6665
}
6766
return response.getStatus();
6867
}

compute/cloud-client/src/main/java/compute/snapshot/CreateSnapshotSchedule.java renamed to compute/cloud-client/src/main/java/compute/snapshotschedule/CreateSnapshotSchedule.java

Lines changed: 7 additions & 8 deletions
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;
@@ -39,7 +39,7 @@ public static void main(String[] args)
3939
// Name of the region in which you want to create the snapshot schedule.
4040
String region = "us-central1";
4141
// Name of the snapshot schedule you want to create.
42-
String scheduleName = "YOUR_SCHEDULE_NAME";
42+
String snapshotScheduleName = "YOUR_SCHEDULE_NAME";
4343
// Description of the snapshot schedule.
4444
String scheduleDescription = "YOUR_SCHEDULE_DESCRIPTION";
4545
// Maximum number of days to retain snapshots.
@@ -51,13 +51,13 @@ public static void main(String[] args)
5151
// Determines what happens to your snapshots if the source disk is deleted.
5252
String onSourceDiskDelete = "KEEP_AUTO_SNAPSHOTS";
5353

54-
createSnapshotSchedule(projectId, region, scheduleName, scheduleDescription, maxRetentionDays,
55-
storageLocation, onSourceDiskDelete);
54+
createSnapshotSchedule(projectId, region, snapshotScheduleName, scheduleDescription,
55+
maxRetentionDays, storageLocation, onSourceDiskDelete);
5656
}
5757

5858
// Creates a snapshot schedule policy.
5959
public static Operation.Status createSnapshotSchedule(String projectId, String region,
60-
String scheduleName, String scheduleDescription, int maxRetentionDays,
60+
String snapshotScheduleName, String scheduleDescription, int maxRetentionDays,
6161
String storageLocation, String onSourceDiskDelete)
6262
throws IOException, ExecutionException, InterruptedException, TimeoutException {
6363
String startTime = "08:00";
@@ -112,7 +112,7 @@ public static Operation.Status createSnapshotSchedule(String projectId, String r
112112
.build();
113113

114114
ResourcePolicy resourcePolicy = ResourcePolicy.newBuilder()
115-
.setName(scheduleName)
115+
.setName(snapshotScheduleName)
116116
.setDescription(scheduleDescription)
117117
.setSnapshotSchedulePolicy(snapshotSchedulePolicy)
118118
.build();
@@ -125,8 +125,7 @@ public static Operation.Status createSnapshotSchedule(String projectId, String r
125125
.get(3, TimeUnit.MINUTES);
126126

127127
if (response.hasError()) {
128-
System.out.printf("Snapshot schedule creation failed: %s%n", response.getError());
129-
return null;
128+
throw new Error("Snapshot schedule creation failed! " + response.getError());
130129
}
131130
return response.getStatus();
132131
}

compute/cloud-client/src/main/java/compute/snapshot/DeleteSnapshotSchedule.java renamed to compute/cloud-client/src/main/java/compute/snapshotschedule/DeleteSnapshotSchedule.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package compute.snapshot;
17+
package compute.snapshotschedule;
1818

1919
// [START compute_snapshot_schedule_delete]
20+
import com.google.cloud.compute.v1.Operation;
2021
import com.google.cloud.compute.v1.ResourcePoliciesClient;
2122
import java.io.IOException;
2223
import java.util.concurrent.ExecutionException;
@@ -32,19 +33,26 @@ public static void main(String[] args)
3233
// Name of the region where your snapshot schedule is located.
3334
String region = "us-central1";
3435
// Name of the snapshot schedule you want to delete.
35-
String scheduleName = "YOUR_SCHEDULE_NAME";
36+
String snapshotScheduleName = "YOUR_SCHEDULE_NAME";
3637

37-
deleteSnapshotSchedule(projectId, region, scheduleName);
38+
deleteSnapshotSchedule(projectId, region, snapshotScheduleName);
3839
}
3940

4041
// Deletes a snapshot schedule policy.
41-
public static void deleteSnapshotSchedule(String projectId, String region, String scheduleName)
42+
public static Operation.Status deleteSnapshotSchedule(
43+
String projectId, String region, String snapshotScheduleName)
4244
throws IOException, ExecutionException, InterruptedException, TimeoutException {
43-
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests.
4447
try (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) {
45-
resourcePoliciesClient.deleteAsync(projectId, region, scheduleName).get(3, TimeUnit.MINUTES);
48+
Operation response = resourcePoliciesClient
49+
.deleteAsync(projectId, region, snapshotScheduleName)
50+
.get(3, TimeUnit.MINUTES);
4651

47-
System.out.println("Snapshot schedule deleted successfully: " + scheduleName);
52+
if (response.hasError()) {
53+
throw new Error("Snapshot schedule deletion failed! " + response.getError());
54+
}
55+
return response.getStatus();
4856
}
4957
}
5058
}

compute/cloud-client/src/test/java/compute/Util.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package compute;
1818

19-
import static compute.snapshot.DeleteSnapshotSchedule.deleteSnapshotSchedule;
20-
2119
import com.google.cloud.compute.v1.DeleteStoragePoolRequest;
2220
import com.google.cloud.compute.v1.Disk;
2321
import com.google.cloud.compute.v1.DisksClient;
@@ -42,6 +40,7 @@
4240
import compute.disks.DeleteDisk;
4341
import compute.disks.DeleteSnapshot;
4442
import compute.reservation.DeleteReservation;
43+
import compute.snapshotschedule.DeleteSnapshotSchedule;
4544
import java.io.IOException;
4645
import java.lang.Error;
4746
import java.nio.charset.StandardCharsets;
@@ -280,7 +279,7 @@ public static void cleanUpExistingSnapshotSchedule(
280279
for (ResourcePolicy resource : resourcePoliciesClient.list(projectId, region).iterateAll()) {
281280
if (containPrefixToDeleteAndZone(resource, prefixToDelete, region)
282281
&& isCreatedBeforeThresholdTime(resource.getCreationTimestamp())) {
283-
deleteSnapshotSchedule(projectId, region, resource.getName());
282+
DeleteSnapshotSchedule.deleteSnapshotSchedule(projectId, region, resource.getName());
284283
}
285284
}
286285
}

compute/cloud-client/src/test/java/compute/disks/DisksIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
import com.google.cloud.compute.v1.SnapshotsClient;
3636
import compute.DeleteInstance;
3737
import compute.Util;
38-
import compute.snapshot.CreateSnapshotSchedule;
39-
import compute.snapshot.DeleteSnapshotSchedule;
38+
import compute.snapshotschedule.CreateSnapshotSchedule;
39+
import compute.snapshotschedule.DeleteSnapshotSchedule;
4040
import java.io.ByteArrayOutputStream;
4141
import java.io.IOException;
4242
import java.io.PrintStream;

0 commit comments

Comments
 (0)