@Override public boolean equals(Object obj) { return obj == this || obj != null && obj.getClass().equals(CreateDiskConfiguration.class) && baseEquals((CreateDiskConfiguration) obj); }
@Override public int hashCode() { return Objects.hash(baseHashCode(), diskName, diskType, diskSizeGb, sourceImage); }
@SuppressWarnings("unchecked") static <T extends AttachedDiskConfiguration> T fromPb( com.google.api.services.compute.model.AttachedDisk diskPb) { switch (Type.valueOf(diskPb.getType())) { case PERSISTENT: if (diskPb.getSource() == null) { return (T) CreateDiskConfiguration.fromPb(diskPb); } else { return (T) PersistentDiskConfiguration.fromPb(diskPb); } case SCRATCH: return (T) ScratchDiskConfiguration.fromPb(diskPb); default: // should be unreachable throw new IllegalArgumentException("Unrecognized attached disk type"); } } }
AttachedDisk.of( "dev0", AttachedDisk.CreateDiskConfiguration.newBuilder(IMAGE_ID).setAutoDelete(true).build()); AttachedDisk disk2 = AttachedDisk.of(
AttachedDisk.of( "dev0", AttachedDisk.CreateDiskConfiguration.newBuilder(IMAGE_ID).setAutoDelete(true).build()); InstanceInfo instanceInfo = InstanceInfo.newBuilder(instanceId, MachineTypeId.of(ZONE, MACHINE_TYPE))
AttachedDisk.of( "dev0", AttachedDisk.CreateDiskConfiguration.newBuilder(IMAGE_ID).setAutoDelete(true).build()); InstanceInfo instanceInfo = InstanceInfo.newBuilder(instanceId, MachineTypeId.of(ZONE, MACHINE_TYPE))
AttachedDisk.of( "dev0", AttachedDisk.CreateDiskConfiguration.newBuilder(IMAGE_ID).setAutoDelete(true).build()); InstanceInfo instanceInfo = InstanceInfo.newBuilder(instanceId, MachineTypeId.of(ZONE, MACHINE_TYPE))
AttachedDisk.of( "dev0", AttachedDisk.CreateDiskConfiguration.newBuilder(IMAGE_ID).setAutoDelete(true).build()); InstanceInfo instanceInfo = InstanceInfo.newBuilder(instanceId, MachineTypeId.of(ZONE, MACHINE_TYPE))
AttachedDisk.of(scratchDiskConfiguration).setProjectId("project")); CreateDiskConfiguration createDiskConfiguration = CreateDiskConfiguration.of(ImageId.of("image")); CreateDiskConfiguration createDiskConfigurationWithProject = CreateDiskConfiguration.of(ImageId.of("project", "image")); compareAttachedDisk( AttachedDisk.of(createDiskConfigurationWithProject), createDiskConfiguration = CREATE_DISK_CONFIGURATION .toBuilder() .setDiskType(DiskTypeId.of("zone", "diskType")) .setSourceImage(ImageId.of("image"))
@Test public void testConfigurationToAndFromPb() { PersistentDiskConfiguration persistentConfiguration = PersistentDiskConfiguration.of(DISK_ID); comparePersistentDiskConfiguration( persistentConfiguration, AttachedDisk.AttachedDiskConfiguration.<PersistentDiskConfiguration>fromPb( persistentConfiguration.toPb())); comparePersistentDiskConfiguration( PERSISTENT_DISK_CONFIGURATION, AttachedDisk.AttachedDiskConfiguration.<PersistentDiskConfiguration>fromPb( PERSISTENT_DISK_CONFIGURATION.toPb())); ScratchDiskConfiguration scratchDiskConfiguration = ScratchDiskConfiguration.of(DISK_TYPE_ID); compareScratchDiskConfiguration( scratchDiskConfiguration, AttachedDisk.AttachedDiskConfiguration.<ScratchDiskConfiguration>fromPb( scratchDiskConfiguration.toPb())); compareScratchDiskConfiguration( SCRATCH_DISK_CONFIGURATION, AttachedDisk.AttachedDiskConfiguration.<ScratchDiskConfiguration>fromPb( SCRATCH_DISK_CONFIGURATION.toPb())); CreateDiskConfiguration createDiskConfiguration = CreateDiskConfiguration.of(IMAGE_ID); compareCreateDiskConfiguration( createDiskConfiguration, AttachedDisk.AttachedDiskConfiguration.<CreateDiskConfiguration>fromPb( createDiskConfiguration.toPb())); compareCreateDiskConfiguration( CREATE_DISK_CONFIGURATION, AttachedDisk.AttachedDiskConfiguration.<CreateDiskConfiguration>fromPb( CREATE_DISK_CONFIGURATION.toPb())); }
@Test public void testConfigurationOf() { PersistentDiskConfiguration persistentConfiguration = PersistentDiskConfiguration.of(DISK_ID); assertEquals(DISK_ID, persistentConfiguration.getSourceDisk()); assertEquals(Type.PERSISTENT, persistentConfiguration.getType()); assertNull(persistentConfiguration.autoDelete()); assertNull(persistentConfiguration.boot()); assertNull(persistentConfiguration.getInterfaceType()); ScratchDiskConfiguration scratchDiskConfiguration = ScratchDiskConfiguration.of(DISK_TYPE_ID); assertEquals(DISK_TYPE_ID, scratchDiskConfiguration.getDiskType()); assertNull(scratchDiskConfiguration.getInterfaceType()); assertEquals(Type.SCRATCH, scratchDiskConfiguration.getType()); assertTrue(scratchDiskConfiguration.autoDelete()); assertFalse(scratchDiskConfiguration.boot()); assertNull(scratchDiskConfiguration.getInterfaceType()); CreateDiskConfiguration createDiskConfiguration = CreateDiskConfiguration.of(IMAGE_ID); assertEquals(IMAGE_ID, createDiskConfiguration.getSourceImage()); assertNull(createDiskConfiguration.getDiskType()); assertNull(createDiskConfiguration.getDiskName()); assertNull(createDiskConfiguration.getDiskSizeGb()); assertNull(createDiskConfiguration.getInterfaceType()); assertEquals(Type.PERSISTENT, createDiskConfiguration.getType()); assertNull(createDiskConfiguration.autoDelete()); assertTrue(createDiskConfiguration.boot()); assertNull(createDiskConfiguration.getInterfaceType()); }
@Test public void testConfigurationSetProjectId() { PersistentDiskConfiguration persistentConfiguration = PersistentDiskConfiguration.of(DiskId.of("zone", "disk")); comparePersistentDiskConfiguration( PersistentDiskConfiguration.of(DiskId.of("project", "zone", "disk")), persistentConfiguration.setProjectId("project")); ScratchDiskConfiguration scratchDiskConfiguration = ScratchDiskConfiguration.of(DiskTypeId.of("zone", "diskType")); compareScratchDiskConfiguration( ScratchDiskConfiguration.of(DiskTypeId.of("project", "zone", "diskType")), scratchDiskConfiguration.setProjectId("project")); CreateDiskConfiguration createDiskConfiguration = CREATE_DISK_CONFIGURATION .toBuilder() .setDiskType(DiskTypeId.of("zone", "diskType")) .setSourceImage(ImageId.of("image")) .build(); compareCreateDiskConfiguration( CREATE_DISK_CONFIGURATION, createDiskConfiguration.setProjectId("project")); }
@Test public void testConfigurationToBuilder() { comparePersistentDiskConfiguration( PERSISTENT_DISK_CONFIGURATION, PERSISTENT_DISK_CONFIGURATION.toBuilder().build()); compareScratchDiskConfiguration( SCRATCH_DISK_CONFIGURATION, SCRATCH_DISK_CONFIGURATION.toBuilder().build()); compareCreateDiskConfiguration( CREATE_DISK_CONFIGURATION, CREATE_DISK_CONFIGURATION.toBuilder().build()); PersistentDiskConfiguration persistentDiskConfiguration = PERSISTENT_DISK_CONFIGURATION.toBuilder().setAutoDelete(false).build(); assertFalse(persistentDiskConfiguration.autoDelete()); persistentDiskConfiguration = persistentDiskConfiguration.toBuilder().setAutoDelete(AUTO_DELETE).build(); assertEquals(PERSISTENT_DISK_CONFIGURATION, persistentDiskConfiguration); ScratchDiskConfiguration scratchDiskConfiguration = SCRATCH_DISK_CONFIGURATION.toBuilder().setInterfaceType(InterfaceType.SCSI).build(); assertEquals(InterfaceType.SCSI, scratchDiskConfiguration.getInterfaceType()); scratchDiskConfiguration = scratchDiskConfiguration.toBuilder().setInterfaceType(INTERFACE_TYPE).build(); assertEquals(SCRATCH_DISK_CONFIGURATION, scratchDiskConfiguration); CreateDiskConfiguration createDiskConfiguration = CREATE_DISK_CONFIGURATION.toBuilder().setAutoDelete(false).build(); assertFalse(createDiskConfiguration.autoDelete()); createDiskConfiguration = createDiskConfiguration.toBuilder().setAutoDelete(AUTO_DELETE).build(); assertEquals(CREATE_DISK_CONFIGURATION, createDiskConfiguration); }
@Test public void testConfigurationToBuilderIncomplete() { PersistentDiskConfiguration persistentConfiguration = PersistentDiskConfiguration.of(DISK_ID); comparePersistentDiskConfiguration( persistentConfiguration, AttachedDisk.AttachedDiskConfiguration.<PersistentDiskConfiguration>fromPb( persistentConfiguration.toPb())); ScratchDiskConfiguration scratchDiskConfiguration = ScratchDiskConfiguration.of(DISK_TYPE_ID); compareScratchDiskConfiguration( scratchDiskConfiguration, AttachedDisk.AttachedDiskConfiguration.<ScratchDiskConfiguration>fromPb( scratchDiskConfiguration.toPb())); CreateDiskConfiguration createDiskConfiguration = CreateDiskConfiguration.of(IMAGE_ID); compareCreateDiskConfiguration( createDiskConfiguration, AttachedDisk.AttachedDiskConfiguration.<CreateDiskConfiguration>fromPb( createDiskConfiguration.toPb())); }
@Test public void testConfigurationBuilder() { assertTrue(CREATE_DISK_CONFIGURATION.boot()); assertEquals(AUTO_DELETE, CREATE_DISK_CONFIGURATION.autoDelete()); assertNull(CREATE_DISK_CONFIGURATION.getInterfaceType()); assertEquals(Type.PERSISTENT, CREATE_DISK_CONFIGURATION.getType()); assertEquals(IMAGE_ID, CREATE_DISK_CONFIGURATION.getSourceImage()); assertEquals(DISK_NAME, CREATE_DISK_CONFIGURATION.getDiskName()); assertEquals(DISK_TYPE_ID, CREATE_DISK_CONFIGURATION.getDiskType()); assertEquals(DISK_SIZE_GB, CREATE_DISK_CONFIGURATION.getDiskSizeGb()); assertEquals(IMAGE_ID, CREATE_DISK_CONFIGURATION.getSourceImage()); assertEquals(BOOT, PERSISTENT_DISK_CONFIGURATION.boot()); assertEquals(AUTO_DELETE, PERSISTENT_DISK_CONFIGURATION.autoDelete()); assertNull(PERSISTENT_DISK_CONFIGURATION.getInterfaceType()); assertEquals(Type.PERSISTENT, PERSISTENT_DISK_CONFIGURATION.getType()); assertEquals(MODE, PERSISTENT_DISK_CONFIGURATION.getMode()); assertEquals(DISK_ID, PERSISTENT_DISK_CONFIGURATION.getSourceDisk()); assertFalse(SCRATCH_DISK_CONFIGURATION.boot()); assertTrue(SCRATCH_DISK_CONFIGURATION.autoDelete()); assertEquals(INTERFACE_TYPE, SCRATCH_DISK_CONFIGURATION.getInterfaceType()); assertEquals(Type.SCRATCH, SCRATCH_DISK_CONFIGURATION.getType()); assertEquals(DISK_TYPE_ID, SCRATCH_DISK_CONFIGURATION.getDiskType()); }
@SuppressWarnings("unchecked") static CreateDiskConfiguration fromPb( com.google.api.services.compute.model.AttachedDisk diskPb) { AttachedDiskInitializeParams initializeParamsPb = diskPb.getInitializeParams(); Builder builder = newBuilder(ImageId.fromUrl(initializeParamsPb.getSourceImage())); if (initializeParamsPb.getDiskType() != null) { builder.setDiskType(DiskTypeId.fromUrl(initializeParamsPb.getDiskType())); } builder.setDiskName(initializeParamsPb.getDiskName()); builder.setDiskSizeGb(initializeParamsPb.getDiskSizeGb()); builder.setAutoDelete(diskPb.getAutoDelete()); if (initializeParamsPb.getDiskType() != null) { builder.setDiskType(DiskTypeId.fromUrl(initializeParamsPb.getDiskType())); } return builder.build(); } }
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); } } }
/** * Returns a {@code CreateDiskConfiguration} object given the source image that will be used to * create the disk. */ public static CreateDiskConfiguration of(ImageId sourceImage) { return newBuilder(sourceImage).build(); }
public void compareCreateDiskConfiguration( CreateDiskConfiguration expected, CreateDiskConfiguration value) { compareAttachedDiskConfiguration(expected, value); assertEquals(expected.getDiskName(), value.getDiskName()); assertEquals(expected.getDiskType(), value.getDiskType()); assertEquals(expected.getDiskSizeGb(), value.getDiskSizeGb()); assertEquals(expected.getSourceImage(), value.getSourceImage()); }
private Builder(CreateDiskConfiguration configuration) { this.diskName = configuration.diskName; this.diskType = configuration.diskType; this.diskSizeGb = configuration.diskSizeGb; this.sourceImage = configuration.sourceImage; this.autoDelete = configuration.autoDelete(); }