Skip to content

Commit fe57d61

Browse files
committed
Add functional methods for snapshots and Snapshot class
1 parent f968d38 commit fe57d61

File tree

8 files changed

+991
-8
lines changed

8 files changed

+991
-8
lines changed

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

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
SnapshotFilter(SnapshotField field, ComparisonOperator operator, Object value) {
832+
super(field.selector(), operator, value);
833+
}
834+
835+
/**
836+
* Returns an equality 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 equality 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
*/
@@ -1344,6 +1392,73 @@ public static AddressAggregatedListOption pageToken(String pageToken) {
13441392
}
13451393
}
13461394

1395+
/**
1396+
* Class for specifying snapshot get options.
1397+
*/
1398+
class SnapshotOption extends Option {
1399+
1400+
private static final long serialVersionUID = -3505179459035500945L;
1401+
1402+
private SnapshotOption(ComputeRpc.Option option, Object value) {
1403+
super(option, value);
1404+
}
1405+
1406+
/**
1407+
* Returns an option to specify the snapshot's fields to be returned by the RPC call. If this
1408+
* option is not provided, all snapshot's fields are returned. {@code SnapshotOption.fields} can
1409+
* be used to specify only the fields of interest. {@link Snapshot#snapshotId()} is always
1410+
* returned, even if not specified.
1411+
*/
1412+
public static SnapshotOption fields(SnapshotField... fields) {
1413+
return new SnapshotOption(ComputeRpc.Option.FIELDS, SnapshotField.selector(fields));
1414+
}
1415+
}
1416+
1417+
/**
1418+
* Class for specifying snapshot list options.
1419+
*/
1420+
class SnapshotListOption extends Option {
1421+
1422+
private static final long serialVersionUID = 8278588147660831257L;
1423+
1424+
private SnapshotListOption(ComputeRpc.Option option, Object value) {
1425+
super(option, value);
1426+
}
1427+
1428+
/**
1429+
* Returns an option to specify a filter on the snapshots being listed.
1430+
*/
1431+
public static SnapshotListOption filter(SnapshotFilter filter) {
1432+
return new SnapshotListOption(ComputeRpc.Option.FILTER, filter.toPb());
1433+
}
1434+
1435+
/**
1436+
* Returns an option to specify the maximum number of snapshots returned per page.
1437+
*/
1438+
public static SnapshotListOption pageSize(long pageSize) {
1439+
return new SnapshotListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
1440+
}
1441+
1442+
/**
1443+
* Returns an option to specify the page token from which to start listing snapshots.
1444+
*/
1445+
public static SnapshotListOption pageToken(String pageToken) {
1446+
return new SnapshotListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
1447+
}
1448+
1449+
/**
1450+
* Returns an option to specify the snapshot's fields to be returned by the RPC call. If this
1451+
* option is not provided, all snapshot's fields are returned. {@code SnapshotListOption.fields}
1452+
* can be used to specify only the fields of interest. {@link Snapshot#snapshotId()} is always
1453+
* returned, even if not specified.
1454+
*/
1455+
public static SnapshotListOption fields(SnapshotField... fields) {
1456+
StringBuilder builder = new StringBuilder();
1457+
builder.append("items(").append(SnapshotField.selector(fields)).append("),nextPageToken");
1458+
return new SnapshotListOption(ComputeRpc.Option.FIELDS, builder.toString());
1459+
}
1460+
}
1461+
13471462
/**
13481463
* Returns the requested disk type or {@code null} if not found.
13491464
*
@@ -1525,4 +1640,53 @@ public static AddressAggregatedListOption pageToken(String pageToken) {
15251640
* @throws ComputeException upon failure
15261641
*/
15271642
Operation delete(AddressId addressId, OperationOption... options);
1643+
1644+
/**
1645+
* Creates a new snapshot.
1646+
*
1647+
* @return a zone operation if the create request was issued correctly, {@code null} if
1648+
* {@code snapshot.sourceDisk} was not found
1649+
* @throws ComputeException upon failure
1650+
*/
1651+
Operation create(SnapshotInfo snapshot, OperationOption... options);
1652+
1653+
/**
1654+
* Returns the requested snapshot or {@code null} if not found.
1655+
*
1656+
* @throws ComputeException upon failure
1657+
*/
1658+
Snapshot getSnapshot(String snapshot, SnapshotOption... options);
1659+
1660+
/**
1661+
* Lists all snapshots.
1662+
*
1663+
* @throws ComputeException upon failure
1664+
*/
1665+
Page<Snapshot> listSnapshots(SnapshotListOption... options);
1666+
1667+
/**
1668+
* Deletes the requested snapshot. Keep in mind that deleting a single snapshot might not
1669+
* necessarily delete all the data for that snapshot. If any data for the snapshot that is marked
1670+
* for deletion is needed for subsequent snapshots, the data will be moved to the next snapshot.
1671+
*
1672+
* @return a global operation if the request was issued correctly, {@code null} if the snapshot
1673+
* was not found
1674+
* @throws ComputeException upon failure
1675+
* @see <a href="https://cloud.google.com/compute/docs/disks/persistent-disks#deleting_snapshot">
1676+
* Deleting a snapshot</a>
1677+
*/
1678+
Operation deleteSnapshot(SnapshotId snapshot, OperationOption... options);
1679+
1680+
/**
1681+
* Deletes the requested snapshot. Keep in mind that deleting a single snapshot might not
1682+
* necessarily delete all the data for that snapshot. If any data on the snapshot that is marked
1683+
* for deletion is needed for subsequent snapshots, the data will be moved to the next snapshot.
1684+
*
1685+
* @return a global operation if the request was issued correctly, {@code null} if the snapshot
1686+
* was not found
1687+
* @throws ComputeException upon failure
1688+
* @see <a href="https://cloud.google.com/compute/docs/disks/persistent-disks#deleting_snapshot">
1689+
* Deleting a snapshot</a>
1690+
*/
1691+
Operation deleteSnapshot(String snapshot, OperationOption... options);
15281692
}

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

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,25 @@ public Page<Address> nextPage() {
273273
}
274274
}
275275

276+
private static class SnapshotPageFetcher implements NextPageFetcher<Snapshot> {
277+
278+
private static final long serialVersionUID = 6205774609802216986L;
279+
private final Map<ComputeRpc.Option, ?> requestOptions;
280+
private final ComputeOptions serviceOptions;
281+
282+
SnapshotPageFetcher(ComputeOptions serviceOptions, String cursor,
283+
Map<ComputeRpc.Option, ?> optionMap) {
284+
this.requestOptions =
285+
PageImpl.nextRequestOptions(ComputeRpc.Option.PAGE_TOKEN, cursor, optionMap);
286+
this.serviceOptions = serviceOptions;
287+
}
288+
289+
@Override
290+
public Page<Snapshot> nextPage() {
291+
return listSnapshots(serviceOptions, requestOptions);
292+
}
293+
}
294+
276295
private final ComputeRpc computeRpc;
277296

278297
ComputeImpl(ComputeOptions options) {
@@ -881,8 +900,8 @@ public Address apply(com.google.api.services.compute.model.Address address) {
881900
return Address.fromPb(serviceOptions.service(), address);
882901
}
883902
});
884-
return new PageImpl<>(new AggregatedAddressPageFetcher(serviceOptions, cursor,
885-
optionsMap), cursor, operations);
903+
return new PageImpl<>(new AggregatedAddressPageFetcher(serviceOptions, cursor, optionsMap),
904+
cursor, operations);
886905
} catch (RetryHelper.RetryHelperException e) {
887906
throw ComputeException.translateAndThrow(e);
888907
}
@@ -914,6 +933,99 @@ public com.google.api.services.compute.model.Operation call() {
914933
}
915934
}
916935

936+
@Override
937+
public Operation create(SnapshotInfo snapshot, final OperationOption... options) {
938+
final SnapshotInfo completeSnapshot = snapshot.setProjectId(options().projectId());
939+
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
940+
try {
941+
com.google.api.services.compute.model.Operation answer =
942+
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
943+
@Override
944+
public com.google.api.services.compute.model.Operation call() {
945+
return computeRpc.createSnapshot(completeSnapshot.sourceDisk().zone(),
946+
completeSnapshot.sourceDisk().disk(), completeSnapshot.snapshotId().snapshot(),
947+
completeSnapshot.description(), optionsMap);
948+
}
949+
}, options().retryParams(), EXCEPTION_HANDLER);
950+
return answer == null ? null : Operation.fromPb(this, answer);
951+
} catch (RetryHelper.RetryHelperException e) {
952+
throw ComputeException.translateAndThrow(e);
953+
}
954+
}
955+
956+
@Override
957+
public Snapshot getSnapshot(final String snapshot, SnapshotOption... options) {
958+
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
959+
try {
960+
com.google.api.services.compute.model.Snapshot answer =
961+
runWithRetries(new Callable<com.google.api.services.compute.model.Snapshot>() {
962+
@Override
963+
public com.google.api.services.compute.model.Snapshot call() {
964+
return computeRpc.getSnapshot(snapshot, optionsMap);
965+
}
966+
}, options().retryParams(), EXCEPTION_HANDLER);
967+
return answer == null ? null : Snapshot.fromPb(this, answer);
968+
} catch (RetryHelper.RetryHelperException e) {
969+
throw ComputeException.translateAndThrow(e);
970+
}
971+
}
972+
973+
@Override
974+
public Page<Snapshot> listSnapshots(SnapshotListOption... options) {
975+
return listSnapshots(options(), optionMap(options));
976+
}
977+
978+
private static Page<Snapshot> listSnapshots(final ComputeOptions serviceOptions,
979+
final Map<ComputeRpc.Option, ?> optionsMap) {
980+
try {
981+
ComputeRpc.Tuple<String, Iterable<com.google.api.services.compute.model.Snapshot>> result =
982+
runWithRetries(new Callable<ComputeRpc.Tuple<String,
983+
Iterable<com.google.api.services.compute.model.Snapshot>>>() {
984+
@Override
985+
public ComputeRpc.Tuple<String,
986+
Iterable<com.google.api.services.compute.model.Snapshot>> call() {
987+
return serviceOptions.rpc().listSnapshots(optionsMap);
988+
}
989+
}, serviceOptions.retryParams(), EXCEPTION_HANDLER);
990+
String cursor = result.x();
991+
Iterable<Snapshot> snapshots = Iterables.transform(
992+
result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Snapshot>of()
993+
: result.y(),
994+
new Function<com.google.api.services.compute.model.Snapshot, Snapshot>() {
995+
@Override
996+
public Snapshot apply(com.google.api.services.compute.model.Snapshot snapshot) {
997+
return Snapshot.fromPb(serviceOptions.service(), snapshot);
998+
}
999+
});
1000+
return new PageImpl<>(new SnapshotPageFetcher(serviceOptions, cursor, optionsMap), cursor,
1001+
snapshots);
1002+
} catch (RetryHelper.RetryHelperException e) {
1003+
throw ComputeException.translateAndThrow(e);
1004+
}
1005+
}
1006+
1007+
@Override
1008+
public Operation deleteSnapshot(final SnapshotId snapshot, OperationOption... options) {
1009+
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
1010+
try {
1011+
com.google.api.services.compute.model.Operation answer =
1012+
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
1013+
@Override
1014+
public com.google.api.services.compute.model.Operation call() {
1015+
return computeRpc.deleteSnapshot(snapshot.snapshot(), optionsMap);
1016+
}
1017+
}, options().retryParams(), EXCEPTION_HANDLER);
1018+
return answer == null ? null : Operation.fromPb(this, answer);
1019+
} catch (RetryHelper.RetryHelperException e) {
1020+
throw ComputeException.translateAndThrow(e);
1021+
}
1022+
}
1023+
1024+
@Override
1025+
public Operation deleteSnapshot(final String snapshot, OperationOption... options) {
1026+
return deleteSnapshot(SnapshotId.of(snapshot));
1027+
}
1028+
9171029
private Map<ComputeRpc.Option, ?> optionMap(Option... options) {
9181030
Map<ComputeRpc.Option, Object> optionMap = Maps.newEnumMap(ComputeRpc.Option.class);
9191031
for (Option option : options) {

0 commit comments

Comments
 (0)