@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); }
@Override public ImageId apply(String pb) { return ImageId.fromUrl(pb); } };
@Override public com.google.api.services.compute.model.Operation call() { return computeRpc.deleteImage( completeId.getProject(), completeId.getImage(), optionsMap); } },
@Override ImageId setProjectId(String projectId) { if (getProject() != null) { return this; } return ImageId.of(projectId, image); }
private void compareImageId(ImageId expected, ImageId value) { assertEquals(expected, value); assertEquals(expected.getProject(), expected.getProject()); assertEquals(expected.getImage(), expected.getImage()); assertEquals(expected.getSelfLink(), expected.getSelfLink()); assertEquals(expected.hashCode(), expected.hashCode()); } }
@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 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 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"); }
@Test public void testMatchesUrl() { assertTrue(ImageId.matchesUrl(ImageId.of(PROJECT, NAME).getSelfLink())); assertFalse(ImageId.matchesUrl("notMatchingUrl")); }
@Test public void testSetProjectId() { ImageId imageId = ImageId.of(PROJECT, NAME); assertSame(imageId, imageId.setProjectId(PROJECT)); compareImageId(imageId, ImageId.of(NAME).setProjectId(PROJECT)); }
@Override CreateDiskConfiguration setProjectId(String projectId) { Builder builder = toBuilder(); if (builder.diskType != null) { builder.setDiskType(diskType.setProjectId(projectId)); } if (builder.sourceImage != null) { builder.setSourceImage(sourceImage.setProjectId(projectId)); } return builder.build(); }
Image toPb() { Image imagePb = configuration.toPb(); if (generatedId != null) { imagePb.setId(new BigInteger(generatedId)); } if (creationTimestamp != null) { imagePb.setCreationTimestamp(TIMESTAMP_FORMATTER.print(creationTimestamp)); } imagePb.setName(imageId.getImage()); imagePb.setDescription(description); imagePb.setSelfLink(imageId.getSelfLink()); if (status != null) { imagePb.setStatus(status.name()); } imagePb.setDiskSizeGb(diskSizeGb); if (licenses != null) { imagePb.setLicenses(Lists.transform(licenses, LicenseId.TO_URL_FUNCTION)); } if (deprecationStatus != null) { imagePb.setDeprecated(deprecationStatus.toPb()); } return imagePb; }
@Override public String apply(ImageId imageId) { return imageId.getSelfLink(); } };
/** * Returns an image identity given the image name. The image name must be 1-63 characters long and * comply with RFC1035. Specifically, the name must match the regular expression {@code * [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter, and all * following characters must be a dash, lowercase letter, or digit, except the last character, * which cannot be a dash. * * @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> */ public static ImageId of(String image) { return new ImageId(null, image); }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof ImageId)) { return false; } ImageId other = (ImageId) obj; return baseEquals(other) && Objects.equals(image, other.image); }
@Override public int hashCode() { return Objects.hash(baseHashCode(), image); }
@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())); }
ImageInfo setProjectId(String projectId) { return toBuilder() .setImageId(imageId.setProjectId(projectId)) .setConfiguration(configuration.setProjectId(projectId)) .build(); }
@Override Disk toPb() { return super.toPb().setSourceImage(sourceImage.getSelfLink()).setSourceImageId(sourceImageId); }
/** * Returns an image identity given project and image names. The image name must be 1-63 characters * long and comply with RFC1035. Specifically, the name must match the regular expression {@code * [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter, and all * following characters must be a dash, lowercase letter, or digit, except the last character, * which cannot be a dash. * * @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> */ public static ImageId of(String project, String image) { return new ImageId(project, image); }