@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())); }
public void compareImageInfo(ImageInfo expected, ImageInfo value) { assertEquals(expected, value); assertEquals(expected.getGeneratedId(), value.getGeneratedId()); assertEquals(expected.getImageId(), value.getImageId()); assertEquals(expected.getCreationTimestamp(), value.getCreationTimestamp()); assertEquals(expected.getDescription(), value.getDescription()); assertEquals(expected.getConfiguration(), value.getConfiguration()); assertEquals(expected.getStatus(), value.getStatus()); assertEquals(expected.getDiskSizeGb(), value.getDiskSizeGb()); assertEquals(expected.getLicenses(), value.getLicenses()); assertEquals(expected.getDeprecationStatus(), value.getDeprecationStatus()); assertEquals(expected.hashCode(), value.hashCode()); } }
/** * Returns an {@code ImageInfo} object given the image identity and an image configuration. Use * {@link DiskImageConfiguration} to create an image from an existing disk. Use {@link * StorageImageConfiguration} to create an image from a file stored in Google Cloud Storage. */ public static ImageInfo of(ImageId imageId, ImageConfiguration configuration) { return newBuilder(imageId, configuration).build(); }
@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); }
@Override public Image apply(ImageInfo image) { return image.toPb(); } };
@Test public void testSetProjectId() { ImageInfo imageInfo = DISK_IMAGE .toBuilder() .setImageId(ImageId.of("image")) .setConfiguration( DISK_CONFIGURATION.toBuilder().setSourceDisk(DiskId.of("zone", "disk")).build()) .build(); compareImageInfo(DISK_IMAGE, imageInfo.setProjectId("project")); }
@Test public void testToBuilder() { compareImageInfo(STORAGE_IMAGE, STORAGE_IMAGE.toBuilder().build()); compareImageInfo(DISK_IMAGE, DISK_IMAGE.toBuilder().build()); ImageInfo imageInfo = STORAGE_IMAGE.toBuilder().setDescription("newDescription").build(); assertEquals("newDescription", imageInfo.getDescription()); imageInfo = imageInfo.toBuilder().setDescription("description").build(); compareImageInfo(STORAGE_IMAGE, imageInfo); }
@Override public ImageInfo apply(Image pb) { return ImageInfo.fromPb(pb); } };
@Override public final int hashCode() { return Objects.hash(super.hashCode(), options); }
@Override public void run(Compute compute, ImageInfo image) throws InterruptedException { Operation operation = compute.create(image); while (!operation.isDone()) { System.out.printf( "Waiting for operation %s to complete%n", operation.getOperationId().getOperation()); Thread.sleep(1000L); } operation = operation.reload(); if (operation.getErrors() == null) { System.out.printf("Image %s was created%n", image.getImageId()); } else { System.out.printf("Creation of image %s failed%n", image.getImageId()); System.out.printf("Error: %s%n", operation.getErrors()); } }
ImageInfo setProjectId(String projectId) { return toBuilder() .setImageId(imageId.setProjectId(projectId)) .setConfiguration(configuration.setProjectId(projectId)) .build(); }
@Override public Operation create(ImageInfo image, OperationOption... options) { final ImageInfo completeImage = image.setProjectId(getOptions().getProjectId()); final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options); try { com.google.api.services.compute.model.Operation answer = runWithRetries( new Callable<com.google.api.services.compute.model.Operation>() { @Override public com.google.api.services.compute.model.Operation call() { return computeRpc.createImage(completeImage.toPb(), optionsMap); } }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); } }
@Override public ImageInfo build() { return new ImageInfo(this); } }
/** * 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 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()); }
@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())); }
@Override public com.google.api.services.compute.model.Operation call() { return computeRpc.createImage(completeImage.toPb(), optionsMap); } },
/** * Creates an image for this disk given the image's name and description. * * @return a global operation if the image creation was successfully requested * @throws ComputeException upon failure */ public Operation createImage(String image, String description, OperationOption... options) { ImageInfo imageInfo = ImageInfo.newBuilder(ImageId.of(image), DiskImageConfiguration.of(getDiskId())) .setDescription(description) .build(); return compute.create(imageInfo, options); }