Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functional methods for images and Image class #826

Merged
merged 2 commits into from
Apr 2, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix image's delete and deprecate methods, document image's list and u…
…pdate tests
  • Loading branch information
mziccard committed Apr 1, 2016
commit c671888f94f89c4289b8a21a873b4e57083aa5b1
Original file line number Diff line number Diff line change
Expand Up @@ -1832,9 +1832,14 @@ public static ImageListOption fields(ImageField... fields) {
Image get(ImageId imageId, ImageOption... options);

/**
* Lists images in the provided project that are available to the current user.
* Lists images in the provided project that are available to the current user. This method can be
* used to list publicly-available images by providing the respective image project. Examples of
* image projects are: {@code centos-cloud}, {@code coreos-cloud}, {@code debian-cloud},
* {@code opensuse-cloud}, {@code rhel-cloud}, {@code suse-cloud}, {@code ubuntu-os-cloud} and
* {@code windows-cloud}. Attempting to delete or deprecate a publicly-available image will fail.
*
* @throws ComputeException upon failure
* @see <a href="https://cloud.google.com/compute/docs/operating-systems/">Operating Systems</a>
*/
Page<Image> listImages(String project, ImageListOption... options);

This comment was marked as spam.

This comment was marked as spam.


Expand All @@ -1850,7 +1855,7 @@ public static ImageListOption fields(ImageField... fields) {
*
* @return a global operation if the delete request was issued correctly, {@code null} if the
* image was not found
* @throws ComputeException upon failure
* @throws ComputeException upon failure or if {@code image} is a publicly-available image
*/
Operation delete(ImageId image, OperationOption... options);

Expand All @@ -1859,7 +1864,7 @@ public static ImageListOption fields(ImageField... fields) {
*
* @return a global operation if the deprecation request was issued correctly, {@code null} if the
* image was not found
* @throws ComputeException upon failure
* @throws ComputeException upon failure or if {@code image} is a publicly-available image
*/
Operation deprecate(ImageId image, DeprecationStatus<ImageId> deprecationStatus,
OperationOption... options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1124,14 +1124,15 @@ public Image apply(com.google.api.services.compute.model.Image image) {
}

@Override
public Operation delete(final ImageId image, OperationOption... options) {
public Operation delete(ImageId image, OperationOption... options) {
final ImageId completeId = image.setProjectId(options().projectId());
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.deleteImage(image.image(), optionsMap);
return computeRpc.deleteImage(completeId.project(), completeId.image(), optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : Operation.fromPb(this, answer);
Expand All @@ -1141,15 +1142,17 @@ public com.google.api.services.compute.model.Operation call() {
}

@Override
public Operation deprecate(final ImageId image,
public Operation deprecate(ImageId image,
final DeprecationStatus<ImageId> deprecationStatus, OperationOption... options) {
final ImageId completeId = image.setProjectId(options().projectId());
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.deprecateImage(image.image(), deprecationStatus.toPb(), optionsMap);
return computeRpc.deprecateImage(completeId.project(), completeId.image(),
deprecationStatus.toPb(), optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : Operation.fromPb(this, answer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public Image reload(ImageOption... options) {
*
* @return a global operation if the delete request was successfully sent, {@code null} if the
* image was not found
* @throws ComputeException upon failure
* @throws ComputeException upon failure or if this image is a publicly-available image
*/
public Operation delete(OperationOption... options) {
return compute.delete(imageId(), options);
Expand All @@ -167,7 +167,7 @@ public Operation delete(OperationOption... options) {
*
* @return a global operation if the deprecation request was successfully sent, {@code null} if
* the image was not found
* @throws ComputeException upon failure
* @throws ComputeException upon failure or if this image is a publicly-available image
*/
public Operation deprecate(DeprecationStatus<ImageId> deprecationStatus,
OperationOption... options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ Operation createSnapshot(String zone, String disk, String snapshot, String descr
* image was not found
* @throws ComputeException upon failure
*/
Operation deleteImage(String image, Map<Option, ?> options);
Operation deleteImage(String project, String image, Map<Option, ?> options);

/**
* Deprecates the requested image.
Expand All @@ -375,6 +375,6 @@ Operation createSnapshot(String zone, String disk, String snapshot, String descr
* image was not found
* @throws ComputeException upon failure
*/
Operation deprecateImage(String image, DeprecationStatus deprecationStatus,
Operation deprecateImage(String project, String image, DeprecationStatus deprecationStatus,
Map<Option, ?> options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,10 @@ public Tuple<String, Iterable<Image>> listImages(String project, Map<Option, ?>
}

@Override
public Operation deleteImage(String image, Map<Option, ?> options) {
public Operation deleteImage(String project, String image, Map<Option, ?> options) {
try {
return compute.images()
.delete(this.options.projectId(), image)
.delete(project, image)
.setFields(FIELDS.getString(options))
.execute();
} catch (IOException ex) {
Expand All @@ -621,11 +621,11 @@ public Operation deleteImage(String image, Map<Option, ?> options) {
}

@Override
public Operation deprecateImage(String image, DeprecationStatus deprecationStatus,
public Operation deprecateImage(String project, String image, DeprecationStatus deprecationStatus,
Map<Option, ?> options) {
try {
return compute.images()
.deprecate(this.options.projectId(), image, deprecationStatus)
.deprecate(project, image, deprecationStatus)
.setFields(FIELDS.getString(options))
.execute();
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public class ComputeImplTest {
private static final DiskId DISK_ID = DiskId.of("project", "zone", "disk");
private static final SnapshotId SNAPSHOT_ID = SnapshotId.of("project", "snapshot");
private static final SnapshotInfo SNAPSHOT = SnapshotInfo.of(SNAPSHOT_ID, DISK_ID);
private static final ImageId IMAGE_ID = ImageId.of("project", "snapshot");
private static final ImageId IMAGE_ID = ImageId.of("project", "image");
private static final ImageInfo IMAGE = ImageInfo.of(IMAGE_ID, DiskImageConfiguration.of(DISK_ID));
private static final DeprecationStatus<ImageId> DEPRECATION_STATUS =
DeprecationStatus.builder(DeprecationStatus.Status.DEPRECATED, IMAGE_ID).build();
Expand Down Expand Up @@ -2191,8 +2191,8 @@ public void testGetImageWithSelectedFields() {

@Test
public void testDeleteImage_Operation() {
EasyMock.expect(computeRpcMock.deleteImage(IMAGE_ID.image(), EMPTY_RPC_OPTIONS))
.andReturn(globalOperation.toPb());
EasyMock.expect(computeRpcMock.deleteImage(IMAGE_ID.project(), IMAGE_ID.image(),
EMPTY_RPC_OPTIONS)).andReturn(globalOperation.toPb());
EasyMock.replay(computeRpcMock);
compute = options.service();
assertEquals(globalOperation, compute.delete(IMAGE_ID));
Expand All @@ -2201,11 +2201,11 @@ public void testDeleteImage_Operation() {
@Test
public void testDeleteImageWithSelectedFields_Operation() {
Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance();
EasyMock.expect(computeRpcMock.deleteImage(eq(IMAGE_ID.image()), capture(capturedOptions)))
.andReturn(globalOperation.toPb());
EasyMock.expect(computeRpcMock.deleteImage(eq(PROJECT), eq(IMAGE_ID.image()),
capture(capturedOptions))).andReturn(globalOperation.toPb());
EasyMock.replay(computeRpcMock);
compute = options.service();
Operation operation = compute.delete(IMAGE_ID, OPERATION_OPTION_FIELDS);
Operation operation = compute.delete(ImageId.of("image"), OPERATION_OPTION_FIELDS);
String selector = (String) capturedOptions.getValue().get(OPERATION_OPTION_FIELDS.rpcOption());
assertTrue(selector.contains("selfLink"));
assertTrue(selector.contains("id"));
Expand All @@ -2216,16 +2216,16 @@ public void testDeleteImageWithSelectedFields_Operation() {

@Test
public void testDeleteImage_Null() {
EasyMock.expect(computeRpcMock.deleteImage(IMAGE_ID.image(), EMPTY_RPC_OPTIONS))
.andReturn(null);
EasyMock.expect(computeRpcMock.deleteImage(IMAGE_ID.project(), IMAGE_ID.image(),
EMPTY_RPC_OPTIONS)).andReturn(null);
EasyMock.replay(computeRpcMock);
compute = options.service();
assertNull(compute.delete(IMAGE_ID));
}

@Test
public void testDeprecateImage_Operation() {
EasyMock.expect(computeRpcMock.deprecateImage(IMAGE_ID.image(),
EasyMock.expect(computeRpcMock.deprecateImage(IMAGE_ID.project(), IMAGE_ID.image(),
DEPRECATION_STATUS.toPb(), EMPTY_RPC_OPTIONS)).andReturn(globalOperation.toPb());
EasyMock.replay(computeRpcMock);
compute = options.service();
Expand All @@ -2235,11 +2235,12 @@ public void testDeprecateImage_Operation() {
@Test
public void testDeprecateImageWithSelectedFields_Operation() {
Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance();
EasyMock.expect(computeRpcMock.deprecateImage(eq(IMAGE_ID.image()),
EasyMock.expect(computeRpcMock.deprecateImage(eq(PROJECT), eq(IMAGE_ID.image()),
eq(DEPRECATION_STATUS.toPb()), capture(capturedOptions))).andReturn(globalOperation.toPb());
EasyMock.replay(computeRpcMock);
compute = options.service();
Operation operation = compute.deprecate(IMAGE_ID, DEPRECATION_STATUS, OPERATION_OPTION_FIELDS);
Operation operation =
compute.deprecate(ImageId.of("image"), DEPRECATION_STATUS, OPERATION_OPTION_FIELDS);
String selector = (String) capturedOptions.getValue().get(OPERATION_OPTION_FIELDS.rpcOption());
assertTrue(selector.contains("selfLink"));
assertTrue(selector.contains("id"));
Expand All @@ -2250,8 +2251,8 @@ public void testDeprecateImageWithSelectedFields_Operation() {

@Test
public void testDeprecateImage_Null() {
EasyMock.expect(computeRpcMock.deprecateImage(IMAGE_ID.image(), DEPRECATION_STATUS.toPb(),
EMPTY_RPC_OPTIONS)).andReturn(null);
EasyMock.expect(computeRpcMock.deprecateImage(IMAGE_ID.project(), IMAGE_ID.image(),
DEPRECATION_STATUS.toPb(), EMPTY_RPC_OPTIONS)).andReturn(null);
EasyMock.replay(computeRpcMock);
compute = options.service();
assertNull(compute.deprecate(IMAGE_ID, DEPRECATION_STATUS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableList;
import com.google.gcloud.compute.DeprecationStatus.Status;
import com.google.gcloud.compute.ImageConfiguration.SourceType;

import org.junit.Test;
Expand Down Expand Up @@ -64,7 +65,7 @@ public class ImageTest {
.sourceType(SOURCE_TYPE)
.build();
private static final DeprecationStatus<ImageId> DEPRECATION_STATUS =
DeprecationStatus.of(DeprecationStatus.Status.DELETED, IMAGE_ID);
DeprecationStatus.of(Status.DELETED, IMAGE_ID);

private final Compute serviceMockReturnsOptions = createStrictMock(Compute.class);
private final ComputeOptions mockOptions = createMock(ComputeOptions.class);
Expand Down Expand Up @@ -263,6 +264,31 @@ public void testReloadWithOptions() throws Exception {
verify(compute);
}

@Test
public void testDeprecateImage() {
initializeExpectedImage(3);
expect(compute.options()).andReturn(mockOptions);
Operation operation = new Operation.Builder(serviceMockReturnsOptions)
.operationId(GlobalOperationId.of("project", "op"))
.build();
DeprecationStatus<ImageId> status = DeprecationStatus.of(Status.DEPRECATED, IMAGE_ID);
expect(compute.deprecate(IMAGE_ID, status)).andReturn(operation);
replay(compute);
initializeImage();
assertSame(operation, image.deprecate(status));
}

@Test
public void testDeprecateNull() {
initializeExpectedImage(2);
expect(compute.options()).andReturn(mockOptions);
DeprecationStatus<ImageId> status = DeprecationStatus.of(Status.DEPRECATED, IMAGE_ID);
expect(compute.deprecate(IMAGE_ID, status)).andReturn(null);
replay(compute);
initializeImage();
assertNull(image.deprecate(status));
}

public void compareImage(Image expected, Image value) {
assertEquals(expected, value);
assertEquals(expected.compute().options(), value.compute().options());
Expand Down