@Override public void run(Compute compute, AddressId address) throws InterruptedException { Operation operation = compute.create(AddressInfo.of(address)); while (!operation.isDone()) { System.out.printf( "Waiting for operation %s to complete%n", operation.getOperationId().getOperation()); Thread.sleep(1000L); } operation = operation.reload(); if (operation.getErrors() == null) { System.out.printf("Address %s was created%n", address); } else { System.out.printf("Creation of address %s failed%n", address); System.out.printf("Error: %s%n", operation.getErrors()); } } }
@Override public Operation apply(com.google.api.services.compute.model.Operation operation) { return Operation.fromPb(serviceOptions.getService(), operation); } };
/** * Checks if this operation has completed its execution, either failing or succeeding. If the * operation does not exist this method returns {@code true}. You can wait for operation * completion with: * * <pre>{@code * while(!operation.isDone()) { * Thread.sleep(1000L); * } * }</pre> * * @return {@code true} if this operation is in {@link Operation.Status#DONE} state or if it does * not exist, {@code false} if the state is not {@link Operation.Status#DONE} * @throws ComputeException upon failure */ public boolean isDone() { Operation operation = compute.getOperation(operationId, OperationOption.fields(Compute.OperationField.STATUS)); return operation == null || operation.getStatus() == Status.DONE; }
@Override public void run(Compute compute, DiskId disk) throws InterruptedException { Operation operation = compute.deleteDisk(disk); if (operation == null) { System.out.printf("Disk %s does not exist%n", disk); return; } while (!operation.isDone()) { System.out.printf( "Waiting for operation %s to complete%n", operation.getOperationId().getOperation()); Thread.sleep(1000L); } operation = operation.reload(); if (operation.getErrors() == null) { System.out.printf("Disk %s was deleted%n", disk); } else { System.out.printf("Deletion of disk %s failed%n", disk); System.out.printf("Error: %s%n", operation.getErrors()); } } }
@Override public void run(Compute compute, InstanceId instance) throws InterruptedException { Operation operation = compute.deleteInstance(instance); if (operation == null) { System.out.printf("Instance %s does not exist%n", instance); return; } while (!operation.isDone()) { System.out.printf( "Waiting for operation %s to complete%n", operation.getOperationId().getOperation()); Thread.sleep(1000L); } operation = operation.reload(); if (operation.getErrors() == null) { System.out.printf("Instance %s was deleted%n", instance); } else { System.out.printf("Deletion of instance %s failed%n", instance); System.out.printf("Error: %s%n", operation.getErrors()); } } }
@Test public void testWaitFor() throws InterruptedException { initializeExpectedOperation(4); Compute.OperationOption[] expectedOptions = { Compute.OperationOption.fields(Compute.OperationField.STATUS) }; Operation successOperation = Operation.fromPb(serviceMockReturnsOptions, globalOperation.toPb().setError(null)); expect(compute.getOptions()).andReturn(mockOptions); expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(successOperation); expect(compute.getOperation(GLOBAL_OPERATION_ID)).andReturn(successOperation); replay(compute, mockOptions); initializeOperation(); assertSame(successOperation, operation.waitFor()); verify(mockOptions); }
@Test public void testReload() throws Exception { initializeExpectedOperation(5); expect(compute.getOptions()).andReturn(mockOptions); expect(compute.getOperation(GLOBAL_OPERATION_ID)).andReturn(globalOperation); replay(compute); initializeOperation(); Operation updatedOperation = operation.reload(); compareOperation(globalOperation, updatedOperation); verify(compute); }
@Test public void testIsDone_True() throws Exception { initializeExpectedOperation(3); Compute.OperationOption[] expectedOptions = { Compute.OperationOption.fields(Compute.OperationField.STATUS) }; expect(compute.getOptions()).andReturn(mockOptions); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(globalOperation); replay(compute); initializeOperation(); assertTrue(operation.isDone()); verify(compute); }
@Test public void testGetRegionOperation_Null() { EasyMock.expect( computeRpcMock.getRegionOperation( REGION_OPERATION_ID.getRegion(), REGION_OPERATION_ID.getOperation(), EMPTY_RPC_OPTIONS)) .andReturn(regionOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.getOperation(REGION_OPERATION_ID); assertEquals(regionOperation, operation); }
@Test public void testIsDone_False() throws Exception { initializeExpectedOperation(4); Compute.OperationOption[] expectedOptions = { Compute.OperationOption.fields(Compute.OperationField.STATUS) }; expect(compute.getOptions()).andReturn(mockOptions); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)) .andReturn( Operation.fromPb( serviceMockReturnsOptions, globalOperation.toPb().setStatus("PENDING"))); replay(compute); initializeOperation(); assertFalse(operation.isDone()); verify(compute); }
@Test public void testDeleteGlobalAddress_Operation() { EasyMock.expect( computeRpcMock.deleteGlobalAddress(GLOBAL_ADDRESS_ID.getAddress(), EMPTY_RPC_OPTIONS)) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals(globalOperation, compute.deleteAddress(GLOBAL_ADDRESS_ID)); }
@Test public void testCreateImage() { EasyMock.expect(computeRpcMock.createImage(IMAGE.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.create(IMAGE); assertEquals(globalOperation, operation); }
@Test public void testGetGlobalOperation() { EasyMock.expect( computeRpcMock.getGlobalOperation( GLOBAL_OPERATION_ID.getOperation(), EMPTY_RPC_OPTIONS)) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals(globalOperation, compute.getOperation(GLOBAL_OPERATION_ID)); }
@Test public void testDeleteNetwork_Operation() { EasyMock.expect(computeRpcMock.deleteNetwork(NETWORK_ID.getNetwork(), EMPTY_RPC_OPTIONS)) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals(globalOperation, compute.deleteNetwork(NETWORK_ID)); }
@Test public void testDeleteSnapshot_Operation() { EasyMock.expect(computeRpcMock.deleteSnapshot(SNAPSHOT_ID.getSnapshot(), EMPTY_RPC_OPTIONS)) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals(globalOperation, compute.deleteSnapshot(SNAPSHOT_ID.getSnapshot())); }
@Test public void testDetachDisk_Operation() { EasyMock.expect( computeRpcMock.detachDisk( INSTANCE_ID.getZone(), INSTANCE_ID.getInstance(), "device", EMPTY_RPC_OPTIONS)) .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals(zoneOperation, compute.detachDisk(INSTANCE_ID, "device")); }
@Test public void testDeleteImage_Operation() { EasyMock.expect( computeRpcMock.deleteImage( IMAGE_ID.getProject(), IMAGE_ID.getImage(), EMPTY_RPC_OPTIONS)) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals(globalOperation, compute.deleteImage(IMAGE_ID)); }
@Test public void testDeleteDisk_Operation() { EasyMock.expect( computeRpcMock.deleteDisk(DISK_ID.getZone(), DISK_ID.getDisk(), EMPTY_RPC_OPTIONS)) .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals(zoneOperation, compute.deleteDisk(DISK_ID)); }
@Test public void testDeleteInstance_Operation() { EasyMock.expect( computeRpcMock.deleteInstance( INSTANCE_ID.getZone(), INSTANCE_ID.getInstance(), EMPTY_RPC_OPTIONS)) .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals(zoneOperation, compute.deleteInstance(INSTANCE_ID)); }
@Test public void testGetZoneOperation() { EasyMock.expect( computeRpcMock.getZoneOperation( ZONE_OPERATION_ID.getZone(), ZONE_OPERATION_ID.getOperation(), EMPTY_RPC_OPTIONS)) .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.getOperation(ZONE_OPERATION_ID); assertEquals(zoneOperation, operation); }