@Override public void run(Compute compute, Triple<InstanceId, String, AccessConfig> interfaceAndConfig) throws InterruptedException { InstanceId instance = interfaceAndConfig.x(); String networkInterface = interfaceAndConfig.y(); AccessConfig accessConfig = interfaceAndConfig.z(); Operation operation = compute.addAccessConfig(instance, networkInterface, accessConfig); 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( "Access config added to network interface %s of instance %s%n", networkInterface, instance); } else { System.out.printf( "Attempt to add access config to network interface %s of instance %s%n", networkInterface, instance); System.out.printf("Error: %s%n", operation.getErrors()); } }
/** * Attaches a persistent disk to this instance given the device name and its configuration. * * @return a zone operation if the attach request was issued correctly, {@code null} if the * instance was not found * @throws ComputeException upon failure */ public Operation attachDisk( String deviceName, PersistentDiskConfiguration configuration, OperationOption... options) { return compute.attachDisk(getInstanceId(), deviceName, configuration, options); }
@Override public void run(Compute compute, Triple<InstanceId, String, String> interfaceAndConfig) throws InterruptedException { InstanceId instance = interfaceAndConfig.x(); String networkInterface = interfaceAndConfig.y(); String accessConfig = interfaceAndConfig.z(); Operation operation = compute.deleteAccessConfig(instance, networkInterface, accessConfig); 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( "Access config deleted from network interface %s of instance %s%n", networkInterface, instance); } else { System.out.printf( "Attempt to delete access config from network interface %s of instance %s failed%n", networkInterface, instance); System.out.printf("Error: %s%n", operation.getErrors()); } }
@Test public void testCreateImageWithOptions() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect(computeRpcMock.createImage(eq(IMAGE.toPb()), capture(capturedOptions))) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.create(IMAGE, OPERATION_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(OPERATION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("id")); assertTrue(selector.contains("description")); assertEquals(23, selector.length()); assertEquals(globalOperation, operation); }
@Test public void testAttachDisk_Null() throws Exception { initializeExpectedInstance(1); expect(compute.getOptions()).andReturn(mockOptions); AttachedDisk.PersistentDiskConfiguration configuration = AttachedDisk.PersistentDiskConfiguration.of(DISK_ID); expect(compute.attachDisk(INSTANCE_ID, configuration)).andReturn(null); replay(compute); initializeInstance(); assertNull(instance.attachDisk(configuration)); }
@Test public void testAttachDisk_Operation() { AttachedDisk attachedDisk = AttachedDisk.of(PERSISTENT_DISK_CONFIGURATION); EasyMock.expect( computeRpcMock.attachDisk( INSTANCE_ID.getZone(), INSTANCE_ID.getInstance(), attachedDisk.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals(zoneOperation, compute.attachDisk(INSTANCE_ID, PERSISTENT_DISK_CONFIGURATION)); }
@Test public void testDeleteTrue() { initializeExpectedOperation(3); expect(compute.getOptions()).andReturn(mockOptions); expect(compute.deleteOperation(GLOBAL_OPERATION_ID)).andReturn(true); replay(compute); initializeOperation(); assertTrue(operation.delete()); verify(compute); }
@Test public void testDeleteAccessConfig_Null() throws Exception { initializeExpectedInstance(1); expect(compute.getOptions()).andReturn(mockOptions); expect(compute.deleteAccessConfig(INSTANCE_ID, "nic0", "NAT")).andReturn(null); replay(compute); initializeInstance(); assertNull(instance.deleteAccessConfig("nic0", "NAT")); }
@Test public void testAddAccessConfig_Null() throws Exception { initializeExpectedInstance(1); expect(compute.getOptions()).andReturn(mockOptions); NetworkInterface.AccessConfig accessConfig = NetworkInterface.AccessConfig.of("192.168.1.1"); expect(compute.addAccessConfig(INSTANCE_ID, "nic0", accessConfig)).andReturn(null); replay(compute); initializeInstance(); assertNull(instance.addAccessConfig("nic0", accessConfig)); }
@Test public void testDeleteGlobalAddressWithSelectedFields_Operation() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( computeRpcMock.deleteGlobalAddress( eq(GLOBAL_ADDRESS_ID.getAddress()), capture(capturedOptions))) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.deleteAddress(GLOBAL_ADDRESS_ID, OPERATION_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(OPERATION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("id")); assertTrue(selector.contains("description")); assertEquals(23, selector.length()); assertEquals(globalOperation, operation); }
@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 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 testDeleteRegionAddress_Operation() { EasyMock.expect( computeRpcMock.deleteRegionAddress( REGION_ADDRESS_ID.getRegion(), REGION_ADDRESS_ID.getAddress(), EMPTY_RPC_OPTIONS)) .andReturn(regionOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals(regionOperation, compute.deleteAddress(REGION_ADDRESS_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 testDeleteAccessConfig_Operation() { EasyMock.expect( computeRpcMock.deleteAccessConfig( INSTANCE_ID.getZone(), INSTANCE_ID.getInstance(), "networkInterface", "accessConfig", EMPTY_RPC_OPTIONS)) .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals( zoneOperation, compute.deleteAccessConfig(INSTANCE_ID, "networkInterface", "accessConfig")); }
@Test public void testAddAccessConfig_Operation() { AccessConfig accessConfig = AccessConfig.of("192.168.1.1"); EasyMock.expect( computeRpcMock.addAccessConfig( INSTANCE_ID.getZone(), INSTANCE_ID.getInstance(), "networkInterface", accessConfig.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals( zoneOperation, compute.addAccessConfig(INSTANCE_ID, "networkInterface", accessConfig)); }
@Test public void testDeleteGlobalOperation_False() { EasyMock.expect(computeRpcMock.deleteGlobalOperation(GLOBAL_OPERATION_ID.getOperation())) .andReturn(false); EasyMock.replay(computeRpcMock); compute = options.getService(); assertFalse(compute.deleteOperation(GLOBAL_OPERATION_ID)); }
@Test public void testDeleteRegionOperation_True() { EasyMock.expect( computeRpcMock.deleteRegionOperation( REGION_OPERATION_ID.getRegion(), REGION_OPERATION_ID.getOperation())) .andReturn(true); EasyMock.replay(computeRpcMock); compute = options.getService(); assertTrue(compute.deleteOperation(REGION_OPERATION_ID)); }