|
35 | 35 |
|
36 | 36 | final class ComputeImpl extends BaseService<ComputeOptions> implements Compute { |
37 | 37 |
|
| 38 | + private static class GlobalOperationPageFetcher implements NextPageFetcher<Operation> { |
| 39 | + |
| 40 | + private static final long serialVersionUID = -2488912172182315364L; |
| 41 | + private final Map<ComputeRpc.Option, ?> requestOptions; |
| 42 | + private final ComputeOptions serviceOptions; |
| 43 | + |
| 44 | + GlobalOperationPageFetcher(ComputeOptions serviceOptions, String cursor, |
| 45 | + Map<ComputeRpc.Option, ?> optionMap) { |
| 46 | + this.requestOptions = |
| 47 | + PageImpl.nextRequestOptions(ComputeRpc.Option.PAGE_TOKEN, cursor, optionMap); |
| 48 | + this.serviceOptions = serviceOptions; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Page<Operation> nextPage() { |
| 53 | + return listGlobalOperations(serviceOptions, requestOptions); |
| 54 | + } |
| 55 | + } |
| 56 | + |
38 | 57 | private static class DiskTypePageFetcher implements NextPageFetcher<DiskType> { |
39 | 58 |
|
40 | 59 | private static final long serialVersionUID = -5253916264932522976L; |
@@ -153,6 +172,48 @@ public Page<Zone> nextPage() { |
153 | 172 | } |
154 | 173 | } |
155 | 174 |
|
| 175 | + private static class RegionOperationPageFetcher implements NextPageFetcher<Operation> { |
| 176 | + |
| 177 | + private static final long serialVersionUID = 4111705358926164078L; |
| 178 | + private final Map<ComputeRpc.Option, ?> requestOptions; |
| 179 | + private final ComputeOptions serviceOptions; |
| 180 | + private final String region; |
| 181 | + |
| 182 | + RegionOperationPageFetcher(String region, ComputeOptions serviceOptions, String cursor, |
| 183 | + Map<ComputeRpc.Option, ?> optionMap) { |
| 184 | + this.requestOptions = |
| 185 | + PageImpl.nextRequestOptions(ComputeRpc.Option.PAGE_TOKEN, cursor, optionMap); |
| 186 | + this.serviceOptions = serviceOptions; |
| 187 | + this.region = region; |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public Page<Operation> nextPage() { |
| 192 | + return listRegionOperations(region, serviceOptions, requestOptions); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private static class ZoneOperationPageFetcher implements NextPageFetcher<Operation> { |
| 197 | + |
| 198 | + private static final long serialVersionUID = -9012504536518197793L; |
| 199 | + private final Map<ComputeRpc.Option, ?> requestOptions; |
| 200 | + private final ComputeOptions serviceOptions; |
| 201 | + private final String zone; |
| 202 | + |
| 203 | + ZoneOperationPageFetcher(String zone, ComputeOptions serviceOptions, String cursor, |
| 204 | + Map<ComputeRpc.Option, ?> optionMap) { |
| 205 | + this.requestOptions = |
| 206 | + PageImpl.nextRequestOptions(ComputeRpc.Option.PAGE_TOKEN, cursor, optionMap); |
| 207 | + this.serviceOptions = serviceOptions; |
| 208 | + this.zone = zone; |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public Page<Operation> nextPage() { |
| 213 | + return listZoneOperations(zone, serviceOptions, requestOptions); |
| 214 | + } |
| 215 | + } |
| 216 | + |
156 | 217 | private final ComputeRpc computeRpc; |
157 | 218 |
|
158 | 219 | ComputeImpl(ComputeOptions options) { |
@@ -464,6 +525,165 @@ public com.google.api.services.compute.model.License call() { |
464 | 525 | } |
465 | 526 | } |
466 | 527 |
|
| 528 | + @Override |
| 529 | + public Operation get(final OperationId operationId, OperationOption... options) { |
| 530 | + final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options); |
| 531 | + try { |
| 532 | + com.google.api.services.compute.model.Operation answer = |
| 533 | + runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() { |
| 534 | + @Override |
| 535 | + public com.google.api.services.compute.model.Operation call() { |
| 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 | + case GLOBAL: |
| 546 | + return computeRpc.getGlobalOperation(operationId.operation(), optionsMap); |
| 547 | + default: |
| 548 | + throw new IllegalArgumentException("Unexpected operation identity type"); |
| 549 | + } |
| 550 | + } |
| 551 | + }, options().retryParams(), EXCEPTION_HANDLER); |
| 552 | + return answer == null ? null : Operation.fromPb(this, answer); |
| 553 | + } catch (RetryHelper.RetryHelperException e) { |
| 554 | + throw ComputeException.translateAndThrow(e); |
| 555 | + } |
| 556 | + } |
| 557 | + |
| 558 | + @Override |
| 559 | + public Page<Operation> listGlobalOperations(OperationListOption... options) { |
| 560 | + return listGlobalOperations(options(), optionMap(options)); |
| 561 | + } |
| 562 | + |
| 563 | + private static Page<Operation> listGlobalOperations(final ComputeOptions serviceOptions, |
| 564 | + final Map<ComputeRpc.Option, ?> optionsMap) { |
| 565 | + try { |
| 566 | + ComputeRpc.Tuple<String, Iterable<com.google.api.services.compute.model.Operation>> result = |
| 567 | + runWithRetries(new Callable<ComputeRpc.Tuple<String, |
| 568 | + Iterable<com.google.api.services.compute.model.Operation>>>() { |
| 569 | + @Override |
| 570 | + public ComputeRpc.Tuple<String, |
| 571 | + Iterable<com.google.api.services.compute.model.Operation>> call() { |
| 572 | + return serviceOptions.rpc().listGlobalOperations(optionsMap); |
| 573 | + } |
| 574 | + }, serviceOptions.retryParams(), EXCEPTION_HANDLER); |
| 575 | + String cursor = result.x(); |
| 576 | + Iterable<Operation> operations = Iterables.transform( |
| 577 | + result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Operation>of() |
| 578 | + : result.y(), |
| 579 | + new Function<com.google.api.services.compute.model.Operation, Operation>() { |
| 580 | + @Override |
| 581 | + public Operation apply(com.google.api.services.compute.model.Operation operation) { |
| 582 | + return Operation.fromPb(serviceOptions.service(), operation); |
| 583 | + } |
| 584 | + }); |
| 585 | + return new PageImpl<>(new GlobalOperationPageFetcher(serviceOptions, cursor, optionsMap), |
| 586 | + cursor, operations); |
| 587 | + } catch (RetryHelper.RetryHelperException e) { |
| 588 | + throw ComputeException.translateAndThrow(e); |
| 589 | + } |
| 590 | + } |
| 591 | + |
| 592 | + @Override |
| 593 | + public Page<Operation> listRegionOperations(String region, OperationListOption... options) { |
| 594 | + return listRegionOperations(region, options(), optionMap(options)); |
| 595 | + } |
| 596 | + |
| 597 | + private static Page<Operation> listRegionOperations(final String region, |
| 598 | + final ComputeOptions serviceOptions, final Map<ComputeRpc.Option, ?> optionsMap) { |
| 599 | + try { |
| 600 | + ComputeRpc.Tuple<String, Iterable<com.google.api.services.compute.model.Operation>> result = |
| 601 | + runWithRetries(new Callable<ComputeRpc.Tuple<String, |
| 602 | + Iterable<com.google.api.services.compute.model.Operation>>>() { |
| 603 | + @Override |
| 604 | + public ComputeRpc.Tuple<String, |
| 605 | + Iterable<com.google.api.services.compute.model.Operation>> call() { |
| 606 | + return serviceOptions.rpc().listRegionOperations(region, optionsMap); |
| 607 | + } |
| 608 | + }, serviceOptions.retryParams(), EXCEPTION_HANDLER); |
| 609 | + String cursor = result.x(); |
| 610 | + Iterable<Operation> operations = Iterables.transform( |
| 611 | + result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Operation>of() |
| 612 | + : result.y(), |
| 613 | + new Function<com.google.api.services.compute.model.Operation, Operation>() { |
| 614 | + @Override |
| 615 | + public Operation apply(com.google.api.services.compute.model.Operation operation) { |
| 616 | + return Operation.fromPb(serviceOptions.service(), operation); |
| 617 | + } |
| 618 | + }); |
| 619 | + return new PageImpl<>(new RegionOperationPageFetcher(region, serviceOptions, cursor, |
| 620 | + optionsMap), cursor, operations); |
| 621 | + } catch (RetryHelper.RetryHelperException e) { |
| 622 | + throw ComputeException.translateAndThrow(e); |
| 623 | + } |
| 624 | + } |
| 625 | + |
| 626 | + @Override |
| 627 | + public Page<Operation> listZoneOperations(String zone, OperationListOption... options) { |
| 628 | + return listZoneOperations(zone, options(), optionMap(options)); |
| 629 | + } |
| 630 | + |
| 631 | + private static Page<Operation> listZoneOperations(final String zone, |
| 632 | + final ComputeOptions serviceOptions, final Map<ComputeRpc.Option, ?> optionsMap) { |
| 633 | + try { |
| 634 | + ComputeRpc.Tuple<String, Iterable<com.google.api.services.compute.model.Operation>> result = |
| 635 | + runWithRetries(new Callable<ComputeRpc.Tuple<String, |
| 636 | + Iterable<com.google.api.services.compute.model.Operation>>>() { |
| 637 | + @Override |
| 638 | + public ComputeRpc.Tuple<String, |
| 639 | + Iterable<com.google.api.services.compute.model.Operation>> call() { |
| 640 | + return serviceOptions.rpc().listZoneOperations(zone, optionsMap); |
| 641 | + } |
| 642 | + }, serviceOptions.retryParams(), EXCEPTION_HANDLER); |
| 643 | + String cursor = result.x(); |
| 644 | + Iterable<Operation> operations = Iterables.transform( |
| 645 | + result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Operation>of() |
| 646 | + : result.y(), |
| 647 | + new Function<com.google.api.services.compute.model.Operation, Operation>() { |
| 648 | + @Override |
| 649 | + public Operation apply(com.google.api.services.compute.model.Operation operation) { |
| 650 | + return Operation.fromPb(serviceOptions.service(), operation); |
| 651 | + } |
| 652 | + }); |
| 653 | + return new PageImpl<>(new ZoneOperationPageFetcher(zone, serviceOptions, cursor, optionsMap), |
| 654 | + cursor, operations); |
| 655 | + } catch (RetryHelper.RetryHelperException e) { |
| 656 | + throw ComputeException.translateAndThrow(e); |
| 657 | + } |
| 658 | + } |
| 659 | + |
| 660 | + @Override |
| 661 | + public boolean delete(final OperationId operation) { |
| 662 | + try { |
| 663 | + return runWithRetries(new Callable<Boolean>() { |
| 664 | + @Override |
| 665 | + public Boolean call() { |
| 666 | + switch (operation.type()) { |
| 667 | + case REGION: |
| 668 | + RegionOperationId regionOperationId = (RegionOperationId) operation; |
| 669 | + return computeRpc.deleteRegionOperation(regionOperationId.region(), |
| 670 | + regionOperationId.operation()); |
| 671 | + case ZONE: |
| 672 | + ZoneOperationId zoneOperationId = (ZoneOperationId) operation; |
| 673 | + return computeRpc.deleteZoneOperation(zoneOperationId.zone(), |
| 674 | + zoneOperationId.operation()); |
| 675 | + case GLOBAL: |
| 676 | + return computeRpc.deleteGlobalOperation(operation.operation()); |
| 677 | + default: |
| 678 | + throw new IllegalArgumentException("Unexpected operation identity type"); |
| 679 | + } |
| 680 | + } |
| 681 | + }, options().retryParams(), EXCEPTION_HANDLER); |
| 682 | + } catch (RetryHelper.RetryHelperException e) { |
| 683 | + throw ComputeException.translateAndThrow(e); |
| 684 | + } |
| 685 | + } |
| 686 | + |
467 | 687 | private Map<ComputeRpc.Option, ?> optionMap(Option... options) { |
468 | 688 | Map<ComputeRpc.Option, Object> optionMap = Maps.newEnumMap(ComputeRpc.Option.class); |
469 | 689 | for (Option option : options) { |
|
0 commit comments