Skip to content

Commit f675ee5

Browse files
committed
Add functional methods for disks and Disk class
1 parent 6780c0d commit f675ee5

File tree

9 files changed

+1945
-3
lines changed

9 files changed

+1945
-3
lines changed

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

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,54 @@ public static ImageFilter notEquals(ImageField field, long value) {
917917
}
918918
}
919919

920+
/**
921+
* Class for filtering disk lists.
922+
*/
923+
class DiskFilter extends ListFilter {
924+
925+
private static final long serialVersionUID = 5856790665396877913L;
926+
927+
private DiskFilter(DiskField field, ComparisonOperator operator, Object value) {
928+
super(field.selector(), operator, value);
929+
}
930+
931+
/**
932+
* Returns an equals filter for the given field and string value. For string fields,
933+
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
934+
* match the entire field.
935+
*
936+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
937+
*/
938+
public static DiskFilter equals(DiskField field, String value) {
939+
return new DiskFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
940+
}
941+
942+
/**
943+
* Returns a not-equals filter for the given field and string value. For string fields,
944+
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
945+
* match the entire field.
946+
*
947+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
948+
*/
949+
public static DiskFilter notEquals(DiskField field, String value) {
950+
return new DiskFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
951+
}
952+
953+
/**
954+
* Returns an equals filter for the given field and long value.
955+
*/
956+
public static DiskFilter equals(DiskField field, long value) {
957+
return new DiskFilter(checkNotNull(field), ComparisonOperator.EQ, value);
958+
}
959+
960+
/**
961+
* Returns a not-equals filter for the given field and long value.
962+
*/
963+
public static DiskFilter notEquals(DiskField field, long value) {
964+
return new DiskFilter(checkNotNull(field), ComparisonOperator.NE, value);
965+
}
966+
}
967+
920968
/**
921969
* Class for specifying disk type get options.
922970
*/
@@ -1585,6 +1633,110 @@ public static ImageListOption fields(ImageField... fields) {
15851633
}
15861634
}
15871635

1636+
/**
1637+
* Class for specifying disk get options.
1638+
*/
1639+
class DiskOption extends Option {
1640+
1641+
private static final long serialVersionUID = -4354796876226661667L;
1642+
1643+
private DiskOption(ComputeRpc.Option option, Object value) {
1644+
super(option, value);
1645+
}
1646+
1647+
/**
1648+
* Returns an option to specify the disk's fields to be returned by the RPC call. If this option
1649+
* is not provided, all disk's fields are returned. {@code DiskOption.fields} can be used to
1650+
* specify only the fields of interest. {@link Disk#diskId()},
1651+
* {@link DiskConfiguration#diskType()} and {@link SnapshotDiskConfiguration#sourceSnapshot()}
1652+
* or {@link ImageDiskConfiguration#sourceImage()} are always returned, even if not specified.
1653+
*/
1654+
public static DiskOption fields(DiskField... fields) {
1655+
return new DiskOption(ComputeRpc.Option.FIELDS, DiskField.selector(fields));
1656+
}
1657+
}
1658+
1659+
/**
1660+
* Class for specifying disk list options.
1661+
*/
1662+
class DiskListOption extends Option {
1663+
1664+
private static final long serialVersionUID = -5148497888688645905L;
1665+
1666+
private DiskListOption(ComputeRpc.Option option, Object value) {
1667+
super(option, value);
1668+
}
1669+
1670+
/**
1671+
* Returns an option to specify a filter on the disks being listed.
1672+
*/
1673+
public static DiskListOption filter(DiskFilter filter) {
1674+
return new DiskListOption(ComputeRpc.Option.FILTER, filter.toPb());
1675+
}
1676+
1677+
/**
1678+
* Returns an option to specify the maximum number of disks returned per page. {@code pageSize}
1679+
* must be between 0 and 500 (inclusive). If not specified 500 is used.
1680+
*/
1681+
public static DiskListOption pageSize(long pageSize) {
1682+
return new DiskListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
1683+
}
1684+
1685+
/**
1686+
* Returns an option to specify the page token from which to start listing disks.
1687+
*/
1688+
public static DiskListOption pageToken(String pageToken) {
1689+
return new DiskListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
1690+
}
1691+
1692+
/**
1693+
* Returns an option to specify the disk's fields to be returned by the RPC call. If this option
1694+
* is not provided, all disk's fields are returned. {@code DiskListOption.fields} can be used to
1695+
* specify only the fields of interest. {@link Disk#diskId()},
1696+
* {@link DiskConfiguration#diskType()} and {@link SnapshotDiskConfiguration#sourceSnapshot()}
1697+
* or {@link ImageDiskConfiguration#sourceImage()} are always returned, even if not specified.
1698+
*/
1699+
public static DiskListOption fields(DiskField... fields) {
1700+
StringBuilder builder = new StringBuilder();
1701+
builder.append("items(").append(DiskField.selector(fields)).append("),nextPageToken");
1702+
return new DiskListOption(ComputeRpc.Option.FIELDS, builder.toString());
1703+
}
1704+
}
1705+
1706+
/**
1707+
* Class for specifying disk aggregated list options.
1708+
*/
1709+
class DiskAggregatedListOption extends Option {
1710+
1711+
private static final long serialVersionUID = 1163784797870242766L;
1712+
1713+
private DiskAggregatedListOption(ComputeRpc.Option option, Object value) {
1714+
super(option, value);
1715+
}
1716+
1717+
/**
1718+
* Returns an option to specify a filter on the disks being listed.
1719+
*/
1720+
public static DiskAggregatedListOption filter(DiskFilter filter) {
1721+
return new DiskAggregatedListOption(ComputeRpc.Option.FILTER, filter.toPb());
1722+
}
1723+
1724+
/**
1725+
* Returns an option to specify the maximum number of disks returned per page. {@code pageSize}
1726+
* must be between 0 and 500 (inclusive). If not specified 500 is used.
1727+
*/
1728+
public static DiskAggregatedListOption pageSize(long pageSize) {
1729+
return new DiskAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
1730+
}
1731+
1732+
/**
1733+
* Returns an option to specify the page token from which to start listing disks.
1734+
*/
1735+
public static DiskAggregatedListOption pageToken(String pageToken) {
1736+
return new DiskAggregatedListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
1737+
}
1738+
}
1739+
15881740
/**
15891741
* Returns the requested disk type or {@code null} if not found.
15901742
*
@@ -1868,4 +2020,42 @@ public static ImageListOption fields(ImageField... fields) {
18682020
*/
18692021
Operation deprecate(ImageId image, DeprecationStatus<ImageId> deprecationStatus,
18702022
OperationOption... options);
2023+
2024+
/**
2025+
* Returns the requested disk or {@code null} if not found.
2026+
*
2027+
* @throws ComputeException upon failure
2028+
*/
2029+
Disk get(DiskId diskId, DiskOption... options);
2030+
2031+
/**
2032+
* Creates a new disk.
2033+
*
2034+
* @return a zone operation for disk's creation
2035+
* @throws ComputeException upon failure
2036+
*/
2037+
Operation create(DiskInfo disk, OperationOption... options);
2038+
2039+
/**
2040+
* Lists disks for the provided zone.
2041+
*
2042+
* @throws ComputeException upon failure
2043+
*/
2044+
Page<Disk> listDisks(String zone, DiskListOption... options);
2045+
2046+
/**
2047+
* Lists disks for all zones.
2048+
*
2049+
* @throws ComputeException upon failure
2050+
*/
2051+
Page<Disk> listDisks(DiskAggregatedListOption... options);
2052+
2053+
/**
2054+
* Deletes the requested disk.
2055+
*
2056+
* @return a zone operation if the request was issued correctly, {@code null} if the disk was not
2057+
* found
2058+
* @throws ComputeException upon failure
2059+
*/
2060+
Operation delete(DiskId disk, OperationOption... options);
18712061
}

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

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,46 @@ public Page<Image> nextPage() {
313313
}
314314
}
315315

316+
private static class DiskPageFetcher implements NextPageFetcher<Disk> {
317+
318+
private static final long serialVersionUID = 4146589787872718476L;
319+
private final Map<ComputeRpc.Option, ?> requestOptions;
320+
private final ComputeOptions serviceOptions;
321+
private final String zone;
322+
323+
DiskPageFetcher(String zone, ComputeOptions serviceOptions, String cursor,
324+
Map<ComputeRpc.Option, ?> optionMap) {
325+
this.requestOptions =
326+
PageImpl.nextRequestOptions(ComputeRpc.Option.PAGE_TOKEN, cursor, optionMap);
327+
this.serviceOptions = serviceOptions;
328+
this.zone = zone;
329+
}
330+
331+
@Override
332+
public Page<Disk> nextPage() {
333+
return listDisks(zone, serviceOptions, requestOptions);
334+
}
335+
}
336+
337+
private static class AggregatedDiskPageFetcher implements NextPageFetcher<Disk> {
338+
339+
private static final long serialVersionUID = -5240045334115926206L;
340+
private final Map<ComputeRpc.Option, ?> requestOptions;
341+
private final ComputeOptions serviceOptions;
342+
343+
AggregatedDiskPageFetcher(ComputeOptions serviceOptions, String cursor,
344+
Map<ComputeRpc.Option, ?> optionMap) {
345+
this.requestOptions =
346+
PageImpl.nextRequestOptions(ComputeRpc.Option.PAGE_TOKEN, cursor, optionMap);
347+
this.serviceOptions = serviceOptions;
348+
}
349+
350+
@Override
351+
public Page<Disk> nextPage() {
352+
return listDisks(serviceOptions, requestOptions);
353+
}
354+
}
355+
316356
private final ComputeRpc computeRpc;
317357

318358
ComputeImpl(ComputeOptions options) {
@@ -1161,6 +1201,124 @@ public com.google.api.services.compute.model.Operation call() {
11611201
}
11621202
}
11631203

1204+
@Override
1205+
public Disk get(final DiskId diskId, DiskOption... options) {
1206+
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
1207+
try {
1208+
com.google.api.services.compute.model.Disk answer =
1209+
runWithRetries(new Callable<com.google.api.services.compute.model.Disk>() {
1210+
@Override
1211+
public com.google.api.services.compute.model.Disk call() {
1212+
return computeRpc.getDisk(diskId.zone(), diskId.disk(), optionsMap);
1213+
}
1214+
}, options().retryParams(), EXCEPTION_HANDLER);
1215+
return answer == null ? null : Disk.fromPb(this, answer);
1216+
} catch (RetryHelper.RetryHelperException e) {
1217+
throw ComputeException.translateAndThrow(e);
1218+
}
1219+
}
1220+
1221+
@Override
1222+
public Operation create(final DiskInfo disk, OperationOption... options) {
1223+
final com.google.api.services.compute.model.Disk diskPb =
1224+
disk.setProjectId(options().projectId()).toPb();
1225+
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
1226+
try {
1227+
return Operation.fromPb(this,
1228+
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
1229+
@Override
1230+
public com.google.api.services.compute.model.Operation call() {
1231+
return computeRpc.createDisk(disk.diskId().zone(), diskPb, optionsMap);
1232+
}
1233+
}, options().retryParams(), EXCEPTION_HANDLER));
1234+
} catch (RetryHelper.RetryHelperException e) {
1235+
throw ComputeException.translateAndThrow(e);
1236+
}
1237+
}
1238+
1239+
private static Function<com.google.api.services.compute.model.Disk, Disk> diskFromPb(
1240+
final ComputeOptions serviceOptions) {
1241+
return new Function<com.google.api.services.compute.model.Disk, Disk>() {
1242+
@Override
1243+
public Disk apply(com.google.api.services.compute.model.Disk disk) {
1244+
return Disk.fromPb(serviceOptions.service(), disk);
1245+
}
1246+
};
1247+
}
1248+
1249+
@Override
1250+
public Page<Disk> listDisks(String zone, DiskListOption... options) {
1251+
return listDisks(zone, options(), optionMap(options));
1252+
}
1253+
1254+
private static Page<Disk> listDisks(final String zone, final ComputeOptions serviceOptions,
1255+
final Map<ComputeRpc.Option, ?> optionsMap) {
1256+
try {
1257+
ComputeRpc.Tuple<String, Iterable<com.google.api.services.compute.model.Disk>> result =
1258+
runWithRetries(new Callable<ComputeRpc.Tuple<String,
1259+
Iterable<com.google.api.services.compute.model.Disk>>>() {
1260+
@Override
1261+
public ComputeRpc.Tuple<String,
1262+
Iterable<com.google.api.services.compute.model.Disk>> call() {
1263+
return serviceOptions.rpc().listDisks(zone, optionsMap);
1264+
}
1265+
}, serviceOptions.retryParams(), EXCEPTION_HANDLER);
1266+
String cursor = result.x();
1267+
Iterable<Disk> disks = Iterables.transform(
1268+
result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Disk>of()
1269+
: result.y(), diskFromPb(serviceOptions));
1270+
return new PageImpl<>(new DiskPageFetcher(zone, serviceOptions, cursor, optionsMap),
1271+
cursor, disks);
1272+
} catch (RetryHelper.RetryHelperException e) {
1273+
throw ComputeException.translateAndThrow(e);
1274+
}
1275+
}
1276+
1277+
@Override
1278+
public Page<Disk> listDisks(DiskAggregatedListOption... options) {
1279+
return listDisks(options(), optionMap(options));
1280+
}
1281+
1282+
private static Page<Disk> listDisks(final ComputeOptions serviceOptions,
1283+
final Map<ComputeRpc.Option, ?> optionsMap) {
1284+
try {
1285+
ComputeRpc.Tuple<String, Iterable<com.google.api.services.compute.model.Disk>> result =
1286+
runWithRetries(new Callable<ComputeRpc.Tuple<String,
1287+
Iterable<com.google.api.services.compute.model.Disk>>>() {
1288+
@Override
1289+
public ComputeRpc.Tuple<String,
1290+
Iterable<com.google.api.services.compute.model.Disk>> call() {
1291+
return serviceOptions.rpc().listDisks(optionsMap);
1292+
}
1293+
}, serviceOptions.retryParams(), EXCEPTION_HANDLER);
1294+
String cursor = result.x();
1295+
Iterable<Disk> disks = Iterables.transform(
1296+
result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Disk>of()
1297+
: result.y(), diskFromPb(serviceOptions));
1298+
return new PageImpl<>(new AggregatedDiskPageFetcher(serviceOptions, cursor, optionsMap),
1299+
cursor, disks);
1300+
} catch (RetryHelper.RetryHelperException e) {
1301+
throw ComputeException.translateAndThrow(e);
1302+
}
1303+
}
1304+
1305+
@Override
1306+
public Operation delete(final DiskId disk, OperationOption... options) {
1307+
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
1308+
try {
1309+
com.google.api.services.compute.model.Operation answer =
1310+
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
1311+
@Override
1312+
public com.google.api.services.compute.model.Operation call() {
1313+
return computeRpc.deleteDisk(disk.zone(), disk.disk(), optionsMap);
1314+
}
1315+
}, options().retryParams(), EXCEPTION_HANDLER);
1316+
return answer == null ? null : Operation.fromPb(this, answer);
1317+
} catch (RetryHelper.RetryHelperException e) {
1318+
throw ComputeException.translateAndThrow(e);
1319+
}
1320+
}
1321+
11641322
private Map<ComputeRpc.Option, ?> optionMap(Option... options) {
11651323
Map<ComputeRpc.Option, Object> optionMap = Maps.newEnumMap(ComputeRpc.Option.class);
11661324
for (Option option : options) {

0 commit comments

Comments
 (0)