Skip to content

Commit

Permalink
Refactor compute operations
Browse files Browse the repository at this point in the history
- Add Type enum to OperationId and type() getter
- Replace instanceof with switch on type()
- Add better javadoc to Operation
- Remove final from Operation, make hashCode and equals final
  • Loading branch information
mziccard committed Mar 15, 2016
1 parent ef98269 commit c41ad14
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ abstract class ListFilter implements Serializable {

enum ComparisonOperator {
/**
* Defines an equality filter.
* Defines an equals filter.
*/
EQ,

Expand Down Expand Up @@ -340,11 +340,11 @@ class DiskTypeFilter extends ListFilter {
}

/**
* Returns an equality filter for the given field and string value. For string fields,
* 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">RE2</a>
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
*/
public static DiskTypeFilter equals(DiskTypeField field, String value) {
return new DiskTypeFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
Expand All @@ -355,14 +355,14 @@ public static DiskTypeFilter equals(DiskTypeField field, String value) {
* {@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">RE2</a>
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
*/
public static DiskTypeFilter notEquals(DiskTypeField field, String value) {
return new DiskTypeFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
}

/**
* Returns an equality filter for the given field and long value.
* Returns an equals filter for the given field and long value.
*/
public static DiskTypeFilter equals(DiskTypeField field, long value) {
return new DiskTypeFilter(checkNotNull(field), ComparisonOperator.EQ, value);
Expand All @@ -388,11 +388,11 @@ class MachineTypeFilter extends ListFilter {
}

/**
* Returns an equality filter for the given field and string value. For string fields,
* 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">RE2</a>
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
*/
public static MachineTypeFilter equals(MachineTypeField field, String value) {
return new MachineTypeFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
Expand All @@ -403,14 +403,14 @@ public static MachineTypeFilter equals(MachineTypeField field, String value) {
* {@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">RE2</a>
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
*/
public static MachineTypeFilter notEquals(MachineTypeField field, String value) {
return new MachineTypeFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
}

/**
* Returns an equality filter for the given field and long value.
* Returns an equals filter for the given field and long value.
*/
public static MachineTypeFilter equals(MachineTypeField field, long value) {
return new MachineTypeFilter(checkNotNull(field), ComparisonOperator.EQ, value);
Expand All @@ -436,11 +436,11 @@ class RegionFilter extends ListFilter {
}

/**
* Returns an equality filter for the given field and string value. For string fields,
* 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">RE2</a>
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
*/
public static RegionFilter equals(RegionField field, String value) {
return new RegionFilter(checkNotNull(field), ComparisonOperator.EQ, value);
Expand All @@ -451,7 +451,7 @@ public static RegionFilter equals(RegionField field, String value) {
* {@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">RE2</a>
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
*/
public static RegionFilter notEquals(RegionField field, String value) {
return new RegionFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
Expand All @@ -470,11 +470,11 @@ class ZoneFilter extends ListFilter {
}

/**
* Returns an equality filter for the given field and string value. For string fields,
* 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">RE2</a>
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
*/
public static ZoneFilter equals(ZoneField field, String value) {
return new ZoneFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
Expand All @@ -485,7 +485,7 @@ public static ZoneFilter equals(ZoneField field, String value) {
* {@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">RE2</a>
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
*/
public static ZoneFilter notEquals(ZoneField field, String value) {
return new ZoneFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
Expand All @@ -504,11 +504,11 @@ class OperationFilter extends ListFilter {
}

/**
* Returns an equality filter for the given field and string value. For string fields,
* 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">RE2</a>
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
*/
public static OperationFilter equals(OperationField field, String value) {
return new OperationFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
Expand All @@ -519,14 +519,14 @@ public static OperationFilter equals(OperationField field, String value) {
* {@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">RE2</a>
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
*/
public static OperationFilter notEquals(OperationField field, String value) {
return new OperationFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
}

/**
* Returns an equality filter for the given field and long value.
* Returns an equals filter for the given field and long value.
*/
public static OperationFilter equals(OperationField field, long value) {
return new OperationFilter(checkNotNull(field), ComparisonOperator.EQ, value);
Expand All @@ -538,20 +538,6 @@ public static OperationFilter equals(OperationField field, long value) {
public static OperationFilter notEquals(OperationField field, long value) {
return new OperationFilter(checkNotNull(field), ComparisonOperator.NE, value);
}

/**
* Returns an equality filter for the given field and integer value.
*/
public static OperationFilter equals(OperationField field, int value) {
return new OperationFilter(checkNotNull(field), ComparisonOperator.EQ, value);
}

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

/**
Expand Down Expand Up @@ -595,7 +581,7 @@ public static DiskTypeListOption filter(DiskTypeFilter filter) {
}

/**
* Returns an option to specify the maximum number of disk types to be returned.
* Returns an option to specify the maximum number of disk types returned per page.
*/
public static DiskTypeListOption pageSize(long pageSize) {
return new DiskTypeListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
Expand Down Expand Up @@ -640,7 +626,7 @@ public static DiskTypeAggregatedListOption filter(DiskTypeFilter filter) {
}

/**
* Returns an option to specify the maximum number of disk types to be returned.
* Returns an option to specify the maximum number of disk types returned per page.
*/
public static DiskTypeAggregatedListOption pageSize(long pageSize) {
return new DiskTypeAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
Expand Down Expand Up @@ -695,7 +681,7 @@ public static MachineTypeListOption filter(MachineTypeFilter filter) {
}

/**
* Returns an option to specify the maximum number of machine types to be returned.
* Returns an option to specify the maximum number of machine types returned per page.
*/
public static MachineTypeListOption pageSize(long pageSize) {
return new MachineTypeListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
Expand Down Expand Up @@ -740,7 +726,7 @@ public static MachineTypeAggregatedListOption filter(MachineTypeFilter filter) {
}

/**
* Returns an option to specify the maximum number of machine types to be returned.
* Returns an option to specify the maximum number of machine types returned per page.
*/
public static MachineTypeAggregatedListOption pageSize(long pageSize) {
return new MachineTypeAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
Expand Down Expand Up @@ -795,7 +781,7 @@ public static RegionListOption filter(RegionFilter filter) {
}

/**
* Returns an option to specify the maximum number of regions to be returned.
* Returns an option to specify the maximum number of regions returned per page.
*/
public static RegionListOption pageSize(long pageSize) {
return new RegionListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
Expand Down Expand Up @@ -862,7 +848,7 @@ public static ZoneListOption filter(ZoneFilter filter) {
}

/**
* Returns an option to specify the maximum number of zones to be returned.
* Returns an option to specify the maximum number of zones returned per page.
*/
public static ZoneListOption pageSize(long pageSize) {
return new ZoneListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
Expand Down Expand Up @@ -951,7 +937,7 @@ public static OperationListOption filter(OperationFilter filter) {
}

/**
* Returns an option to specify the maximum number of operations to be returned.
* Returns an option to specify the maximum number of operations returned per page.
*/
public static OperationListOption pageSize(long pageSize) {
return new OperationListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
Expand Down Expand Up @@ -1090,14 +1076,16 @@ public static OperationListOption fields(OperationField... fields) {
Page<Operation> listGlobalOperations(OperationListOption... options);

/**
* Lists the operations in the provided region.
* Lists the operations for the provided region. These are operations that create/modify/delete
* resources that live in a region (e.g. subnetworks).
*
* @throws ComputeException upon failure
*/
Page<Operation> listRegionOperations(String region, OperationListOption... options);

/**
* Lists the operations in the provided zone.
* Lists the operations for the provided zone. These are operations that create/modify/delete
* resources that live in a zone (e.g. instances).
*
* @throws ComputeException upon failure
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,16 +533,17 @@ public Operation get(final OperationId operationId, OperationOption... options)
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
@Override
public com.google.api.services.compute.model.Operation call() {
if (operationId instanceof RegionOperationId) {
RegionOperationId regionOperationId = (RegionOperationId) operationId;
return computeRpc.getRegionOperation(regionOperationId.region(),
regionOperationId.operation(), optionsMap);
} else if (operationId instanceof ZoneOperationId) {
ZoneOperationId zoneOperationId = (ZoneOperationId) operationId;
return computeRpc.getZoneOperation(zoneOperationId.zone(),
zoneOperationId.operation(), optionsMap);
} else {
return computeRpc.getGlobalOperation(operationId.operation(), optionsMap);
switch (operationId.type()) {
case REGION:
RegionOperationId regionOperationId = (RegionOperationId) operationId;
return computeRpc.getRegionOperation(regionOperationId.region(),
regionOperationId.operation(), optionsMap);
case ZONE:
ZoneOperationId zoneOperationId = (ZoneOperationId) operationId;
return computeRpc.getZoneOperation(zoneOperationId.zone(),
zoneOperationId.operation(), optionsMap);
default:
return computeRpc.getGlobalOperation(operationId.operation(), optionsMap);
}
}
}, options().retryParams(), EXCEPTION_HANDLER);
Expand Down Expand Up @@ -660,16 +661,17 @@ public boolean delete(final OperationId operation) {
return runWithRetries(new Callable<Boolean>() {
@Override
public Boolean call() {
if (operation instanceof RegionOperationId) {
RegionOperationId regionOperationId = (RegionOperationId) operation;
return computeRpc.deleteRegionOperation(regionOperationId.region(),
regionOperationId.operation());
} else if (operation instanceof ZoneOperationId) {
ZoneOperationId zoneOperationId = (ZoneOperationId) operation;
return computeRpc.deleteZoneOperation(zoneOperationId.zone(),
zoneOperationId.operation());
} else {
return computeRpc.deleteGlobalOperation(operation.operation());
switch (operation.type()) {
case REGION:
RegionOperationId regionOperationId = (RegionOperationId) operation;
return computeRpc.deleteRegionOperation(regionOperationId.region(),
regionOperationId.operation());
case ZONE:
ZoneOperationId zoneOperationId = (ZoneOperationId) operation;
return computeRpc.deleteZoneOperation(zoneOperationId.zone(),
zoneOperationId.operation());
default:
return computeRpc.deleteGlobalOperation(operation.operation());
}
}
}, options().retryParams(), EXCEPTION_HANDLER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ private GlobalOperationId(String project, String operation) {
this.operation = checkNotNull(operation);
}

@Override
public Type type() {
return Type.GLOBAL;
}

@Override
public String operation() {
return operation;
Expand Down
Loading

0 comments on commit c41ad14

Please sign in to comment.