Skip to content

Commit

Permalink
Add functional methods for disks and Disk class
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Apr 7, 2016
1 parent 6780c0d commit f675ee5
Show file tree
Hide file tree
Showing 9 changed files with 1,945 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,54 @@ public static ImageFilter notEquals(ImageField field, long value) {
}
}

/**
* Class for filtering disk lists.
*/
class DiskFilter extends ListFilter {

private static final long serialVersionUID = 5856790665396877913L;

private DiskFilter(DiskField field, ComparisonOperator operator, Object value) {
super(field.selector(), operator, value);
}

/**
* Returns an equals filter for the given field and string value. For string fields,
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
* match the entire field.
*
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
*/
public static DiskFilter equals(DiskField field, String value) {
return new DiskFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
}

/**
* Returns a not-equals filter for the given field and string value. For string fields,
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
* match the entire field.
*
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
*/
public static DiskFilter notEquals(DiskField field, String value) {
return new DiskFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
}

/**
* Returns an equals filter for the given field and long value.
*/
public static DiskFilter equals(DiskField field, long value) {
return new DiskFilter(checkNotNull(field), ComparisonOperator.EQ, value);
}

/**
* Returns a not-equals filter for the given field and long value.
*/
public static DiskFilter notEquals(DiskField field, long value) {
return new DiskFilter(checkNotNull(field), ComparisonOperator.NE, value);
}
}

/**
* Class for specifying disk type get options.
*/
Expand Down Expand Up @@ -1585,6 +1633,110 @@ public static ImageListOption fields(ImageField... fields) {
}
}

/**
* Class for specifying disk get options.
*/
class DiskOption extends Option {

private static final long serialVersionUID = -4354796876226661667L;

private DiskOption(ComputeRpc.Option option, Object value) {
super(option, value);
}

/**
* Returns an option to specify the disk's fields to be returned by the RPC call. If this option
* is not provided, all disk's fields are returned. {@code DiskOption.fields} can be used to
* specify only the fields of interest. {@link Disk#diskId()},
* {@link DiskConfiguration#diskType()} and {@link SnapshotDiskConfiguration#sourceSnapshot()}
* or {@link ImageDiskConfiguration#sourceImage()} are always returned, even if not specified.
*/
public static DiskOption fields(DiskField... fields) {
return new DiskOption(ComputeRpc.Option.FIELDS, DiskField.selector(fields));
}
}

/**
* Class for specifying disk list options.
*/
class DiskListOption extends Option {

private static final long serialVersionUID = -5148497888688645905L;

private DiskListOption(ComputeRpc.Option option, Object value) {
super(option, value);
}

/**
* Returns an option to specify a filter on the disks being listed.
*/
public static DiskListOption filter(DiskFilter filter) {
return new DiskListOption(ComputeRpc.Option.FILTER, filter.toPb());
}

/**
* Returns an option to specify the maximum number of disks returned per page. {@code pageSize}
* must be between 0 and 500 (inclusive). If not specified 500 is used.
*/
public static DiskListOption pageSize(long pageSize) {
return new DiskListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
}

/**
* Returns an option to specify the page token from which to start listing disks.
*/
public static DiskListOption pageToken(String pageToken) {
return new DiskListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
}

/**
* Returns an option to specify the disk's fields to be returned by the RPC call. If this option
* is not provided, all disk's fields are returned. {@code DiskListOption.fields} can be used to
* specify only the fields of interest. {@link Disk#diskId()},
* {@link DiskConfiguration#diskType()} and {@link SnapshotDiskConfiguration#sourceSnapshot()}
* or {@link ImageDiskConfiguration#sourceImage()} are always returned, even if not specified.
*/
public static DiskListOption fields(DiskField... fields) {
StringBuilder builder = new StringBuilder();
builder.append("items(").append(DiskField.selector(fields)).append("),nextPageToken");
return new DiskListOption(ComputeRpc.Option.FIELDS, builder.toString());
}
}

/**
* Class for specifying disk aggregated list options.
*/
class DiskAggregatedListOption extends Option {

private static final long serialVersionUID = 1163784797870242766L;

private DiskAggregatedListOption(ComputeRpc.Option option, Object value) {
super(option, value);
}

/**
* Returns an option to specify a filter on the disks being listed.
*/
public static DiskAggregatedListOption filter(DiskFilter filter) {
return new DiskAggregatedListOption(ComputeRpc.Option.FILTER, filter.toPb());
}

/**
* Returns an option to specify the maximum number of disks returned per page. {@code pageSize}
* must be between 0 and 500 (inclusive). If not specified 500 is used.
*/
public static DiskAggregatedListOption pageSize(long pageSize) {
return new DiskAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
}

/**
* Returns an option to specify the page token from which to start listing disks.
*/
public static DiskAggregatedListOption pageToken(String pageToken) {
return new DiskAggregatedListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
}
}

/**
* Returns the requested disk type or {@code null} if not found.
*
Expand Down Expand Up @@ -1868,4 +2020,42 @@ public static ImageListOption fields(ImageField... fields) {
*/
Operation deprecate(ImageId image, DeprecationStatus<ImageId> deprecationStatus,
OperationOption... options);

/**
* Returns the requested disk or {@code null} if not found.
*
* @throws ComputeException upon failure
*/
Disk get(DiskId diskId, DiskOption... options);

/**
* Creates a new disk.
*
* @return a zone operation for disk's creation
* @throws ComputeException upon failure
*/
Operation create(DiskInfo disk, OperationOption... options);

/**
* Lists disks for the provided zone.
*
* @throws ComputeException upon failure
*/
Page<Disk> listDisks(String zone, DiskListOption... options);

/**
* Lists disks for all zones.
*
* @throws ComputeException upon failure
*/
Page<Disk> listDisks(DiskAggregatedListOption... options);

/**
* Deletes the requested disk.
*
* @return a zone operation if the request was issued correctly, {@code null} if the disk was not
* found
* @throws ComputeException upon failure
*/
Operation delete(DiskId disk, OperationOption... options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,46 @@ public Page<Image> nextPage() {
}
}

private static class DiskPageFetcher implements NextPageFetcher<Disk> {

private static final long serialVersionUID = 4146589787872718476L;
private final Map<ComputeRpc.Option, ?> requestOptions;
private final ComputeOptions serviceOptions;
private final String zone;

DiskPageFetcher(String zone, ComputeOptions serviceOptions, String cursor,
Map<ComputeRpc.Option, ?> optionMap) {
this.requestOptions =
PageImpl.nextRequestOptions(ComputeRpc.Option.PAGE_TOKEN, cursor, optionMap);
this.serviceOptions = serviceOptions;
this.zone = zone;
}

@Override
public Page<Disk> nextPage() {
return listDisks(zone, serviceOptions, requestOptions);
}
}

private static class AggregatedDiskPageFetcher implements NextPageFetcher<Disk> {

private static final long serialVersionUID = -5240045334115926206L;
private final Map<ComputeRpc.Option, ?> requestOptions;
private final ComputeOptions serviceOptions;

AggregatedDiskPageFetcher(ComputeOptions serviceOptions, String cursor,
Map<ComputeRpc.Option, ?> optionMap) {
this.requestOptions =
PageImpl.nextRequestOptions(ComputeRpc.Option.PAGE_TOKEN, cursor, optionMap);
this.serviceOptions = serviceOptions;
}

@Override
public Page<Disk> nextPage() {
return listDisks(serviceOptions, requestOptions);
}
}

private final ComputeRpc computeRpc;

ComputeImpl(ComputeOptions options) {
Expand Down Expand Up @@ -1161,6 +1201,124 @@ public com.google.api.services.compute.model.Operation call() {
}
}

@Override
public Disk get(final DiskId diskId, DiskOption... options) {
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
try {
com.google.api.services.compute.model.Disk answer =
runWithRetries(new Callable<com.google.api.services.compute.model.Disk>() {
@Override
public com.google.api.services.compute.model.Disk call() {
return computeRpc.getDisk(diskId.zone(), diskId.disk(), optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : Disk.fromPb(this, answer);
} catch (RetryHelper.RetryHelperException e) {
throw ComputeException.translateAndThrow(e);
}
}

@Override
public Operation create(final DiskInfo disk, OperationOption... options) {
final com.google.api.services.compute.model.Disk diskPb =
disk.setProjectId(options().projectId()).toPb();
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
try {
return Operation.fromPb(this,
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
@Override
public com.google.api.services.compute.model.Operation call() {
return computeRpc.createDisk(disk.diskId().zone(), diskPb, optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER));
} catch (RetryHelper.RetryHelperException e) {
throw ComputeException.translateAndThrow(e);
}
}

private static Function<com.google.api.services.compute.model.Disk, Disk> diskFromPb(
final ComputeOptions serviceOptions) {
return new Function<com.google.api.services.compute.model.Disk, Disk>() {
@Override
public Disk apply(com.google.api.services.compute.model.Disk disk) {
return Disk.fromPb(serviceOptions.service(), disk);
}
};
}

@Override
public Page<Disk> listDisks(String zone, DiskListOption... options) {
return listDisks(zone, options(), optionMap(options));
}

private static Page<Disk> listDisks(final String zone, final ComputeOptions serviceOptions,
final Map<ComputeRpc.Option, ?> optionsMap) {
try {
ComputeRpc.Tuple<String, Iterable<com.google.api.services.compute.model.Disk>> result =
runWithRetries(new Callable<ComputeRpc.Tuple<String,
Iterable<com.google.api.services.compute.model.Disk>>>() {
@Override
public ComputeRpc.Tuple<String,
Iterable<com.google.api.services.compute.model.Disk>> call() {
return serviceOptions.rpc().listDisks(zone, optionsMap);
}
}, serviceOptions.retryParams(), EXCEPTION_HANDLER);
String cursor = result.x();
Iterable<Disk> disks = Iterables.transform(
result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Disk>of()
: result.y(), diskFromPb(serviceOptions));
return new PageImpl<>(new DiskPageFetcher(zone, serviceOptions, cursor, optionsMap),
cursor, disks);
} catch (RetryHelper.RetryHelperException e) {
throw ComputeException.translateAndThrow(e);
}
}

@Override
public Page<Disk> listDisks(DiskAggregatedListOption... options) {
return listDisks(options(), optionMap(options));
}

private static Page<Disk> listDisks(final ComputeOptions serviceOptions,
final Map<ComputeRpc.Option, ?> optionsMap) {
try {
ComputeRpc.Tuple<String, Iterable<com.google.api.services.compute.model.Disk>> result =
runWithRetries(new Callable<ComputeRpc.Tuple<String,
Iterable<com.google.api.services.compute.model.Disk>>>() {
@Override
public ComputeRpc.Tuple<String,
Iterable<com.google.api.services.compute.model.Disk>> call() {
return serviceOptions.rpc().listDisks(optionsMap);
}
}, serviceOptions.retryParams(), EXCEPTION_HANDLER);
String cursor = result.x();
Iterable<Disk> disks = Iterables.transform(
result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Disk>of()
: result.y(), diskFromPb(serviceOptions));
return new PageImpl<>(new AggregatedDiskPageFetcher(serviceOptions, cursor, optionsMap),
cursor, disks);
} catch (RetryHelper.RetryHelperException e) {
throw ComputeException.translateAndThrow(e);
}
}

@Override
public Operation delete(final DiskId disk, OperationOption... options) {
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
try {
com.google.api.services.compute.model.Operation answer =
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
@Override
public com.google.api.services.compute.model.Operation call() {
return computeRpc.deleteDisk(disk.zone(), disk.disk(), optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : Operation.fromPb(this, answer);
} catch (RetryHelper.RetryHelperException e) {
throw ComputeException.translateAndThrow(e);
}
}

private Map<ComputeRpc.Option, ?> optionMap(Option... options) {
Map<ComputeRpc.Option, Object> optionMap = Maps.newEnumMap(ComputeRpc.Option.class);
for (Option option : options) {
Expand Down
Loading

0 comments on commit f675ee5

Please sign in to comment.