@Test public void testReloadWithOptions() throws Exception { initializeExpectedDisk(5); expect(compute.getOptions()).andReturn(mockOptions); expect(compute.getDisk(DISK_ID, Compute.DiskOption.fields())).andReturn(imageDisk); replay(compute); initializeDisk(); Disk updatedDisk = disk.reload(Compute.DiskOption.fields()); compareDisk(imageDisk, updatedDisk); verify(compute); }
@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); }
assertNull(remoteDisk.getLastDetachTimestamp()); remoteDisk = compute.getDisk(snapshotDiskId, Compute.DiskOption.fields()); assertNotNull(remoteDisk); assertEquals(ZONE, remoteDisk.getDiskId().getZone());
operation.waitFor(); remoteDisk = compute.getDisk(diskId, Compute.DiskOption.fields(Compute.DiskField.SIZE_GB)); assertNotNull(remoteDisk); assertEquals(ZONE, remoteDisk.getDiskId().getZone());
assertNull(remoteDisk.getLastDetachTimestamp()); remoteDisk = compute.getDisk(diskId, Compute.DiskOption.fields()); assertNotNull(remoteDisk); assertEquals(ZONE, remoteDisk.getDiskId().getZone());
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); } } } }
/** * Returns an option to specify the disk's fields to be returned by the RPC call. If this option * is not provided, all disk's fields are returned. {@code DiskOption.fields} can be used to * specify only the fields of interest. {@link Disk#getDiskId()}, {@link * DiskConfiguration#getDiskType()} and either {@link * SnapshotDiskConfiguration#getSourceSnapshot()} or {@link * ImageDiskConfiguration#getSourceImage()} are always returned, even if not specified. */ public static DiskOption fields(DiskField... fields) { return new DiskOption( ComputeRpc.Option.FIELDS, Helper.selector(DiskField.REQUIRED_FIELDS, fields)); } }
@Test public void testExists_True() throws Exception { initializeExpectedDisk(3); Compute.DiskOption[] expectedOptions = {Compute.DiskOption.fields()}; expect(compute.getOptions()).andReturn(mockOptions); expect(compute.getDisk(DISK_ID, expectedOptions)).andReturn(imageDisk); replay(compute); initializeDisk(); assertTrue(disk.exists()); verify(compute); }
@Test public void testExists_False() throws Exception { initializeExpectedDisk(3); Compute.DiskOption[] expectedOptions = {Compute.DiskOption.fields()}; expect(compute.getOptions()).andReturn(mockOptions); expect(compute.getDisk(DISK_ID, expectedOptions)).andReturn(null); replay(compute); initializeDisk(); assertFalse(disk.exists()); verify(compute); }
/** * Checks if this disk exists. * * @return {@code true} if this disk exists, {@code false} otherwise * @throws ComputeException upon failure */ public boolean exists() { return reload(DiskOption.fields()) != null; }