@Override public Operation attachDisk( InstanceId instance, String deviceName, PersistentDiskConfiguration configuration, OperationOption... options) { return attachDisk(instance, AttachedDisk.of(deviceName, configuration), options); }
@Override InstanceInfo parse(String... args) throws Exception { String message; if (args.length == 5) { String zone = args[0]; String instance = args[1]; InstanceId instanceId = InstanceId.of(zone, instance); MachineTypeId machineTypeId = MachineTypeId.of(zone, args[2]); DiskId diskId = DiskId.of(zone, args[3]); AttachedDisk disk = AttachedDisk.of(PersistentDiskConfiguration.newBuilder(diskId).setBoot(true).build()); NetworkInterface networkInterface = NetworkInterface.of(args[4]); return InstanceInfo.of(instanceId, machineTypeId, disk, networkInterface); } else if (args.length > 5) { message = "Too many arguments."; } else { message = "Missing required arguments."; } throw new IllegalArgumentException(message); }
@Test public void testToBuilderIncomplete() { AttachedDisk attachedDisk = AttachedDisk.of(PERSISTENT_DISK_CONFIGURATION); assertEquals(attachedDisk, attachedDisk.toBuilder().build()); attachedDisk = AttachedDisk.of(SCRATCH_DISK_CONFIGURATION); assertEquals(attachedDisk, attachedDisk.toBuilder().build()); attachedDisk = AttachedDisk.of(CREATE_DISK_CONFIGURATION); assertEquals(attachedDisk, attachedDisk.toBuilder().build()); }
@Test public void testAttachDisk_Null() { AttachedDisk attachedDisk = AttachedDisk.of(PERSISTENT_DISK_CONFIGURATION); EasyMock.expect( computeRpcMock.attachDisk( INSTANCE_ID.getZone(), INSTANCE_ID.getInstance(), attachedDisk.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(null); EasyMock.replay(computeRpcMock); compute = options.getService(); assertNull(compute.attachDisk(INSTANCE_ID, PERSISTENT_DISK_CONFIGURATION)); }
@Test public void testAttachDiskName_Null() { AttachedDisk attachedDisk = AttachedDisk.of("dev0", PERSISTENT_DISK_CONFIGURATION); EasyMock.expect( computeRpcMock.attachDisk( INSTANCE_ID.getZone(), INSTANCE_ID.getInstance(), attachedDisk.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(null); EasyMock.replay(computeRpcMock); compute = options.getService(); assertNull(compute.attachDisk(INSTANCE_ID, "dev0", PERSISTENT_DISK_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 testAttachDiskName_Operation() { AttachedDisk attachedDisk = AttachedDisk.of("dev0", 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, "dev0", PERSISTENT_DISK_CONFIGURATION)); }
PersistentDiskConfiguration persistentConfigurationWithProject = PersistentDiskConfiguration.of(DiskId.of("project", "zone", "disk")); AttachedDisk attachedDisk = AttachedDisk.of(persistentConfiguration); compareAttachedDisk( AttachedDisk.of(persistentConfigurationWithProject), attachedDisk.setProjectId("project")); ScratchDiskConfiguration scratchDiskConfiguration = ScratchDiskConfiguration.of(DiskTypeId.of("zone", "diskType")); ScratchDiskConfiguration.of(DiskTypeId.of("project", "zone", "diskType")); compareAttachedDisk( AttachedDisk.of(scratchDiskConfigurationWithProject), AttachedDisk.of(scratchDiskConfiguration).setProjectId("project")); CreateDiskConfiguration createDiskConfiguration = CreateDiskConfiguration.of(ImageId.of("image")); CreateDiskConfiguration.of(ImageId.of("project", "image")); compareAttachedDisk( AttachedDisk.of(createDiskConfigurationWithProject), AttachedDisk.of(createDiskConfiguration).setProjectId("project")); createDiskConfiguration = CREATE_DISK_CONFIGURATION .build(); compareAttachedDisk( AttachedDisk.of(CREATE_DISK_CONFIGURATION), AttachedDisk.of(createDiskConfiguration).setProjectId("project"));
@Test public void testToAndFromPb() { AttachedDisk attachedDisk = AttachedDisk.fromPb(PERSISTENT_DISK.toPb()); compareAttachedDisk(PERSISTENT_DISK, attachedDisk); attachedDisk = AttachedDisk.fromPb(SCRATCH_DISK.toPb()); compareAttachedDisk(SCRATCH_DISK, attachedDisk); attachedDisk = AttachedDisk.fromPb(CREATED_DISK.toPb()); compareAttachedDisk(CREATED_DISK, attachedDisk); attachedDisk = AttachedDisk.of(DEVICE_NAME, PERSISTENT_DISK_CONFIGURATION); compareAttachedDisk(attachedDisk, AttachedDisk.fromPb(attachedDisk.toPb())); attachedDisk = AttachedDisk.of(PERSISTENT_DISK_CONFIGURATION); compareAttachedDisk(attachedDisk, AttachedDisk.fromPb(attachedDisk.toPb())); attachedDisk = AttachedDisk.of(DEVICE_NAME, SCRATCH_DISK_CONFIGURATION); compareAttachedDisk(attachedDisk, AttachedDisk.fromPb(attachedDisk.toPb())); attachedDisk = AttachedDisk.of(SCRATCH_DISK_CONFIGURATION); compareAttachedDisk(attachedDisk, AttachedDisk.fromPb(attachedDisk.toPb())); attachedDisk = AttachedDisk.of(DEVICE_NAME, CREATE_DISK_CONFIGURATION); compareAttachedDisk(attachedDisk, AttachedDisk.fromPb(attachedDisk.toPb())); attachedDisk = AttachedDisk.of(CREATE_DISK_CONFIGURATION); compareAttachedDisk(attachedDisk, AttachedDisk.fromPb(attachedDisk.toPb())); }
@Test public void testAttachDiskWithSelectedFields_Operation() { AttachedDisk attachedDisk = AttachedDisk.of(PERSISTENT_DISK_CONFIGURATION); Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( computeRpcMock.attachDisk( eq(INSTANCE_ID.getZone()), eq(INSTANCE_ID.getInstance()), eq(attachedDisk.toPb()), capture(capturedOptions))) .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.attachDisk(INSTANCE_ID, PERSISTENT_DISK_CONFIGURATION, 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(zoneOperation, operation); }
@Test public void testAttachDiskNameWithSelectedFields_Operation() { AttachedDisk attachedDisk = AttachedDisk.of("dev0", PERSISTENT_DISK_CONFIGURATION); Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( computeRpcMock.attachDisk( eq(INSTANCE_ID.getZone()), eq(INSTANCE_ID.getInstance()), eq(attachedDisk.toPb()), capture(capturedOptions))) .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.attachDisk( INSTANCE_ID, "dev0", PERSISTENT_DISK_CONFIGURATION, 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(zoneOperation, operation); }
PersistentDiskConfiguration attachConfiguration = PersistentDiskConfiguration.newBuilder(diskId).setBoot(true).build(); AttachedDisk attachedDisk = AttachedDisk.of("dev0", attachConfiguration); NetworkInterface networkInterface = NetworkInterface.newBuilder(networkId)
public static void main(String... args) throws InterruptedException, TimeoutException { Compute compute = ComputeOptions.getDefaultInstance().getService(); ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329"); NetworkId networkId = NetworkId.of("default"); AttachedDisk attachedDisk = AttachedDisk.of(AttachedDisk.CreateDiskConfiguration.of(imageId)); NetworkInterface networkInterface = NetworkInterface.of(networkId); InstanceId instanceId = InstanceId.of("us-central1-a", "instance-name"); MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1"); Operation operation = compute.create(InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface)); operation = operation.waitFor(); if (operation.getErrors() == null) { // use instance Instance instance = compute.getInstance(instanceId); } } }
@Test public void testSetProjectId() { InstanceInfo instance = InstanceInfo.of( InstanceId.of("zone", "instance"), MachineTypeId.of("zone", "type"), AttachedDisk.of(AttachedDisk.PersistentDiskConfiguration.of(DiskId.of("zone", "disk"))), NetworkInterface.of(NetworkId.of("project", "network"))); InstanceInfo instanceWithProject = InstanceInfo.of(INSTANCE_ID, MACHINE_TYPE, ATTACHED_DISK, NETWORK_INTERFACE); compareInstanceInfo(instanceWithProject, instance.setProjectId("project")); }
@Test public void testOf() { AttachedDisk attachedDisk = AttachedDisk.of(DEVICE_NAME, PERSISTENT_DISK_CONFIGURATION); assertEquals(PERSISTENT_DISK_CONFIGURATION, attachedDisk.getConfiguration()); assertEquals(DEVICE_NAME, attachedDisk.getDeviceName()); assertNull(attachedDisk.getIndex()); assertNull(attachedDisk.getLicenses()); attachedDisk = AttachedDisk.of(PERSISTENT_DISK_CONFIGURATION); assertEquals(PERSISTENT_DISK_CONFIGURATION, attachedDisk.getConfiguration()); assertNull(attachedDisk.getDeviceName()); assertNull(attachedDisk.getIndex()); assertNull(attachedDisk.getLicenses()); attachedDisk = AttachedDisk.of(DEVICE_NAME, SCRATCH_DISK_CONFIGURATION); assertEquals(SCRATCH_DISK_CONFIGURATION, attachedDisk.getConfiguration()); assertEquals(DEVICE_NAME, attachedDisk.getDeviceName()); assertNull(attachedDisk.getIndex()); assertNull(attachedDisk.getLicenses()); attachedDisk = AttachedDisk.of(SCRATCH_DISK_CONFIGURATION); assertEquals(SCRATCH_DISK_CONFIGURATION, attachedDisk.getConfiguration()); assertNull(attachedDisk.getDeviceName()); assertNull(attachedDisk.getIndex()); assertNull(attachedDisk.getLicenses()); attachedDisk = AttachedDisk.of(DEVICE_NAME, CREATE_DISK_CONFIGURATION); assertEquals(CREATE_DISK_CONFIGURATION, attachedDisk.getConfiguration()); assertEquals(DEVICE_NAME, attachedDisk.getDeviceName()); assertNull(attachedDisk.getIndex()); assertNull(attachedDisk.getLicenses()); attachedDisk = AttachedDisk.of(CREATE_DISK_CONFIGURATION); assertEquals(CREATE_DISK_CONFIGURATION, attachedDisk.getConfiguration()); assertNull(attachedDisk.getDeviceName());
NetworkInterface networkInterface = NetworkInterface.newBuilder(networkId).build(); AttachedDisk disk = AttachedDisk.of( "dev0", AttachedDisk.CreateDiskConfiguration.newBuilder(IMAGE_ID).setAutoDelete(true).build());
NetworkInterface networkInterface = NetworkInterface.newBuilder(networkId).build(); AttachedDisk disk = AttachedDisk.of( "dev0", AttachedDisk.CreateDiskConfiguration.newBuilder(IMAGE_ID).setAutoDelete(true).build());
NetworkInterface networkInterface = NetworkInterface.newBuilder(networkId).build(); AttachedDisk disk = AttachedDisk.of( "dev0", AttachedDisk.CreateDiskConfiguration.newBuilder(IMAGE_ID).setAutoDelete(true).build());
NetworkInterface networkInterface = NetworkInterface.newBuilder(networkId).build(); AttachedDisk disk = AttachedDisk.of( "dev0", AttachedDisk.CreateDiskConfiguration.newBuilder(IMAGE_ID).setAutoDelete(true).build());