Skip to content

Commit 0358d58

Browse files
committed
Refactor compute operations
- 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
1 parent ef98269 commit 0358d58

File tree

10 files changed

+117
-66
lines changed

10 files changed

+117
-66
lines changed

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

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ abstract class ListFilter implements Serializable {
288288

289289
enum ComparisonOperator {
290290
/**
291-
* Defines an equality filter.
291+
* Defines an equals filter.
292292
*/
293293
EQ,
294294

@@ -340,11 +340,11 @@ class DiskTypeFilter extends ListFilter {
340340
}
341341

342342
/**
343-
* Returns an equality filter for the given field and string value. For string fields,
343+
* Returns an equals filter for the given field and string value. For string fields,
344344
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
345345
* match the entire field.
346346
*
347-
* @see <a href="https://github.com/google/re2">RE2</a>
347+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
348348
*/
349349
public static DiskTypeFilter equals(DiskTypeField field, String value) {
350350
return new DiskTypeFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
@@ -355,14 +355,14 @@ public static DiskTypeFilter equals(DiskTypeField field, String value) {
355355
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
356356
* match the entire field.
357357
*
358-
* @see <a href="https://github.com/google/re2">RE2</a>
358+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
359359
*/
360360
public static DiskTypeFilter notEquals(DiskTypeField field, String value) {
361361
return new DiskTypeFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
362362
}
363363

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

390390
/**
391-
* Returns an equality filter for the given field and string value. For string fields,
391+
* Returns an equals filter for the given field and string value. For string fields,
392392
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
393393
* match the entire field.
394394
*
395-
* @see <a href="https://github.com/google/re2">RE2</a>
395+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
396396
*/
397397
public static MachineTypeFilter equals(MachineTypeField field, String value) {
398398
return new MachineTypeFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
@@ -403,14 +403,14 @@ public static MachineTypeFilter equals(MachineTypeField field, String value) {
403403
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
404404
* match the entire field.
405405
*
406-
* @see <a href="https://github.com/google/re2">RE2</a>
406+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
407407
*/
408408
public static MachineTypeFilter notEquals(MachineTypeField field, String value) {
409409
return new MachineTypeFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
410410
}
411411

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

438438
/**
439-
* Returns an equality filter for the given field and string value. For string fields,
439+
* Returns an equals filter for the given field and string value. For string fields,
440440
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
441441
* match the entire field.
442442
*
443-
* @see <a href="https://github.com/google/re2">RE2</a>
443+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
444444
*/
445445
public static RegionFilter equals(RegionField field, String value) {
446446
return new RegionFilter(checkNotNull(field), ComparisonOperator.EQ, value);
@@ -451,7 +451,7 @@ public static RegionFilter equals(RegionField field, String value) {
451451
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
452452
* match the entire field.
453453
*
454-
* @see <a href="https://github.com/google/re2">RE2</a>
454+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
455455
*/
456456
public static RegionFilter notEquals(RegionField field, String value) {
457457
return new RegionFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
@@ -470,11 +470,11 @@ class ZoneFilter extends ListFilter {
470470
}
471471

472472
/**
473-
* Returns an equality filter for the given field and string value. For string fields,
473+
* Returns an equals filter for the given field and string value. For string fields,
474474
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
475475
* match the entire field.
476476
*
477-
* @see <a href="https://github.com/google/re2">RE2</a>
477+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
478478
*/
479479
public static ZoneFilter equals(ZoneField field, String value) {
480480
return new ZoneFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
@@ -485,7 +485,7 @@ public static ZoneFilter equals(ZoneField field, String value) {
485485
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
486486
* match the entire field.
487487
*
488-
* @see <a href="https://github.com/google/re2">RE2</a>
488+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
489489
*/
490490
public static ZoneFilter notEquals(ZoneField field, String value) {
491491
return new ZoneFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
@@ -504,11 +504,11 @@ class OperationFilter extends ListFilter {
504504
}
505505

506506
/**
507-
* Returns an equality filter for the given field and string value. For string fields,
507+
* Returns an equals filter for the given field and string value. For string fields,
508508
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
509509
* match the entire field.
510510
*
511-
* @see <a href="https://github.com/google/re2">RE2</a>
511+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
512512
*/
513513
public static OperationFilter equals(OperationField field, String value) {
514514
return new OperationFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
@@ -519,14 +519,14 @@ public static OperationFilter equals(OperationField field, String value) {
519519
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
520520
* match the entire field.
521521
*
522-
* @see <a href="https://github.com/google/re2">RE2</a>
522+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
523523
*/
524524
public static OperationFilter notEquals(OperationField field, String value) {
525525
return new OperationFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
526526
}
527527

528528
/**
529-
* Returns an equality filter for the given field and long value.
529+
* Returns an equals filter for the given field and long value.
530530
*/
531531
public static OperationFilter equals(OperationField field, long value) {
532532
return new OperationFilter(checkNotNull(field), ComparisonOperator.EQ, value);
@@ -538,20 +538,6 @@ public static OperationFilter equals(OperationField field, long value) {
538538
public static OperationFilter notEquals(OperationField field, long value) {
539539
return new OperationFilter(checkNotNull(field), ComparisonOperator.NE, value);
540540
}
541-
542-
/**
543-
* Returns an equality filter for the given field and integer value.
544-
*/
545-
public static OperationFilter equals(OperationField field, int value) {
546-
return new OperationFilter(checkNotNull(field), ComparisonOperator.EQ, value);
547-
}
548-
549-
/**
550-
* Returns a not-equals filter for the given field and integer value.
551-
*/
552-
public static OperationFilter notEquals(OperationField field, int value) {
553-
return new OperationFilter(checkNotNull(field), ComparisonOperator.NE, value);
554-
}
555541
}
556542

557543
/**
@@ -595,7 +581,7 @@ public static DiskTypeListOption filter(DiskTypeFilter filter) {
595581
}
596582

597583
/**
598-
* Returns an option to specify the maximum number of disk types to be returned.
584+
* Returns an option to specify the maximum number of disk types returned per page.
599585
*/
600586
public static DiskTypeListOption pageSize(long pageSize) {
601587
return new DiskTypeListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -640,7 +626,7 @@ public static DiskTypeAggregatedListOption filter(DiskTypeFilter filter) {
640626
}
641627

642628
/**
643-
* Returns an option to specify the maximum number of disk types to be returned.
629+
* Returns an option to specify the maximum number of disk types returned per page.
644630
*/
645631
public static DiskTypeAggregatedListOption pageSize(long pageSize) {
646632
return new DiskTypeAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -695,7 +681,7 @@ public static MachineTypeListOption filter(MachineTypeFilter filter) {
695681
}
696682

697683
/**
698-
* Returns an option to specify the maximum number of machine types to be returned.
684+
* Returns an option to specify the maximum number of machine types returned per page.
699685
*/
700686
public static MachineTypeListOption pageSize(long pageSize) {
701687
return new MachineTypeListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -740,7 +726,7 @@ public static MachineTypeAggregatedListOption filter(MachineTypeFilter filter) {
740726
}
741727

742728
/**
743-
* Returns an option to specify the maximum number of machine types to be returned.
729+
* Returns an option to specify the maximum number of machine types returned per page.
744730
*/
745731
public static MachineTypeAggregatedListOption pageSize(long pageSize) {
746732
return new MachineTypeAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -795,7 +781,7 @@ public static RegionListOption filter(RegionFilter filter) {
795781
}
796782

797783
/**
798-
* Returns an option to specify the maximum number of regions to be returned.
784+
* Returns an option to specify the maximum number of regions returned per page.
799785
*/
800786
public static RegionListOption pageSize(long pageSize) {
801787
return new RegionListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -862,7 +848,7 @@ public static ZoneListOption filter(ZoneFilter filter) {
862848
}
863849

864850
/**
865-
* Returns an option to specify the maximum number of zones to be returned.
851+
* Returns an option to specify the maximum number of zones returned per page.
866852
*/
867853
public static ZoneListOption pageSize(long pageSize) {
868854
return new ZoneListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -951,7 +937,7 @@ public static OperationListOption filter(OperationFilter filter) {
951937
}
952938

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

10921078
/**
1093-
* Lists the operations in the provided region.
1079+
* Lists the operations for the provided region. These are operations that create/modify/delete
1080+
* resources that live in a region (e.g. subnetworks).
10941081
*
10951082
* @throws ComputeException upon failure
10961083
*/
10971084
Page<Operation> listRegionOperations(String region, OperationListOption... options);
10981085

10991086
/**
1100-
* Lists the operations in the provided zone.
1087+
* Lists the operations for the provided zone. These are operations that create/modify/delete
1088+
* resources that live in a zone (e.g. instances).
11011089
*
11021090
* @throws ComputeException upon failure
11031091
*/

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -533,16 +533,17 @@ public Operation get(final OperationId operationId, OperationOption... options)
533533
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
534534
@Override
535535
public com.google.api.services.compute.model.Operation call() {
536-
if (operationId instanceof RegionOperationId) {
537-
RegionOperationId regionOperationId = (RegionOperationId) operationId;
538-
return computeRpc.getRegionOperation(regionOperationId.region(),
539-
regionOperationId.operation(), optionsMap);
540-
} else if (operationId instanceof ZoneOperationId) {
541-
ZoneOperationId zoneOperationId = (ZoneOperationId) operationId;
542-
return computeRpc.getZoneOperation(zoneOperationId.zone(),
543-
zoneOperationId.operation(), optionsMap);
544-
} else {
545-
return computeRpc.getGlobalOperation(operationId.operation(), optionsMap);
536+
switch (operationId.type()) {
537+
case REGION:
538+
RegionOperationId regionOperationId = (RegionOperationId) operationId;
539+
return computeRpc.getRegionOperation(regionOperationId.region(),
540+
regionOperationId.operation(), optionsMap);
541+
case ZONE:
542+
ZoneOperationId zoneOperationId = (ZoneOperationId) operationId;
543+
return computeRpc.getZoneOperation(zoneOperationId.zone(),
544+
zoneOperationId.operation(), optionsMap);
545+
default:
546+
return computeRpc.getGlobalOperation(operationId.operation(), optionsMap);
546547
}
547548
}
548549
}, options().retryParams(), EXCEPTION_HANDLER);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ private GlobalOperationId(String project, String operation) {
4040
this.operation = checkNotNull(operation);
4141
}
4242

43+
@Override
44+
public Type type() {
45+
return Type.GLOBAL;
46+
}
47+
4348
@Override
4449
public String operation() {
4550
return operation;

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.google.gcloud.compute;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static com.google.gcloud.compute.OperationId.Type.REGION;
21+
import static com.google.gcloud.compute.OperationId.Type.ZONE;
2022

2123
import com.google.common.base.Function;
2224
import com.google.common.base.MoreObjects;
@@ -43,7 +45,7 @@
4345
* To get an {@code Operation} object with the most recent information, use
4446
* {@link #reload(Compute.OperationOption...)}.
4547
*/
46-
public final class Operation implements Serializable {
48+
public class Operation implements Serializable {
4749

4850
private static final long serialVersionUID = -8979001444590023899L;
4951
private static final DateTimeFormatter TIMESTAMP_FORMATTER = ISODateTimeFormat.dateTime();
@@ -124,7 +126,7 @@ public String code() {
124126
}
125127

126128
/**
127-
* Returns the field in the request which caused the error. Might be {@code null}.
129+
* Returns the field in the request which caused the error. This value is optional.
128130
*/
129131
public String location() {
130132
return location;
@@ -213,7 +215,8 @@ public com.google.api.services.compute.model.Operation.Warnings apply(
213215
}
214216

215217
/**
216-
* Returns a warning identifier for this warning.
218+
* Returns a warning identifier for this warning. For example, {@code NO_RESULTS_ON_PAGE} if
219+
* there are no results in the response.
217220
*/
218221
public String code() {
219222
return code;
@@ -227,7 +230,12 @@ public String message() {
227230
}
228231

229232
/**
230-
* Returns metadata about this warning.
233+
* Returns metadata about this warning. Each key provides more detail on the warning being
234+
* returned. For example, for warnings where there are no results in a list request for a
235+
* particular zone, this key might be {@code scope} and the key's value might be the zone name.
236+
* Other examples might be a key indicating a deprecated resource, and a suggested replacement,
237+
* or a warning about invalid network settings (for example, if an instance attempts to perform
238+
* IP forwarding but is not enabled for IP forwarding).
231239
*/
232240
public Map<String, String> metadata() {
233241
return metadata;
@@ -587,13 +595,15 @@ public Long insertTime() {
587595

588596
/**
589597
* Returns the time that this operation was started by the service. In milliseconds since epoch.
598+
* This value will be {@code null} if the operation has not started yet.
590599
*/
591600
public Long startTime() {
592601
return startTime;
593602
}
594603

595604
/**
596-
* Returns the time that this operation was completed. In milliseconds since epoch.
605+
* Returns the time that this operation was completed. In milliseconds since epoch. This value
606+
* will be {@code null} if the operation has not finished yet.
597607
*/
598608
public Long endTime() {
599609
return endTime;
@@ -717,12 +727,12 @@ public String toString() {
717727
}
718728

719729
@Override
720-
public int hashCode() {
730+
public final int hashCode() {
721731
return Objects.hash(id);
722732
}
723733

724734
@Override
725-
public boolean equals(Object obj) {
735+
public final boolean equals(Object obj) {
726736
return obj instanceof Operation
727737
&& Objects.equals(toPb(), ((Operation) obj).toPb())
728738
&& Objects.equals(options, ((Operation) obj).options);
@@ -739,11 +749,13 @@ com.google.api.services.compute.model.Operation toPb() {
739749
}
740750
operationPb.setName(operationId.operation());
741751
operationPb.setClientOperationId(clientOperationId);
742-
if (operationId instanceof RegionOperationId) {
743-
operationPb.setRegion(this.<RegionOperationId>operationId().regionId().selfLink());
744-
}
745-
if (operationId instanceof ZoneOperationId) {
746-
operationPb.setZone(this.<ZoneOperationId>operationId().zoneId().selfLink());
752+
switch (operationId.type()) {
753+
case REGION:
754+
operationPb.setRegion(this.<RegionOperationId>operationId().regionId().selfLink());
755+
break;
756+
case ZONE:
757+
operationPb.setZone(this.<ZoneOperationId>operationId().zoneId().selfLink());
758+
break;
747759
}
748760
if (operationType != null) {
749761
operationPb.setOperationType(operationType);

0 commit comments

Comments
 (0)