Skip to content

Commit c671888

Browse files
committed
Fix image's delete and deprecate methods, document image's list and update tests
1 parent 2812b94 commit c671888

7 files changed

Lines changed: 64 additions & 29 deletions

File tree

gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,9 +1832,14 @@ public static ImageListOption fields(ImageField... fields) {
18321832
Image get(ImageId imageId, ImageOption... options);
18331833

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

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

@@ -1859,7 +1864,7 @@ public static ImageListOption fields(ImageField... fields) {
18591864
*
18601865
* @return a global operation if the deprecation request was issued correctly, {@code null} if the
18611866
* image was not found
1862-
* @throws ComputeException upon failure
1867+
* @throws ComputeException upon failure or if {@code image} is a publicly-available image
18631868
*/
18641869
Operation deprecate(ImageId image, DeprecationStatus<ImageId> deprecationStatus,
18651870
OperationOption... options);

gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,14 +1124,15 @@ public Image apply(com.google.api.services.compute.model.Image image) {
11241124
}
11251125

11261126
@Override
1127-
public Operation delete(final ImageId image, OperationOption... options) {
1127+
public Operation delete(ImageId image, OperationOption... options) {
1128+
final ImageId completeId = image.setProjectId(options().projectId());
11281129
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
11291130
try {
11301131
com.google.api.services.compute.model.Operation answer =
11311132
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
11321133
@Override
11331134
public com.google.api.services.compute.model.Operation call() {
1134-
return computeRpc.deleteImage(image.image(), optionsMap);
1135+
return computeRpc.deleteImage(completeId.project(), completeId.image(), optionsMap);
11351136
}
11361137
}, options().retryParams(), EXCEPTION_HANDLER);
11371138
return answer == null ? null : Operation.fromPb(this, answer);
@@ -1141,15 +1142,17 @@ public com.google.api.services.compute.model.Operation call() {
11411142
}
11421143

11431144
@Override
1144-
public Operation deprecate(final ImageId image,
1145+
public Operation deprecate(ImageId image,
11451146
final DeprecationStatus<ImageId> deprecationStatus, OperationOption... options) {
1147+
final ImageId completeId = image.setProjectId(options().projectId());
11461148
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
11471149
try {
11481150
com.google.api.services.compute.model.Operation answer =
11491151
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
11501152
@Override
11511153
public com.google.api.services.compute.model.Operation call() {
1152-
return computeRpc.deprecateImage(image.image(), deprecationStatus.toPb(), optionsMap);
1154+
return computeRpc.deprecateImage(completeId.project(), completeId.image(),
1155+
deprecationStatus.toPb(), optionsMap);
11531156
}
11541157
}, options().retryParams(), EXCEPTION_HANDLER);
11551158
return answer == null ? null : Operation.fromPb(this, answer);

gcloud-java-compute/src/main/java/com/google/gcloud/compute/Image.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public Image reload(ImageOption... options) {
156156
*
157157
* @return a global operation if the delete request was successfully sent, {@code null} if the
158158
* image was not found
159-
* @throws ComputeException upon failure
159+
* @throws ComputeException upon failure or if this image is a publicly-available image
160160
*/
161161
public Operation delete(OperationOption... options) {
162162
return compute.delete(imageId(), options);
@@ -167,7 +167,7 @@ public Operation delete(OperationOption... options) {
167167
*
168168
* @return a global operation if the deprecation request was successfully sent, {@code null} if
169169
* the image was not found
170-
* @throws ComputeException upon failure
170+
* @throws ComputeException upon failure or if this image is a publicly-available image
171171
*/
172172
public Operation deprecate(DeprecationStatus<ImageId> deprecationStatus,
173173
OperationOption... options) {

gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpc.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ Operation createSnapshot(String zone, String disk, String snapshot, String descr
366366
* image was not found
367367
* @throws ComputeException upon failure
368368
*/
369-
Operation deleteImage(String image, Map<Option, ?> options);
369+
Operation deleteImage(String project, String image, Map<Option, ?> options);
370370

371371
/**
372372
* Deprecates the requested image.
@@ -375,6 +375,6 @@ Operation createSnapshot(String zone, String disk, String snapshot, String descr
375375
* image was not found
376376
* @throws ComputeException upon failure
377377
*/
378-
Operation deprecateImage(String image, DeprecationStatus deprecationStatus,
378+
Operation deprecateImage(String project, String image, DeprecationStatus deprecationStatus,
379379
Map<Option, ?> options);
380380
}

gcloud-java-compute/src/main/java/com/google/gcloud/spi/DefaultComputeRpc.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,10 @@ public Tuple<String, Iterable<Image>> listImages(String project, Map<Option, ?>
609609
}
610610

611611
@Override
612-
public Operation deleteImage(String image, Map<Option, ?> options) {
612+
public Operation deleteImage(String project, String image, Map<Option, ?> options) {
613613
try {
614614
return compute.images()
615-
.delete(this.options.projectId(), image)
615+
.delete(project, image)
616616
.setFields(FIELDS.getString(options))
617617
.execute();
618618
} catch (IOException ex) {
@@ -621,11 +621,11 @@ public Operation deleteImage(String image, Map<Option, ?> options) {
621621
}
622622

623623
@Override
624-
public Operation deprecateImage(String image, DeprecationStatus deprecationStatus,
624+
public Operation deprecateImage(String project, String image, DeprecationStatus deprecationStatus,
625625
Map<Option, ?> options) {
626626
try {
627627
return compute.images()
628-
.deprecate(this.options.projectId(), image, deprecationStatus)
628+
.deprecate(project, image, deprecationStatus)
629629
.setFields(FIELDS.getString(options))
630630
.execute();
631631
} catch (IOException ex) {

gcloud-java-compute/src/test/java/com/google/gcloud/compute/ComputeImplTest.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public class ComputeImplTest {
194194
private static final DiskId DISK_ID = DiskId.of("project", "zone", "disk");
195195
private static final SnapshotId SNAPSHOT_ID = SnapshotId.of("project", "snapshot");
196196
private static final SnapshotInfo SNAPSHOT = SnapshotInfo.of(SNAPSHOT_ID, DISK_ID);
197-
private static final ImageId IMAGE_ID = ImageId.of("project", "snapshot");
197+
private static final ImageId IMAGE_ID = ImageId.of("project", "image");
198198
private static final ImageInfo IMAGE = ImageInfo.of(IMAGE_ID, DiskImageConfiguration.of(DISK_ID));
199199
private static final DeprecationStatus<ImageId> DEPRECATION_STATUS =
200200
DeprecationStatus.builder(DeprecationStatus.Status.DEPRECATED, IMAGE_ID).build();
@@ -2191,8 +2191,8 @@ public void testGetImageWithSelectedFields() {
21912191

21922192
@Test
21932193
public void testDeleteImage_Operation() {
2194-
EasyMock.expect(computeRpcMock.deleteImage(IMAGE_ID.image(), EMPTY_RPC_OPTIONS))
2195-
.andReturn(globalOperation.toPb());
2194+
EasyMock.expect(computeRpcMock.deleteImage(IMAGE_ID.project(), IMAGE_ID.image(),
2195+
EMPTY_RPC_OPTIONS)).andReturn(globalOperation.toPb());
21962196
EasyMock.replay(computeRpcMock);
21972197
compute = options.service();
21982198
assertEquals(globalOperation, compute.delete(IMAGE_ID));
@@ -2201,11 +2201,11 @@ public void testDeleteImage_Operation() {
22012201
@Test
22022202
public void testDeleteImageWithSelectedFields_Operation() {
22032203
Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance();
2204-
EasyMock.expect(computeRpcMock.deleteImage(eq(IMAGE_ID.image()), capture(capturedOptions)))
2205-
.andReturn(globalOperation.toPb());
2204+
EasyMock.expect(computeRpcMock.deleteImage(eq(PROJECT), eq(IMAGE_ID.image()),
2205+
capture(capturedOptions))).andReturn(globalOperation.toPb());
22062206
EasyMock.replay(computeRpcMock);
22072207
compute = options.service();
2208-
Operation operation = compute.delete(IMAGE_ID, OPERATION_OPTION_FIELDS);
2208+
Operation operation = compute.delete(ImageId.of("image"), OPERATION_OPTION_FIELDS);
22092209
String selector = (String) capturedOptions.getValue().get(OPERATION_OPTION_FIELDS.rpcOption());
22102210
assertTrue(selector.contains("selfLink"));
22112211
assertTrue(selector.contains("id"));
@@ -2216,16 +2216,16 @@ public void testDeleteImageWithSelectedFields_Operation() {
22162216

22172217
@Test
22182218
public void testDeleteImage_Null() {
2219-
EasyMock.expect(computeRpcMock.deleteImage(IMAGE_ID.image(), EMPTY_RPC_OPTIONS))
2220-
.andReturn(null);
2219+
EasyMock.expect(computeRpcMock.deleteImage(IMAGE_ID.project(), IMAGE_ID.image(),
2220+
EMPTY_RPC_OPTIONS)).andReturn(null);
22212221
EasyMock.replay(computeRpcMock);
22222222
compute = options.service();
22232223
assertNull(compute.delete(IMAGE_ID));
22242224
}
22252225

22262226
@Test
22272227
public void testDeprecateImage_Operation() {
2228-
EasyMock.expect(computeRpcMock.deprecateImage(IMAGE_ID.image(),
2228+
EasyMock.expect(computeRpcMock.deprecateImage(IMAGE_ID.project(), IMAGE_ID.image(),
22292229
DEPRECATION_STATUS.toPb(), EMPTY_RPC_OPTIONS)).andReturn(globalOperation.toPb());
22302230
EasyMock.replay(computeRpcMock);
22312231
compute = options.service();
@@ -2235,11 +2235,12 @@ public void testDeprecateImage_Operation() {
22352235
@Test
22362236
public void testDeprecateImageWithSelectedFields_Operation() {
22372237
Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance();
2238-
EasyMock.expect(computeRpcMock.deprecateImage(eq(IMAGE_ID.image()),
2238+
EasyMock.expect(computeRpcMock.deprecateImage(eq(PROJECT), eq(IMAGE_ID.image()),
22392239
eq(DEPRECATION_STATUS.toPb()), capture(capturedOptions))).andReturn(globalOperation.toPb());
22402240
EasyMock.replay(computeRpcMock);
22412241
compute = options.service();
2242-
Operation operation = compute.deprecate(IMAGE_ID, DEPRECATION_STATUS, OPERATION_OPTION_FIELDS);
2242+
Operation operation =
2243+
compute.deprecate(ImageId.of("image"), DEPRECATION_STATUS, OPERATION_OPTION_FIELDS);
22432244
String selector = (String) capturedOptions.getValue().get(OPERATION_OPTION_FIELDS.rpcOption());
22442245
assertTrue(selector.contains("selfLink"));
22452246
assertTrue(selector.contains("id"));
@@ -2250,8 +2251,8 @@ public void testDeprecateImageWithSelectedFields_Operation() {
22502251

22512252
@Test
22522253
public void testDeprecateImage_Null() {
2253-
EasyMock.expect(computeRpcMock.deprecateImage(IMAGE_ID.image(), DEPRECATION_STATUS.toPb(),
2254-
EMPTY_RPC_OPTIONS)).andReturn(null);
2254+
EasyMock.expect(computeRpcMock.deprecateImage(IMAGE_ID.project(), IMAGE_ID.image(),
2255+
DEPRECATION_STATUS.toPb(), EMPTY_RPC_OPTIONS)).andReturn(null);
22552256
EasyMock.replay(computeRpcMock);
22562257
compute = options.service();
22572258
assertNull(compute.deprecate(IMAGE_ID, DEPRECATION_STATUS));

gcloud-java-compute/src/test/java/com/google/gcloud/compute/ImageTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static org.junit.Assert.assertTrue;
2929

3030
import com.google.common.collect.ImmutableList;
31+
import com.google.gcloud.compute.DeprecationStatus.Status;
3132
import com.google.gcloud.compute.ImageConfiguration.SourceType;
3233

3334
import org.junit.Test;
@@ -64,7 +65,7 @@ public class ImageTest {
6465
.sourceType(SOURCE_TYPE)
6566
.build();
6667
private static final DeprecationStatus<ImageId> DEPRECATION_STATUS =
67-
DeprecationStatus.of(DeprecationStatus.Status.DELETED, IMAGE_ID);
68+
DeprecationStatus.of(Status.DELETED, IMAGE_ID);
6869

6970
private final Compute serviceMockReturnsOptions = createStrictMock(Compute.class);
7071
private final ComputeOptions mockOptions = createMock(ComputeOptions.class);
@@ -263,6 +264,31 @@ public void testReloadWithOptions() throws Exception {
263264
verify(compute);
264265
}
265266

267+
@Test
268+
public void testDeprecateImage() {
269+
initializeExpectedImage(3);
270+
expect(compute.options()).andReturn(mockOptions);
271+
Operation operation = new Operation.Builder(serviceMockReturnsOptions)
272+
.operationId(GlobalOperationId.of("project", "op"))
273+
.build();
274+
DeprecationStatus<ImageId> status = DeprecationStatus.of(Status.DEPRECATED, IMAGE_ID);
275+
expect(compute.deprecate(IMAGE_ID, status)).andReturn(operation);
276+
replay(compute);
277+
initializeImage();
278+
assertSame(operation, image.deprecate(status));
279+
}
280+
281+
@Test
282+
public void testDeprecateNull() {
283+
initializeExpectedImage(2);
284+
expect(compute.options()).andReturn(mockOptions);
285+
DeprecationStatus<ImageId> status = DeprecationStatus.of(Status.DEPRECATED, IMAGE_ID);
286+
expect(compute.deprecate(IMAGE_ID, status)).andReturn(null);
287+
replay(compute);
288+
initializeImage();
289+
assertNull(image.deprecate(status));
290+
}
291+
266292
public void compareImage(Image expected, Image value) {
267293
assertEquals(expected, value);
268294
assertEquals(expected.compute().options(), value.compute().options());

0 commit comments

Comments
 (0)