@Override ImageId parse(String... args) throws Exception { String message; if (args.length == 1) { return ImageId.of(args[0]); } else if (args.length > 1) { message = "Too many arguments."; } else { message = "Missing required image id."; } throw new IllegalArgumentException(message); }
static ImageId fromUrl(String url) { Matcher matcher = PATTERN.matcher(url); if (!matcher.matches()) { throw new IllegalArgumentException(url + " is not a valid image URL"); } return ImageId.of(matcher.group(1), matcher.group(2)); } }
@Override ImageId setProjectId(String projectId) { if (getProject() != null) { return this; } return ImageId.of(projectId, image); }
@Override DiskInfo parse(String... args) throws Exception { if (args.length == 3) { DiskId diskId = parseDiskId(args); return DiskInfo.of(diskId, ImageDiskConfiguration.of(ImageId.of(args[2]))); } else if (args.length == 4) { DiskId diskId = parseDiskId(args); return DiskInfo.of(diskId, ImageDiskConfiguration.of(ImageId.of(args[2], args[3]))); } else if (args.length > 4) { throw new IllegalArgumentException("Too many arguments."); } else { throw new IllegalArgumentException("Missing required arguments."); } }
@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); }
/** * 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); }
@Test public void testDeleteImageWithSelectedFields_Operation() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( computeRpcMock.deleteImage( eq(PROJECT), eq(IMAGE_ID.getImage()), capture(capturedOptions))) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.deleteImage(ImageId.of("image"), OPERATION_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(OPERATION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("id")); assertTrue(selector.contains("description")); assertEquals(23, selector.length()); assertEquals(globalOperation, operation); }
@Test public void testOf() { ImageId imageId = ImageId.of(PROJECT, NAME); assertEquals(PROJECT, imageId.getProject()); assertEquals(NAME, imageId.getImage()); assertEquals(URL, imageId.getSelfLink()); imageId = ImageId.of(NAME); assertNull(imageId.getProject()); assertEquals(NAME, imageId.getImage()); }
@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 testMatchesUrl() { assertTrue(ImageId.matchesUrl(ImageId.of(PROJECT, NAME).getSelfLink())); assertFalse(ImageId.matchesUrl("notMatchingUrl")); }
@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 testCreateImageWithDescription() { 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.newBuilder(imageId, DiskImageConfiguration.of(DISK_ID)) .setDescription("description") .build(); expect(compute.create(image)).andReturn(operation); replay(compute); initializeDisk(); assertSame(operation, disk.createImage(IMAGE.getImage(), "description")); }
@Test public void testToAndFromUrl() { ImageId imageId = ImageId.of(PROJECT, NAME); compareImageId(imageId, ImageId.fromUrl(imageId.getSelfLink())); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("notMatchingUrl is not a valid image URL"); ImageId.fromUrl("notMatchingUrl"); }
public static void main(String... args) throws InterruptedException, TimeoutException { Compute compute = ComputeOptions.getDefaultInstance().getService(); ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329"); NetworkId networkId = NetworkId.of("default"); AttachedDisk attachedDisk = AttachedDisk.of(AttachedDisk.CreateDiskConfiguration.of(imageId)); NetworkInterface networkInterface = NetworkInterface.of(networkId); InstanceId instanceId = InstanceId.of("us-central1-a", "instance-name"); MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1"); Operation operation = compute.create(InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface)); operation = operation.waitFor(); if (operation.getErrors() == null) { // use instance Instance instance = compute.getInstance(instanceId); } } }
@Test public void testCreateImageWithDescriptionAndOptions() { 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.newBuilder(imageId, DiskImageConfiguration.of(DISK_ID)) .setDescription("description") .build(); expect(compute.create(image, Compute.OperationOption.fields())).andReturn(operation); replay(compute); initializeDisk(); assertSame( operation, disk.createImage(IMAGE.getImage(), "description", Compute.OperationOption.fields())); }
@Test public void testSetProjectId() { ImageDiskConfiguration diskConfiguration = DISK_CONFIGURATION .toBuilder() .setDiskType(DiskTypeId.of(DISK_TYPE.getZone(), DISK_TYPE.getType())) .setSourceImage(ImageId.of(IMAGE.getImage())) .build(); compareImageDiskConfiguration(DISK_CONFIGURATION, diskConfiguration.setProjectId("project")); }
@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 testConfigurationSetProjectId() { PersistentDiskConfiguration persistentConfiguration = PersistentDiskConfiguration.of(DiskId.of("zone", "disk")); comparePersistentDiskConfiguration( PersistentDiskConfiguration.of(DiskId.of("project", "zone", "disk")), persistentConfiguration.setProjectId("project")); ScratchDiskConfiguration scratchDiskConfiguration = ScratchDiskConfiguration.of(DiskTypeId.of("zone", "diskType")); compareScratchDiskConfiguration( ScratchDiskConfiguration.of(DiskTypeId.of("project", "zone", "diskType")), scratchDiskConfiguration.setProjectId("project")); CreateDiskConfiguration createDiskConfiguration = CREATE_DISK_CONFIGURATION .toBuilder() .setDiskType(DiskTypeId.of("zone", "diskType")) .setSourceImage(ImageId.of("image")) .build(); compareCreateDiskConfiguration( CREATE_DISK_CONFIGURATION, createDiskConfiguration.setProjectId("project")); }