Refine search
@Override public Disk build() { return new Disk(compute, infoBuilder); } }
@Test public void testToAndFromPb() { initializeExpectedDisk(24); compareDisk(standardDisk, Disk.fromPb(serviceMockReturnsOptions, standardDisk.toPb())); compareDisk(imageDisk, Disk.fromPb(serviceMockReturnsOptions, imageDisk.toPb())); compareDisk(snapshotDisk, Disk.fromPb(serviceMockReturnsOptions, snapshotDisk.toPb())); Disk disk = new Disk.Builder(serviceMockReturnsOptions, DISK_ID, DISK_CONFIGURATION).build(); compareDisk(disk, Disk.fromPb(serviceMockReturnsOptions, disk.toPb())); disk = new Disk.Builder(serviceMockReturnsOptions, DISK_ID, SNAPSHOT_DISK_CONFIGURATION).build(); compareDisk(disk, Disk.fromPb(serviceMockReturnsOptions, disk.toPb())); disk = new Disk.Builder(serviceMockReturnsOptions, DISK_ID, IMAGE_DISK_CONFIGURATION).build(); compareDisk(disk, Disk.fromPb(serviceMockReturnsOptions, disk.toPb())); }
public void compareDisk(Disk expected, Disk value) { assertEquals(expected, value); assertEquals(expected.getCompute().getOptions(), value.getCompute().getOptions()); assertEquals(expected.getDiskId(), value.getDiskId()); assertEquals(expected.getConfiguration(), value.getConfiguration()); assertEquals(expected.getGeneratedId(), value.getGeneratedId()); assertEquals(expected.getCreationTimestamp(), value.getCreationTimestamp()); assertEquals(expected.getCreationStatus(), value.getCreationStatus()); assertEquals(expected.getDescription(), value.getDescription()); assertEquals(expected.getLicenses(), value.getLicenses()); assertEquals(expected.getAttachedInstances(), value.getAttachedInstances()); assertEquals(expected.getLastAttachTimestamp(), value.getLastAttachTimestamp()); assertEquals(expected.getLastDetachTimestamp(), value.getLastDetachTimestamp()); assertEquals(expected.hashCode(), value.hashCode()); } }
@Test public void testReload() throws Exception { initializeExpectedDisk(5); expect(compute.getOptions()).andReturn(mockOptions); expect(compute.getDisk(DISK_ID)).andReturn(imageDisk); replay(compute); initializeDisk(); Disk updatedDisk = disk.reload(); compareDisk(imageDisk, updatedDisk); verify(compute); }
@Test public void testAggregatedListDisks() { String cursor = "cursor"; compute = options.getService(); ImmutableList<Disk> diskList = ImmutableList.of( new Disk(compute, new DiskInfo.BuilderImpl(DISK)), new Disk(compute, new DiskInfo.BuilderImpl(DISK))); Tuple<String, Iterable<com.google.api.services.compute.model.Disk>> result = Tuple.of(cursor, Iterables.transform(diskList, DiskInfo.TO_PB_FUNCTION)); EasyMock.expect(computeRpcMock.listDisks(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); Page<Disk> page = compute.listDisks(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(diskList.toArray(), Iterables.toArray(page.getValues(), Disk.class)); }
@Test public void testCreateSnapshot() { initializeExpectedDisk(4); expect(compute.getOptions()).andReturn(mockOptions); Operation operation = new Operation.Builder(serviceMockReturnsOptions) .setOperationId(ZoneOperationId.of("project", "zone", "op")) .build(); SnapshotId snapshotId = SnapshotId.of(SNAPSHOT.getSnapshot()); SnapshotInfo snapshot = SnapshotInfo.newBuilder(snapshotId, DISK_ID).build(); expect(compute.create(snapshot)).andReturn(operation); replay(compute); initializeDisk(); assertSame(operation, disk.createSnapshot(SNAPSHOT.getSnapshot())); }
@Test public void testCreateImage() { initializeExpectedDisk(4); expect(compute.getOptions()).andReturn(mockOptions); Operation operation = new Operation.Builder(serviceMockReturnsOptions) .setOperationId(GlobalOperationId.of("project", "op")) .build(); ImageId imageId = ImageId.of(IMAGE.getImage()); ImageInfo image = ImageInfo.of(imageId, DiskImageConfiguration.of(DISK_ID)); expect(compute.create(image)).andReturn(operation); replay(compute); initializeDisk(); assertSame(operation, disk.createImage(IMAGE.getImage())); }
@Test public void testDeleteNull() { initializeExpectedDisk(3); expect(compute.getOptions()).andReturn(mockOptions); expect(compute.deleteDisk(DISK_ID)).andReturn(null); replay(compute); initializeDisk(); assertNull(disk.delete()); }
@Test public void testGetDiskWithSelectedFields() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( computeRpcMock.getDisk( eq(DISK_ID.getZone()), eq(DISK_ID.getDisk()), capture(capturedOptions))) .andReturn(DISK.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Disk disk = compute.getDisk(DISK_ID, DISK_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(DISK_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("type")); assertTrue(selector.contains("sourceImage")); assertTrue(selector.contains("sourceSnapshot")); assertTrue(selector.contains("id")); assertTrue(selector.contains("description")); assertEquals(55, selector.length()); assertEquals(new Disk(compute, new DiskInfo.BuilderImpl(DISK)), disk); }
@Test public void testGetDisk() { EasyMock.expect(computeRpcMock.getDisk(DISK_ID.getZone(), DISK_ID.getDisk(), EMPTY_RPC_OPTIONS)) .andReturn(DISK.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Disk disk = compute.getDisk(DISK_ID); assertEquals(new Disk(compute, new DiskInfo.BuilderImpl(DISK)), disk); }
Disk remoteDisk = compute.getDisk(diskId); resourceCleaner.add(diskId); assertNotNull(remoteDisk); assertEquals(ZONE, remoteDisk.getDiskId().getZone()); assertEquals(diskId.getDisk(), remoteDisk.getDiskId().getDisk()); assertNotNull(remoteDisk.getCreationTimestamp()); assertNotNull(remoteDisk.getGeneratedId()); assertTrue(remoteDisk.getConfiguration() instanceof StandardDiskConfiguration); StandardDiskConfiguration remoteConfiguration = remoteDisk.getConfiguration(); assertEquals(100L, (long) remoteConfiguration.getSizeGb()); assertEquals("pd-ssd", remoteConfiguration.getDiskType().getType()); assertEquals(DiskConfiguration.Type.STANDARD, remoteConfiguration.getType()); assertNull(remoteDisk.getLastAttachTimestamp()); assertNull(remoteDisk.getLastDetachTimestamp()); operation = remoteDisk.resize(200L); operation.waitFor(); remoteDisk = compute.getDisk(diskId, Compute.DiskOption.fields(Compute.DiskField.SIZE_GB)); assertNotNull(remoteDisk); assertEquals(ZONE, remoteDisk.getDiskId().getZone()); assertEquals(diskId.getDisk(), remoteDisk.getDiskId().getDisk()); assertNull(remoteDisk.getCreationTimestamp()); assertNull(remoteDisk.getGeneratedId()); assertTrue(remoteDisk.getConfiguration() instanceof StandardDiskConfiguration); remoteConfiguration = remoteDisk.getConfiguration(); assertEquals(200L, (long) remoteConfiguration.getSizeGb()); assertEquals("pd-ssd", remoteConfiguration.getDiskType().getType()); assertEquals(DiskConfiguration.Type.STANDARD, remoteConfiguration.getType()); assertNull(remoteDisk.getLastAttachTimestamp());
Page<Disk> diskPage = compute.listDisks(ZONE, Compute.DiskListOption.filter(diskFilter)); Iterator<Disk> diskIterator = diskPage.iterateAll().iterator(); int count = 0; while (diskIterator.hasNext()) { Disk remoteDisk = diskIterator.next(); assertEquals(ZONE, remoteDisk.getDiskId().getZone()); assertTrue(diskSet.contains(remoteDisk.getDiskId().getDisk())); assertEquals(DiskInfo.CreationStatus.READY, remoteDisk.getCreationStatus()); assertNotNull(remoteDisk.getCreationTimestamp()); assertNotNull(remoteDisk.getGeneratedId()); assertTrue(remoteDisk.getConfiguration() instanceof StandardDiskConfiguration); StandardDiskConfiguration remoteConfiguration = remoteDisk.getConfiguration(); assertEquals(100L, (long) remoteConfiguration.getSizeGb()); assertEquals("pd-ssd", remoteConfiguration.getDiskType().getType()); assertEquals(DiskConfiguration.Type.STANDARD, remoteConfiguration.getType()); assertNull(remoteDisk.getLastAttachTimestamp()); assertNull(remoteDisk.getLastDetachTimestamp()); count++; while (diskIterator.hasNext()) { Disk remoteDisk = diskIterator.next(); assertEquals(ZONE, remoteDisk.getDiskId().getZone()); assertTrue(diskSet.contains(remoteDisk.getDiskId().getDisk())); assertEquals(DiskInfo.CreationStatus.READY, remoteDisk.getCreationStatus()); assertNull(remoteDisk.getCreationTimestamp()); assertNull(remoteDisk.getGeneratedId()); assertTrue(remoteDisk.getConfiguration() instanceof StandardDiskConfiguration); StandardDiskConfiguration remoteConfiguration = remoteDisk.getConfiguration(); assertNull(remoteConfiguration.getSizeGb());
@Test public void testBuilder() { initializeExpectedDisk(4); assertEquals(DISK_ID, standardDisk.getDiskId()); assertEquals(GENERATED_ID, standardDisk.getGeneratedId()); assertEquals(DISK_CONFIGURATION, standardDisk.getConfiguration()); assertEquals(CREATION_TIMESTAMP, standardDisk.getCreationTimestamp()); assertEquals(CREATION_STATUS, standardDisk.getCreationStatus()); assertEquals(DESCRIPTION, standardDisk.getDescription()); assertEquals(LICENSES, standardDisk.getLicenses()); assertEquals(ATTACHED_INSTANCES, standardDisk.getAttachedInstances()); assertEquals(LAST_ATTACH_TIMESTAMP, standardDisk.getLastAttachTimestamp()); assertEquals(LAST_DETACH_TIMESTAMP, standardDisk.getLastDetachTimestamp()); assertSame(serviceMockReturnsOptions, standardDisk.getCompute()); assertEquals(DISK_ID, imageDisk.getDiskId()); assertEquals(GENERATED_ID, imageDisk.getGeneratedId()); assertEquals(IMAGE_DISK_CONFIGURATION, imageDisk.getConfiguration()); assertEquals(CREATION_TIMESTAMP, imageDisk.getCreationTimestamp()); assertEquals(CREATION_STATUS, imageDisk.getCreationStatus()); assertEquals(DESCRIPTION, imageDisk.getDescription()); assertEquals(LICENSES, imageDisk.getLicenses()); assertEquals(ATTACHED_INSTANCES, imageDisk.getAttachedInstances()); assertEquals(LAST_ATTACH_TIMESTAMP, imageDisk.getLastAttachTimestamp()); assertEquals(LAST_DETACH_TIMESTAMP, imageDisk.getLastDetachTimestamp()); assertSame(serviceMockReturnsOptions, imageDisk.getCompute()); assertEquals(DISK_ID, snapshotDisk.getDiskId()); assertEquals(GENERATED_ID, snapshotDisk.getGeneratedId()); assertEquals(SNAPSHOT_DISK_CONFIGURATION, snapshotDisk.getConfiguration()); assertEquals(CREATION_TIMESTAMP, snapshotDisk.getCreationTimestamp()); assertEquals(CREATION_STATUS, snapshotDisk.getCreationStatus());
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(); resourceCleaner.add(firstDiskId); 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().iterator(); int count = 0; while (diskIterator.hasNext()) { Disk remoteDisk = diskIterator.next(); assertTrue(zoneSet.contains(remoteDisk.getDiskId().getZone())); assertTrue(diskSet.contains(remoteDisk.getDiskId().getDisk())); assertEquals(DiskInfo.CreationStatus.READY, remoteDisk.getCreationStatus()); assertNotNull(remoteDisk.getCreationTimestamp()); assertNotNull(remoteDisk.getGeneratedId()); assertTrue(remoteDisk.getConfiguration() instanceof StandardDiskConfiguration); StandardDiskConfiguration remoteConfiguration = remoteDisk.getConfiguration(); assertEquals(100L, (long) remoteConfiguration.getSizeGb()); assertEquals("pd-ssd", remoteConfiguration.getDiskType().getType());
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(); resourceCleaner.add(diskId); image = compute.getImage(imageId); assertEquals(deprecationStatus, image.getDeprecationStatus()); remoteDisk.delete(); resourceCleaner.remove(diskId); operation = image.delete();
/** * Creates a snapshot for this disk given the snapshot's name. * * @return a zone operation for snapshot creation * @throws ComputeException upon failure */ public Operation createSnapshot(String snapshot, OperationOption... options) { return compute.create(SnapshotInfo.of(SnapshotId.of(snapshot), getDiskId()), options); }
public static void main(String... args) throws InterruptedException, TimeoutException { Compute compute = ComputeOptions.getDefaultInstance().getService(); 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.getErrors() == null) { // use snapshot Snapshot snapshot = compute.getSnapshot(snapshotName); } } } }
/** * Fetches current disk's latest information. Returns {@code null} if the disk does not exist. * * @param options disk options * @return a {@code Disk} object with latest information or {@code null} if not found * @throws ComputeException upon failure */ public Disk reload(DiskOption... options) { return compute.getDisk(getDiskId(), options); }
/** * Deletes this disk. * * @return a zone operation if the delete request was successfully sent, {@code null} if the disk * was not found * @throws ComputeException upon failure */ public Operation delete(OperationOption... options) { return compute.deleteDisk(getDiskId(), options); }
/** * Resizes this disk to the requested size. The new size must be larger than the previous one. * * @return a zone operation if the resize request was issued correctly, {@code null} if this disk * was not found * @throws ComputeException upon failure or if the new disk size is smaller than the previous one */ public Operation resize(long sizeGb, OperationOption... options) { return compute.resize(getDiskId(), sizeGb, options); }