Skip to content

Commit 32394d2

Browse files
committed
Merge pull request #780 from mziccard/compute
Add functional methods for snapshots and Snapshot class
2 parents b1274b5 + bc2d7a1 commit 32394d2

File tree

12 files changed

+1061
-41
lines changed

12 files changed

+1061
-41
lines changed

gcloud-java-compute/src/main/java/com/google/gcloud/compute/Address.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ public final int hashCode() {
172172
return Objects.hash(super.hashCode(), options);
173173
}
174174

175-
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
176-
in.defaultReadObject();
175+
private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
176+
input.defaultReadObject();
177177
this.compute = options.service();
178178
}
179179

gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java

+183-9
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ class DiskTypeFilter extends ListFilter {
582582

583583
private static final long serialVersionUID = 4847837203592234453L;
584584

585-
DiskTypeFilter(DiskTypeField field, ComparisonOperator operator, Object value) {
585+
private DiskTypeFilter(DiskTypeField field, ComparisonOperator operator, Object value) {
586586
super(field.selector(), operator, value);
587587
}
588588

@@ -630,7 +630,7 @@ class MachineTypeFilter extends ListFilter {
630630

631631
private static final long serialVersionUID = 7346062041571853235L;
632632

633-
MachineTypeFilter(MachineTypeField field, ComparisonOperator operator, Object value) {
633+
private MachineTypeFilter(MachineTypeField field, ComparisonOperator operator, Object value) {
634634
super(field.selector(), operator, value);
635635
}
636636

@@ -678,7 +678,7 @@ class RegionFilter extends ListFilter {
678678

679679
private static final long serialVersionUID = 4464892812442567172L;
680680

681-
RegionFilter(RegionField field, ComparisonOperator operator, Object value) {
681+
private RegionFilter(RegionField field, ComparisonOperator operator, Object value) {
682682
super(field.selector(), operator, value);
683683
}
684684

@@ -712,7 +712,7 @@ class ZoneFilter extends ListFilter {
712712

713713
private static final long serialVersionUID = -3927428278548808737L;
714714

715-
ZoneFilter(ZoneField field, ComparisonOperator operator, Object value) {
715+
private ZoneFilter(ZoneField field, ComparisonOperator operator, Object value) {
716716
super(field.selector(), operator, value);
717717
}
718718

@@ -746,7 +746,7 @@ class OperationFilter extends ListFilter {
746746

747747
private static final long serialVersionUID = -3202249202748346427L;
748748

749-
OperationFilter(OperationField field, ComparisonOperator operator, Object value) {
749+
private OperationFilter(OperationField field, ComparisonOperator operator, Object value) {
750750
super(field.selector(), operator, value);
751751
}
752752

@@ -794,7 +794,7 @@ class AddressFilter extends ListFilter {
794794

795795
private static final long serialVersionUID = -227481644259653765L;
796796

797-
AddressFilter(AddressField field, ComparisonOperator operator, Object value) {
797+
private AddressFilter(AddressField field, ComparisonOperator operator, Object value) {
798798
super(field.selector(), operator, value);
799799
}
800800

@@ -821,6 +821,54 @@ public static AddressFilter notEquals(AddressField field, String value) {
821821
}
822822
}
823823

824+
/**
825+
* Class for filtering snapshot lists.
826+
*/
827+
class SnapshotFilter extends ListFilter {
828+
829+
private static final long serialVersionUID = 8757711630092406747L;
830+
831+
private SnapshotFilter(SnapshotField field, ComparisonOperator operator, Object value) {
832+
super(field.selector(), operator, value);
833+
}
834+
835+
/**
836+
* Returns an equals filter for the given field and string value. For string fields,
837+
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
838+
* match the entire field.
839+
*
840+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
841+
*/
842+
public static SnapshotFilter equals(SnapshotField field, String value) {
843+
return new SnapshotFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
844+
}
845+
846+
/**
847+
* Returns a not-equals filter for the given field and string value. For string fields,
848+
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
849+
* match the entire field.
850+
*
851+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
852+
*/
853+
public static SnapshotFilter notEquals(SnapshotField field, String value) {
854+
return new SnapshotFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
855+
}
856+
857+
/**
858+
* Returns an equals filter for the given field and long value.
859+
*/
860+
public static SnapshotFilter equals(SnapshotField field, long value) {
861+
return new SnapshotFilter(checkNotNull(field), ComparisonOperator.EQ, value);
862+
}
863+
864+
/**
865+
* Returns a not-equals filter for the given field and long value.
866+
*/
867+
public static SnapshotFilter notEquals(SnapshotField field, long value) {
868+
return new SnapshotFilter(checkNotNull(field), ComparisonOperator.NE, value);
869+
}
870+
}
871+
824872
/**
825873
* Class for specifying disk type get options.
826874
*/
@@ -863,6 +911,7 @@ public static DiskTypeListOption filter(DiskTypeFilter filter) {
863911

864912
/**
865913
* Returns an option to specify the maximum number of disk types returned per page.
914+
* {@code pageSize} must be between 0 and 500 (inclusive). If not specified 500 is used.
866915
*/
867916
public static DiskTypeListOption pageSize(long pageSize) {
868917
return new DiskTypeListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -908,6 +957,7 @@ public static DiskTypeAggregatedListOption filter(DiskTypeFilter filter) {
908957

909958
/**
910959
* Returns an option to specify the maximum number of disk types returned per page.
960+
* {@code pageSize} must be between 0 and 500 (inclusive). If not specified 500 is used.
911961
*/
912962
public static DiskTypeAggregatedListOption pageSize(long pageSize) {
913963
return new DiskTypeAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -963,6 +1013,7 @@ public static MachineTypeListOption filter(MachineTypeFilter filter) {
9631013

9641014
/**
9651015
* Returns an option to specify the maximum number of machine types returned per page.
1016+
* {@code pageSize} must be between 0 and 500 (inclusive). If not specified 500 is used.
9661017
*/
9671018
public static MachineTypeListOption pageSize(long pageSize) {
9681019
return new MachineTypeListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -1008,6 +1059,7 @@ public static MachineTypeAggregatedListOption filter(MachineTypeFilter filter) {
10081059

10091060
/**
10101061
* Returns an option to specify the maximum number of machine types returned per page.
1062+
* {@code pageSize} must be between 0 and 500 (inclusive). If not specified 500 is used.
10111063
*/
10121064
public static MachineTypeAggregatedListOption pageSize(long pageSize) {
10131065
return new MachineTypeAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -1063,6 +1115,7 @@ public static RegionListOption filter(RegionFilter filter) {
10631115

10641116
/**
10651117
* Returns an option to specify the maximum number of regions returned per page.
1118+
* {@code pageSize} must be between 0 and 500 (inclusive). If not specified 500 is used.
10661119
*/
10671120
public static RegionListOption pageSize(long pageSize) {
10681121
return new RegionListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -1130,6 +1183,7 @@ public static ZoneListOption filter(ZoneFilter filter) {
11301183

11311184
/**
11321185
* Returns an option to specify the maximum number of zones returned per page.
1186+
* {@code pageSize} must be between 0 and 500 (inclusive). If not specified 500 is used.
11331187
*/
11341188
public static ZoneListOption pageSize(long pageSize) {
11351189
return new ZoneListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -1219,6 +1273,7 @@ public static OperationListOption filter(OperationFilter filter) {
12191273

12201274
/**
12211275
* Returns an option to specify the maximum number of operations returned per page.
1276+
* {@code pageSize} must be between 0 and 500 (inclusive). If not specified 500 is used.
12221277
*/
12231278
public static OperationListOption pageSize(long pageSize) {
12241279
return new OperationListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -1286,6 +1341,7 @@ public static AddressListOption filter(AddressFilter filter) {
12861341

12871342
/**
12881343
* Returns an option to specify the maximum number of addresses returned per page.
1344+
* {@code pageSize} must be between 0 and 500 (inclusive). If not specified 500 is used.
12891345
*/
12901346
public static AddressListOption pageSize(long pageSize) {
12911347
return new AddressListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -1331,6 +1387,7 @@ public static AddressAggregatedListOption filter(AddressFilter filter) {
13311387

13321388
/**
13331389
* Returns an option to specify the maximum number of addresses returned per page.
1390+
* {@code pageSize} must be between 0 and 500 (inclusive). If not specified 500 is used.
13341391
*/
13351392
public static AddressAggregatedListOption pageSize(long pageSize) {
13361393
return new AddressAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -1344,6 +1401,74 @@ public static AddressAggregatedListOption pageToken(String pageToken) {
13441401
}
13451402
}
13461403

1404+
/**
1405+
* Class for specifying snapshot get options.
1406+
*/
1407+
class SnapshotOption extends Option {
1408+
1409+
private static final long serialVersionUID = -3505179459035500945L;
1410+
1411+
private SnapshotOption(ComputeRpc.Option option, Object value) {
1412+
super(option, value);
1413+
}
1414+
1415+
/**
1416+
* Returns an option to specify the snapshot's fields to be returned by the RPC call. If this
1417+
* option is not provided, all the snapshot's fields are returned. {@code SnapshotOption.fields}
1418+
* can be used to specify only the fields of interest. {@link Snapshot#snapshotId()} is always
1419+
* returned, even if not specified.
1420+
*/
1421+
public static SnapshotOption fields(SnapshotField... fields) {
1422+
return new SnapshotOption(ComputeRpc.Option.FIELDS, SnapshotField.selector(fields));
1423+
}
1424+
}
1425+
1426+
/**
1427+
* Class for specifying snapshot list options.
1428+
*/
1429+
class SnapshotListOption extends Option {
1430+
1431+
private static final long serialVersionUID = 8278588147660831257L;
1432+
1433+
private SnapshotListOption(ComputeRpc.Option option, Object value) {
1434+
super(option, value);
1435+
}
1436+
1437+
/**
1438+
* Returns an option to specify a filter on the snapshots being listed.
1439+
*/
1440+
public static SnapshotListOption filter(SnapshotFilter filter) {
1441+
return new SnapshotListOption(ComputeRpc.Option.FILTER, filter.toPb());
1442+
}
1443+
1444+
/**
1445+
* Returns an option to specify the maximum number of snapshots returned per page.
1446+
* {@code pageSize} must be between 0 and 500 (inclusive). If not specified 500 is used.
1447+
*/
1448+
public static SnapshotListOption pageSize(long pageSize) {
1449+
return new SnapshotListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
1450+
}
1451+
1452+
/**
1453+
* Returns an option to specify the page token from which to start listing snapshots.
1454+
*/
1455+
public static SnapshotListOption pageToken(String pageToken) {
1456+
return new SnapshotListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
1457+
}
1458+
1459+
/**
1460+
* Returns an option to specify the snapshot's fields to be returned by the RPC call. If this
1461+
* option is not provided, all the snapshot's fields are returned.
1462+
* {@code SnapshotListOption.fields} can be used to specify only the fields of interest.
1463+
* {@link Snapshot#snapshotId()} is always returned, even if not specified.
1464+
*/
1465+
public static SnapshotListOption fields(SnapshotField... fields) {
1466+
StringBuilder builder = new StringBuilder();
1467+
builder.append("items(").append(SnapshotField.selector(fields)).append("),nextPageToken");
1468+
return new SnapshotListOption(ComputeRpc.Option.FIELDS, builder.toString());
1469+
}
1470+
}
1471+
13471472
/**
13481473
* Returns the requested disk type or {@code null} if not found.
13491474
*
@@ -1366,7 +1491,7 @@ public static AddressAggregatedListOption pageToken(String pageToken) {
13661491
Page<DiskType> listDiskTypes(String zone, DiskTypeListOption... options);
13671492

13681493
/**
1369-
* Lists all disk types.
1494+
* Lists the disk types in all zones.
13701495
*
13711496
* @throws ComputeException upon failure
13721497
*/
@@ -1394,7 +1519,7 @@ public static AddressAggregatedListOption pageToken(String pageToken) {
13941519
Page<MachineType> listMachineTypes(String zone, MachineTypeListOption... options);
13951520

13961521
/**
1397-
* Lists all machine types.
1522+
* Lists the machine types in all zones.
13981523
*
13991524
* @throws ComputeException upon failure
14001525
*/
@@ -1511,7 +1636,7 @@ public static AddressAggregatedListOption pageToken(String pageToken) {
15111636
Page<Address> listRegionAddresses(String region, AddressListOption... options);
15121637

15131638
/**
1514-
* Lists all addresses.
1639+
* Lists both global and region addresses.
15151640
*
15161641
* @throws ComputeException upon failure
15171642
*/
@@ -1525,4 +1650,53 @@ public static AddressAggregatedListOption pageToken(String pageToken) {
15251650
* @throws ComputeException upon failure
15261651
*/
15271652
Operation delete(AddressId addressId, OperationOption... options);
1653+
1654+
/**
1655+
* Creates a new snapshot.
1656+
*
1657+
* @return a zone operation if the create request was issued correctly, {@code null} if
1658+
* {@code snapshot.sourceDisk} was not found
1659+
* @throws ComputeException upon failure
1660+
*/
1661+
Operation create(SnapshotInfo snapshot, OperationOption... options);
1662+
1663+
/**
1664+
* Returns the requested snapshot or {@code null} if not found.
1665+
*
1666+
* @throws ComputeException upon failure
1667+
*/
1668+
Snapshot getSnapshot(String snapshot, SnapshotOption... options);
1669+
1670+
/**
1671+
* Lists snapshots.
1672+
*
1673+
* @throws ComputeException upon failure
1674+
*/
1675+
Page<Snapshot> listSnapshots(SnapshotListOption... options);
1676+
1677+
/**
1678+
* Deletes the requested snapshot. Keep in mind that deleting a single snapshot might not
1679+
* necessarily delete all the data for that snapshot. If any data for the snapshot that is marked
1680+
* for deletion is needed for subsequent snapshots, the data will be moved to the next snapshot.
1681+
*
1682+
* @return a global operation if the request was issued correctly, {@code null} if the snapshot
1683+
* was not found
1684+
* @throws ComputeException upon failure
1685+
* @see <a href="https://cloud.google.com/compute/docs/disks/persistent-disks#deleting_snapshot">
1686+
* Deleting a snapshot</a>
1687+
*/
1688+
Operation deleteSnapshot(SnapshotId snapshot, OperationOption... options);
1689+
1690+
/**
1691+
* Deletes the requested snapshot. Keep in mind that deleting a single snapshot might not
1692+
* necessarily delete all the data for that snapshot. If any data on the snapshot that is marked
1693+
* for deletion is needed for subsequent snapshots, the data will be moved to the next snapshot.
1694+
*
1695+
* @return a global operation if the request was issued correctly, {@code null} if the snapshot
1696+
* was not found
1697+
* @throws ComputeException upon failure
1698+
* @see <a href="https://cloud.google.com/compute/docs/disks/persistent-disks#deleting_snapshot">
1699+
* Deleting a snapshot</a>
1700+
*/
1701+
Operation deleteSnapshot(String snapshot, OperationOption... options);
15281702
}

0 commit comments

Comments
 (0)