com.google.cloud.compute.DiskId

Here are the examples of the java api class com.google.cloud.compute.DiskId taken from open source projects.

1. CreateSnapshot#main()

Project: gcloud-java
Source File: CreateSnapshot.java
View license
public static void main(String... args) throws InterruptedException, TimeoutException {
    Compute compute = ComputeOptions.defaultInstance().service();
    DiskId diskId = DiskId.of("us-central1-a", "disk-name");
    Disk disk = compute.getDisk(diskId, Compute.DiskOption.fields());
    if (disk != null) {
        String snapshotName = "disk-name-snapshot";
        Operation operation = disk.createSnapshot(snapshotName);
        operation = operation.waitFor();
        if (operation.errors() == null) {
            // use snapshot
            Snapshot snapshot = compute.getSnapshot(snapshotName);
        }
    }
}

2. ITComputeTest#testCreateGetAndDeleteSnapshotAndSnapshotDisk()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testCreateGetAndDeleteSnapshotAndSnapshotDisk() throws InterruptedException, TimeoutException {
    String diskName = BASE_RESOURCE_NAME + "create-and-get-snapshot-disk1";
    String snapshotDiskName = BASE_RESOURCE_NAME + "create-and-get-snapshot-disk2";
    DiskId diskId = DiskId.of(ZONE, diskName);
    DiskId snapshotDiskId = DiskId.of(ZONE, snapshotDiskName);
    String snapshotName = BASE_RESOURCE_NAME + "create-and-get-snapshot";
    DiskInfo diskInfo = DiskInfo.of(diskId, StandardDiskConfiguration.of(DiskTypeId.of(ZONE, "pd-ssd"), 100L));
    Operation operation = compute.create(diskInfo);
    operation.waitFor();
    Disk remoteDisk = compute.getDisk(diskId);
    operation = remoteDisk.createSnapshot(snapshotName);
    operation.waitFor();
    // test get snapshot with selected fields
    Snapshot snapshot = compute.getSnapshot(snapshotName, Compute.SnapshotOption.fields(Compute.SnapshotField.CREATION_TIMESTAMP));
    assertNull(snapshot.generatedId());
    assertNotNull(snapshot.snapshotId());
    assertNotNull(snapshot.creationTimestamp());
    assertNull(snapshot.description());
    assertNull(snapshot.status());
    assertNull(snapshot.diskSizeGb());
    assertNull(snapshot.licenses());
    assertNull(snapshot.sourceDisk());
    assertNull(snapshot.sourceDiskId());
    assertNull(snapshot.storageBytes());
    assertNull(snapshot.storageBytesStatus());
    // test get snapshot
    snapshot = compute.getSnapshot(snapshotName);
    assertNotNull(snapshot.generatedId());
    assertNotNull(snapshot.snapshotId());
    assertNotNull(snapshot.creationTimestamp());
    assertNotNull(snapshot.status());
    assertEquals(100L, (long) snapshot.diskSizeGb());
    assertEquals(diskName, snapshot.sourceDisk().disk());
    assertNotNull(snapshot.sourceDiskId());
    assertNotNull(snapshot.storageBytes());
    assertNotNull(snapshot.storageBytesStatus());
    remoteDisk.delete();
    diskInfo = DiskInfo.of(snapshotDiskId, SnapshotDiskConfiguration.of(SnapshotId.of(snapshotName)));
    operation = compute.create(diskInfo);
    operation.waitFor();
    // test get disk
    remoteDisk = compute.getDisk(snapshotDiskId);
    assertNotNull(remoteDisk);
    assertEquals(ZONE, remoteDisk.diskId().zone());
    assertEquals(snapshotDiskId.disk(), remoteDisk.diskId().disk());
    assertEquals(DiskInfo.CreationStatus.READY, remoteDisk.creationStatus());
    assertNotNull(remoteDisk.creationTimestamp());
    assertNotNull(remoteDisk.generatedId());
    assertTrue(remoteDisk.configuration() instanceof SnapshotDiskConfiguration);
    SnapshotDiskConfiguration remoteConfiguration = remoteDisk.configuration();
    assertEquals(DiskConfiguration.Type.SNAPSHOT, remoteConfiguration.type());
    assertEquals(snapshotName, remoteConfiguration.sourceSnapshot().snapshot());
    assertEquals(100L, (long) remoteConfiguration.sizeGb());
    assertEquals("pd-standard", remoteConfiguration.diskType().type());
    assertNotNull(remoteConfiguration.sourceSnapshotId());
    assertNull(remoteDisk.lastAttachTimestamp());
    assertNull(remoteDisk.lastDetachTimestamp());
    // test get disk with selected fields
    remoteDisk = compute.getDisk(snapshotDiskId, Compute.DiskOption.fields());
    assertNotNull(remoteDisk);
    assertEquals(ZONE, remoteDisk.diskId().zone());
    assertEquals(snapshotDiskId.disk(), remoteDisk.diskId().disk());
    assertNull(remoteDisk.creationStatus());
    assertNull(remoteDisk.creationTimestamp());
    assertNull(remoteDisk.generatedId());
    assertTrue(remoteDisk.configuration() instanceof SnapshotDiskConfiguration);
    remoteConfiguration = remoteDisk.configuration();
    assertEquals(DiskConfiguration.Type.SNAPSHOT, remoteConfiguration.type());
    assertEquals(snapshotName, remoteConfiguration.sourceSnapshot().snapshot());
    assertNull(remoteConfiguration.sizeGb());
    assertEquals("pd-standard", remoteConfiguration.diskType().type());
    assertNull(remoteDisk.<SnapshotDiskConfiguration>configuration().sourceSnapshotId());
    assertNull(remoteDisk.lastAttachTimestamp());
    assertNull(remoteDisk.lastDetachTimestamp());
    operation = remoteDisk.delete();
    operation.waitFor();
    assertNull(compute.getDisk(snapshotDiskId));
    operation = snapshot.delete();
    operation.waitFor();
    assertNull(compute.getSnapshot(snapshotName));
}

3. ITComputeTest#testListDisksAndSnapshots()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testListDisksAndSnapshots() throws InterruptedException, TimeoutException {
    String prefix = BASE_RESOURCE_NAME + "list-disks-and-snapshots-disk";
    String[] diskNames = { prefix + "1", prefix + "2" };
    DiskId firstDiskId = DiskId.of(ZONE, diskNames[0]);
    DiskId secondDiskId = DiskId.of(ZONE, diskNames[1]);
    DiskConfiguration configuration = StandardDiskConfiguration.of(DiskTypeId.of(ZONE, "pd-ssd"), 100L);
    Operation firstOperation = compute.create(DiskInfo.of(firstDiskId, configuration));
    Operation secondOperation = compute.create(DiskInfo.of(secondDiskId, configuration));
    firstOperation.waitFor();
    secondOperation.waitFor();
    Set<String> diskSet = ImmutableSet.copyOf(diskNames);
    // test list disks
    Compute.DiskFilter diskFilter = Compute.DiskFilter.equals(Compute.DiskField.NAME, prefix + "\\d");
    Page<Disk> diskPage = compute.listDisks(ZONE, Compute.DiskListOption.filter(diskFilter));
    Iterator<Disk> diskIterator = diskPage.iterateAll();
    int count = 0;
    while (diskIterator.hasNext()) {
        Disk remoteDisk = diskIterator.next();
        assertEquals(ZONE, remoteDisk.diskId().zone());
        assertTrue(diskSet.contains(remoteDisk.diskId().disk()));
        assertEquals(DiskInfo.CreationStatus.READY, remoteDisk.creationStatus());
        assertNotNull(remoteDisk.creationTimestamp());
        assertNotNull(remoteDisk.generatedId());
        assertTrue(remoteDisk.configuration() instanceof StandardDiskConfiguration);
        StandardDiskConfiguration remoteConfiguration = remoteDisk.configuration();
        assertEquals(100L, (long) remoteConfiguration.sizeGb());
        assertEquals("pd-ssd", remoteConfiguration.diskType().type());
        assertEquals(DiskConfiguration.Type.STANDARD, remoteConfiguration.type());
        assertNull(remoteDisk.lastAttachTimestamp());
        assertNull(remoteDisk.lastDetachTimestamp());
        count++;
    }
    assertEquals(2, count);
    // test list disks with selected fields
    count = 0;
    diskPage = compute.listDisks(ZONE, Compute.DiskListOption.filter(diskFilter), Compute.DiskListOption.fields(Compute.DiskField.STATUS));
    diskIterator = diskPage.iterateAll();
    while (diskIterator.hasNext()) {
        Disk remoteDisk = diskIterator.next();
        assertEquals(ZONE, remoteDisk.diskId().zone());
        assertTrue(diskSet.contains(remoteDisk.diskId().disk()));
        assertEquals(DiskInfo.CreationStatus.READY, remoteDisk.creationStatus());
        assertNull(remoteDisk.creationTimestamp());
        assertNull(remoteDisk.generatedId());
        assertTrue(remoteDisk.configuration() instanceof StandardDiskConfiguration);
        StandardDiskConfiguration remoteConfiguration = remoteDisk.configuration();
        assertNull(remoteConfiguration.sizeGb());
        assertEquals("pd-ssd", remoteConfiguration.diskType().type());
        assertEquals(DiskConfiguration.Type.STANDARD, remoteConfiguration.type());
        assertNull(remoteDisk.lastAttachTimestamp());
        assertNull(remoteDisk.lastDetachTimestamp());
        count++;
    }
    assertEquals(2, count);
    // test snapshots
    SnapshotId firstSnapshotId = SnapshotId.of(diskNames[0]);
    SnapshotId secondSnapshotId = SnapshotId.of(diskNames[1]);
    firstOperation = compute.create(SnapshotInfo.of(firstSnapshotId, firstDiskId));
    secondOperation = compute.create(SnapshotInfo.of(secondSnapshotId, secondDiskId));
    firstOperation.waitFor();
    secondOperation.waitFor();
    // test list snapshots
    Compute.SnapshotFilter snapshotFilter = Compute.SnapshotFilter.equals(Compute.SnapshotField.NAME, prefix + "\\d");
    Page<Snapshot> snapshotPage = compute.listSnapshots(Compute.SnapshotListOption.filter(snapshotFilter));
    Iterator<Snapshot> snapshotIterator = snapshotPage.iterateAll();
    count = 0;
    while (snapshotIterator.hasNext()) {
        Snapshot remoteSnapshot = snapshotIterator.next();
        assertNotNull(remoteSnapshot.generatedId());
        assertTrue(diskSet.contains(remoteSnapshot.snapshotId().snapshot()));
        assertNotNull(remoteSnapshot.creationTimestamp());
        assertNotNull(remoteSnapshot.status());
        assertEquals(100L, (long) remoteSnapshot.diskSizeGb());
        assertTrue(diskSet.contains(remoteSnapshot.sourceDisk().disk()));
        assertNotNull(remoteSnapshot.sourceDiskId());
        assertNotNull(remoteSnapshot.storageBytes());
        assertNotNull(remoteSnapshot.storageBytesStatus());
        count++;
    }
    assertEquals(2, count);
    // test list snapshots with selected fields
    snapshotPage = compute.listSnapshots(Compute.SnapshotListOption.filter(snapshotFilter), Compute.SnapshotListOption.fields(Compute.SnapshotField.CREATION_TIMESTAMP));
    snapshotIterator = snapshotPage.iterateAll();
    count = 0;
    while (snapshotIterator.hasNext()) {
        Snapshot remoteSnapshot = snapshotIterator.next();
        assertNull(remoteSnapshot.generatedId());
        assertTrue(diskSet.contains(remoteSnapshot.snapshotId().snapshot()));
        assertNotNull(remoteSnapshot.creationTimestamp());
        assertNull(remoteSnapshot.status());
        assertNull(remoteSnapshot.diskSizeGb());
        assertNull(remoteSnapshot.sourceDisk());
        assertNull(remoteSnapshot.sourceDiskId());
        assertNull(remoteSnapshot.storageBytes());
        assertNull(remoteSnapshot.storageBytesStatus());
        count++;
    }
    assertEquals(2, count);
    compute.deleteDisk(firstDiskId);
    compute.deleteDisk(secondDiskId);
    compute.deleteSnapshot(firstSnapshotId);
    compute.deleteSnapshot(secondSnapshotId);
}

4. ITComputeTest#testAggregatedListDisks()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testAggregatedListDisks() throws InterruptedException, TimeoutException {
    String prefix = BASE_RESOURCE_NAME + "list-aggregated-disk";
    String[] diskZones = { "us-central1-a", "us-east1-c" };
    String[] diskNames = { prefix + "1", prefix + "2" };
    DiskId firstDiskId = DiskId.of(diskZones[0], diskNames[0]);
    DiskId secondDiskId = DiskId.of(diskZones[1], diskNames[1]);
    DiskConfiguration configuration = StandardDiskConfiguration.of(DiskTypeId.of(ZONE, "pd-ssd"), 100L);
    Operation firstOperation = compute.create(DiskInfo.of(firstDiskId, configuration));
    Operation secondOperation = compute.create(DiskInfo.of(secondDiskId, configuration));
    firstOperation.waitFor();
    secondOperation.waitFor();
    Set<String> zoneSet = ImmutableSet.copyOf(diskZones);
    Set<String> diskSet = ImmutableSet.copyOf(diskNames);
    Compute.DiskFilter diskFilter = Compute.DiskFilter.equals(Compute.DiskField.NAME, prefix + "\\d");
    Page<Disk> diskPage = compute.listDisks(Compute.DiskAggregatedListOption.filter(diskFilter));
    Iterator<Disk> diskIterator = diskPage.iterateAll();
    int count = 0;
    while (diskIterator.hasNext()) {
        Disk remoteDisk = diskIterator.next();
        assertTrue(zoneSet.contains(remoteDisk.diskId().zone()));
        assertTrue(diskSet.contains(remoteDisk.diskId().disk()));
        assertEquals(DiskInfo.CreationStatus.READY, remoteDisk.creationStatus());
        assertNotNull(remoteDisk.creationTimestamp());
        assertNotNull(remoteDisk.generatedId());
        assertTrue(remoteDisk.configuration() instanceof StandardDiskConfiguration);
        StandardDiskConfiguration remoteConfiguration = remoteDisk.configuration();
        assertEquals(100L, (long) remoteConfiguration.sizeGb());
        assertEquals("pd-ssd", remoteConfiguration.diskType().type());
        assertEquals(DiskConfiguration.Type.STANDARD, remoteConfiguration.type());
        count++;
    }
    assertEquals(2, count);
    compute.deleteDisk(firstDiskId);
    compute.deleteDisk(secondDiskId);
}

5. ITComputeTest#testCreateGetResizeAndDeleteStandardDisk()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testCreateGetResizeAndDeleteStandardDisk() throws InterruptedException, TimeoutException {
    String name = BASE_RESOURCE_NAME + "create-and-get-standard-disk";
    DiskId diskId = DiskId.of(ZONE, name);
    DiskInfo diskInfo = DiskInfo.of(diskId, StandardDiskConfiguration.of(DiskTypeId.of(ZONE, "pd-ssd"), 100L));
    Operation operation = compute.create(diskInfo);
    operation.waitFor();
    // test get
    Disk remoteDisk = compute.getDisk(diskId);
    assertNotNull(remoteDisk);
    assertEquals(ZONE, remoteDisk.diskId().zone());
    assertEquals(diskId.disk(), remoteDisk.diskId().disk());
    assertNotNull(remoteDisk.creationTimestamp());
    assertNotNull(remoteDisk.generatedId());
    assertTrue(remoteDisk.configuration() instanceof StandardDiskConfiguration);
    StandardDiskConfiguration remoteConfiguration = remoteDisk.configuration();
    assertEquals(100L, (long) remoteConfiguration.sizeGb());
    assertEquals("pd-ssd", remoteConfiguration.diskType().type());
    assertEquals(DiskConfiguration.Type.STANDARD, remoteConfiguration.type());
    assertNull(remoteDisk.lastAttachTimestamp());
    assertNull(remoteDisk.lastDetachTimestamp());
    operation = remoteDisk.resize(200L);
    operation.waitFor();
    // test resize and get with selected fields
    remoteDisk = compute.getDisk(diskId, Compute.DiskOption.fields(Compute.DiskField.SIZE_GB));
    assertNotNull(remoteDisk);
    assertEquals(ZONE, remoteDisk.diskId().zone());
    assertEquals(diskId.disk(), remoteDisk.diskId().disk());
    assertNull(remoteDisk.creationTimestamp());
    assertNull(remoteDisk.generatedId());
    assertTrue(remoteDisk.configuration() instanceof StandardDiskConfiguration);
    remoteConfiguration = remoteDisk.configuration();
    assertEquals(200L, (long) remoteConfiguration.sizeGb());
    assertEquals("pd-ssd", remoteConfiguration.diskType().type());
    assertEquals(DiskConfiguration.Type.STANDARD, remoteConfiguration.type());
    assertNull(remoteDisk.lastAttachTimestamp());
    assertNull(remoteDisk.lastDetachTimestamp());
    operation = remoteDisk.delete();
    operation.waitFor();
    assertNull(compute.getDisk(diskId));
}

6. ITComputeTest#testCreateGetAndDeleteImageDisk()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testCreateGetAndDeleteImageDisk() throws InterruptedException, TimeoutException {
    String name = BASE_RESOURCE_NAME + "create-and-get-image-disk";
    DiskId diskId = DiskId.of(ZONE, name);
    DiskInfo diskInfo = DiskInfo.of(diskId, ImageDiskConfiguration.of(IMAGE_ID));
    Operation operation = compute.create(diskInfo);
    operation.waitFor();
    // test get
    Disk remoteDisk = compute.getDisk(diskId);
    assertNotNull(remoteDisk);
    assertEquals(ZONE, remoteDisk.diskId().zone());
    assertEquals(diskId.disk(), remoteDisk.diskId().disk());
    assertEquals(DiskInfo.CreationStatus.READY, remoteDisk.creationStatus());
    assertNotNull(remoteDisk.creationTimestamp());
    assertNotNull(remoteDisk.generatedId());
    assertTrue(remoteDisk.configuration() instanceof ImageDiskConfiguration);
    ImageDiskConfiguration remoteConfiguration = remoteDisk.configuration();
    assertEquals(IMAGE_ID, remoteConfiguration.sourceImage());
    assertNotNull(remoteConfiguration.sourceImageId());
    assertEquals(DiskConfiguration.Type.IMAGE, remoteConfiguration.type());
    assertNotNull(remoteConfiguration.sizeGb());
    assertEquals("pd-standard", remoteConfiguration.diskType().type());
    assertNull(remoteDisk.lastAttachTimestamp());
    assertNull(remoteDisk.lastDetachTimestamp());
    // test get with selected fields
    remoteDisk = compute.getDisk(diskId, Compute.DiskOption.fields());
    assertNotNull(remoteDisk);
    assertEquals(ZONE, remoteDisk.diskId().zone());
    assertEquals(diskId.disk(), remoteDisk.diskId().disk());
    assertNull(remoteDisk.creationTimestamp());
    assertNull(remoteDisk.generatedId());
    assertTrue(remoteDisk.configuration() instanceof ImageDiskConfiguration);
    remoteConfiguration = remoteDisk.configuration();
    assertEquals(IMAGE_ID, remoteConfiguration.sourceImage());
    assertNull(remoteConfiguration.sourceImageId());
    assertEquals(DiskConfiguration.Type.IMAGE, remoteConfiguration.type());
    assertNull(remoteConfiguration.sizeGb());
    assertEquals("pd-standard", remoteConfiguration.diskType().type());
    assertNull(remoteDisk.lastAttachTimestamp());
    assertNull(remoteDisk.lastDetachTimestamp());
    operation = remoteDisk.delete();
    operation.waitFor();
    assertNull(compute.getDisk(diskId));
}

7. ITComputeTest#testCreateGetAndDeprecateImage()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testCreateGetAndDeprecateImage() throws InterruptedException, TimeoutException {
    String diskName = BASE_RESOURCE_NAME + "create-and-get-image-disk";
    String imageName = BASE_RESOURCE_NAME + "create-and-get-image";
    DiskId diskId = DiskId.of(ZONE, diskName);
    ImageId imageId = ImageId.of(imageName);
    DiskInfo diskInfo = DiskInfo.of(diskId, StandardDiskConfiguration.of(DiskTypeId.of(ZONE, "pd-ssd"), 100L));
    Operation operation = compute.create(diskInfo);
    operation.waitFor();
    Disk remoteDisk = compute.getDisk(diskId);
    ImageInfo imageInfo = ImageInfo.of(imageId, DiskImageConfiguration.of(diskId));
    operation = compute.create(imageInfo);
    operation.waitFor();
    // test get image with selected fields
    Image image = compute.getImage(imageId, Compute.ImageOption.fields(Compute.ImageField.CREATION_TIMESTAMP));
    assertNull(image.generatedId());
    assertNotNull(image.imageId());
    assertNotNull(image.creationTimestamp());
    assertNull(image.description());
    assertNotNull(image.configuration());
    assertTrue(image.configuration() instanceof DiskImageConfiguration);
    DiskImageConfiguration remoteConfiguration = image.configuration();
    Assert.assertEquals(ImageConfiguration.Type.DISK, remoteConfiguration.type());
    assertEquals(diskName, remoteConfiguration.sourceDisk().disk());
    assertNull(image.status());
    assertNull(image.diskSizeGb());
    assertNull(image.licenses());
    assertNull(image.deprecationStatus());
    // test get image
    image = compute.getImage(imageId);
    assertNotNull(image.generatedId());
    assertNotNull(image.imageId());
    assertNotNull(image.creationTimestamp());
    assertNotNull(image.configuration());
    assertTrue(image.configuration() instanceof DiskImageConfiguration);
    remoteConfiguration = image.configuration();
    assertEquals(ImageConfiguration.Type.DISK, remoteConfiguration.type());
    assertEquals(diskName, remoteConfiguration.sourceDisk().disk());
    assertEquals(100L, (long) image.diskSizeGb());
    assertNotNull(image.status());
    assertNull(image.deprecationStatus());
    // test deprecate image
    DeprecationStatus<ImageId> deprecationStatus = DeprecationStatus.builder(DeprecationStatus.Status.DEPRECATED, imageId).deprecated(System.currentTimeMillis()).build();
    operation = image.deprecate(deprecationStatus);
    operation.waitFor();
    image = compute.getImage(imageId);
    assertEquals(deprecationStatus, image.deprecationStatus());
    remoteDisk.delete();
    operation = image.delete();
    operation.waitFor();
    assertNull(compute.getImage(imageId));
}

8. ITComputeTest#testAttachAndDetachDisk()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testAttachAndDetachDisk() throws InterruptedException, TimeoutException {
    String instanceName = BASE_RESOURCE_NAME + "attach-and-detach-disk-instance";
    String diskName = BASE_RESOURCE_NAME + "attach-and-detach-disk";
    InstanceId instanceId = InstanceId.of(ZONE, instanceName);
    NetworkId networkId = NetworkId.of("default");
    NetworkInterface networkInterface = NetworkInterface.builder(networkId).build();
    AttachedDisk disk = AttachedDisk.of("dev0", AttachedDisk.CreateDiskConfiguration.builder(IMAGE_ID).autoDelete(true).build());
    InstanceInfo instanceInfo = InstanceInfo.builder(instanceId, MachineTypeId.of(ZONE, MACHINE_TYPE)).attachedDisks(disk).networkInterfaces(networkInterface).build();
    Operation instanceOperation = compute.create(instanceInfo);
    DiskId diskId = DiskId.of(ZONE, diskName);
    Operation diskOperation = compute.create(DiskInfo.of(diskId, StandardDiskConfiguration.of(DiskTypeId.of(ZONE, "pd-ssd"))));
    instanceOperation.waitFor();
    diskOperation.waitFor();
    Instance remoteInstance = compute.getInstance(instanceId);
    // test attach disk
    instanceOperation = remoteInstance.attachDisk("dev1", AttachedDisk.PersistentDiskConfiguration.builder(diskId).build());
    instanceOperation.waitFor();
    remoteInstance = compute.getInstance(instanceId);
    Set<String> deviceSet = ImmutableSet.of("dev0", "dev1");
    assertEquals(2, remoteInstance.attachedDisks().size());
    for (AttachedDisk remoteAttachedDisk : remoteInstance.attachedDisks()) {
        assertTrue(deviceSet.contains(remoteAttachedDisk.deviceName()));
    }
    // test set disk auto-delete
    instanceOperation = remoteInstance.setDiskAutoDelete("dev1", true);
    instanceOperation.waitFor();
    remoteInstance = compute.getInstance(instanceId);
    assertEquals(2, remoteInstance.attachedDisks().size());
    for (AttachedDisk remoteAttachedDisk : remoteInstance.attachedDisks()) {
        assertTrue(deviceSet.contains(remoteAttachedDisk.deviceName()));
        assertTrue(remoteAttachedDisk.configuration().autoDelete());
    }
    // test detach disk
    instanceOperation = remoteInstance.detachDisk("dev1");
    instanceOperation.waitFor();
    remoteInstance = compute.getInstance(instanceId);
    assertEquals(1, remoteInstance.attachedDisks().size());
    assertEquals("dev0", remoteInstance.attachedDisks().get(0).deviceName());
    remoteInstance.delete();
    compute.deleteDisk(diskId);
}

9. CreateAddressDiskAndInstance#main()

View license
public static void main(String... args) throws InterruptedException, TimeoutException {
    // Create a service object
    // Credentials are inferred from the environment.
    Compute compute = ComputeOptions.defaultInstance().service();
    // Create an external region address
    RegionAddressId addressId = RegionAddressId.of("us-central1", "test-address");
    Operation operation = compute.create(AddressInfo.of(addressId));
    // Wait for operation to complete
    operation = operation.waitFor();
    if (operation.errors() == null) {
        System.out.println("Address " + addressId + " was successfully created");
    } else {
        // inspect operation.errors()
        throw new RuntimeException("Address creation failed");
    }
    // Create a persistent disk
    ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329");
    DiskId diskId = DiskId.of("us-central1-a", "test-disk");
    ImageDiskConfiguration diskConfiguration = ImageDiskConfiguration.of(imageId);
    DiskInfo disk = DiskInfo.of(diskId, diskConfiguration);
    operation = compute.create(disk);
    // Wait for operation to complete
    operation = operation.waitFor();
    if (operation.errors() == null) {
        System.out.println("Disk " + diskId + " was successfully created");
    } else {
        // inspect operation.errors()
        throw new RuntimeException("Disk creation failed");
    }
    // Create a virtual machine instance
    Address externalIp = compute.getAddress(addressId);
    InstanceId instanceId = InstanceId.of("us-central1-a", "test-instance");
    NetworkId networkId = NetworkId.of("default");
    PersistentDiskConfiguration attachConfiguration = PersistentDiskConfiguration.builder(diskId).boot(true).build();
    AttachedDisk attachedDisk = AttachedDisk.of("dev0", attachConfiguration);
    NetworkInterface networkInterface = NetworkInterface.builder(networkId).accessConfigurations(AccessConfig.of(externalIp.address())).build();
    MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
    InstanceInfo instance = InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface);
    operation = compute.create(instance);
    // Wait for operation to complete
    operation = operation.waitFor();
    if (operation.errors() == null) {
        System.out.println("Instance " + instanceId + " was successfully created");
    } else {
        // inspect operation.errors()
        throw new RuntimeException("Instance creation failed");
    }
}