@Override ImageInfo parse(String... args) throws Exception { String message; if (args.length == 3) { String image = args[0]; String zone = args[1]; String disk = args[2]; return ImageInfo.of(ImageId.of(image), DiskImageConfiguration.of(DiskId.of(zone, disk))); } else if (args.length > 3) { message = "Too many arguments."; } else { message = "Missing required arguments."; } throw new IllegalArgumentException(message); }
/** * Creates an image for this disk given the image's name. * * @return a global operation if the image creation was successfully requested * @throws ComputeException upon failure */ public Operation createImage(String image, OperationOption... options) { ImageInfo imageInfo = ImageInfo.of(ImageId.of(image), DiskImageConfiguration.of(getDiskId())); return compute.create(imageInfo, options); }
@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 testCreateImageWithOptions() { 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, Compute.OperationOption.fields())).andReturn(operation); replay(compute); initializeDisk(); assertSame(operation, disk.createImage(IMAGE.getImage(), Compute.OperationOption.fields())); }
@Test public void testToAndFromPb() { compareImageInfo(STORAGE_IMAGE, ImageInfo.fromPb(STORAGE_IMAGE.toPb())); compareImageInfo(DISK_IMAGE, ImageInfo.fromPb(DISK_IMAGE.toPb())); ImageInfo imageInfo = ImageInfo.of(IMAGE_ID, StorageImageConfiguration.of(STORAGE_SOURCE)); compareImageInfo(imageInfo, ImageInfo.fromPb(imageInfo.toPb())); imageInfo = ImageInfo.of(IMAGE_ID, DiskImageConfiguration.of(SOURCE_DISK)); compareImageInfo(imageInfo, ImageInfo.fromPb(imageInfo.toPb())); }
@Test public void testToBuilderIncomplete() { initializeExpectedImage(6); ImageInfo imageInfo = ImageInfo.of(IMAGE_ID, DISK_CONFIGURATION); Image image = new Image(serviceMockReturnsOptions, new ImageInfo.BuilderImpl(imageInfo)); compareImage(image, image.toBuilder().build()); }
@Test public void testOf() { ImageInfo imageInfo = ImageInfo.of(IMAGE_ID, STORAGE_CONFIGURATION); assertEquals(IMAGE_ID, imageInfo.getImageId()); assertEquals(STORAGE_CONFIGURATION, imageInfo.getConfiguration()); assertNull(imageInfo.getGeneratedId()); assertNull(imageInfo.getCreationTimestamp()); assertNull(imageInfo.getDescription()); assertNull(imageInfo.getStatus()); assertNull(imageInfo.getDiskSizeGb()); assertNull(imageInfo.getLicenses()); assertNull(imageInfo.getDeprecationStatus()); imageInfo = ImageInfo.of(IMAGE_ID, DISK_CONFIGURATION); assertEquals(IMAGE_ID, imageInfo.getImageId()); assertEquals(DISK_CONFIGURATION, imageInfo.getConfiguration()); assertNull(imageInfo.getGeneratedId()); assertNull(imageInfo.getCreationTimestamp()); assertNull(imageInfo.getDescription()); assertNull(imageInfo.getStatus()); assertNull(imageInfo.getDiskSizeGb()); assertNull(imageInfo.getLicenses()); assertNull(imageInfo.getDeprecationStatus()); }
operation.waitFor(); Disk remoteDisk = compute.getDisk(diskId); ImageInfo imageInfo = ImageInfo.of(imageId, DiskImageConfiguration.of(diskId)); operation = compute.create(imageInfo); operation.waitFor();