@Override public ImageInRegion getImage(String id) { String region = ImageInRegion.extractRegion(id); String imageId = ImageInRegion.extractImageId(id); // The id of the image can be an id or a slug. Use the corresponding method of the API depending on what is // provided. If it can be parsed as a number, use the method to get by ID. Otherwise, get by slug. Integer numericId = Ints.tryParse(imageId); Image image = numericId == null ? api.imageApi().get(imageId) : api.imageApi().get(numericId); return image == null ? null : ImageInRegion.create(image, region); }
@Override protected IterableWithMarker<Image> fetchPageUsingOptions(ImageListOptions options, Optional<Object> arg0) { return api.imageApi().list(options); } }
public void testGetImageBySlug() { Optional<Image> first = api().list().concat().firstMatch(new Predicate<Image>() { @Override public boolean apply(Image input) { return !isNullOrEmpty(input.slug()); } }); assertTrue(first.isPresent(), "At least one image with the 'slug' field set was expected to exist"); assertNotNull(api().get(first.get().slug())); }
@Override public boolean deleteImage(String id) { String imageId = ImageInRegion.extractImageId(id); Integer numericId = Ints.tryParse(imageId); // User images don't have a slug, so we expect a numeric id here if (numericId != null) { try { logger.debug(">> deleting image %s...", id); api.imageApi().delete(numericId); return true; } catch (Exception ex) { logger.error(ex, ">> error deleting image %s", id); } } else { logger.warn(">> image %s is not a user image and cannot be deleted", id); } return false; } }
@Override public boolean deleteImage(String id) { String imageId = ImageInRegion.extractImageId(id); Integer numericId = Ints.tryParse(imageId); // User images don't have a slug, so we expect a numeric id here if (numericId != null) { try { logger.debug(">> deleting image %s...", id); api.imageApi().delete(numericId); return true; } catch (Exception ex) { logger.error(ex, ">> error deleting image %s", id); } } else { logger.warn(">> image %s is not a user image and cannot be deleted", id); } return false; } }
@Override protected IterableWithMarker<Image> fetchPageUsingOptions(ImageListOptions options, Optional<Object> arg0) { return api.imageApi().list(options); } }
@Override public ImageInRegion getImage(String id) { String region = ImageInRegion.extractRegion(id); String imageId = ImageInRegion.extractImageId(id); // The id of the image can be an id or a slug. Use the corresponding method of the API depending on what is // provided. If it can be parsed as a number, use the method to get by ID. Otherwise, get by slug. Integer numericId = Ints.tryParse(imageId); Image image = numericId == null ? api.imageApi().get(imageId) : api.imageApi().get(numericId); return image == null ? null : ImageInRegion.create(image, region); }
@Override public boolean deleteImage(String id) { String imageId = ImageInRegion.extractImageId(id); Integer numericId = Ints.tryParse(imageId); // User images don't have a slug, so we expect a numeric id here if (numericId != null) { try { logger.debug(">> deleting image %s...", id); api.imageApi().delete(numericId); return true; } catch (Exception ex) { logger.error(ex, ">> error deleting image %s", id); } } else { logger.warn(">> image %s is not a user image and cannot be deleted", id); } return false; } }
@Override protected IterableWithMarker<Image> fetchPageUsingOptions(ImageListOptions options, Optional<Object> arg0) { return api.imageApi().list(options); } }
@Override public ImageInRegion getImage(String id) { String region = ImageInRegion.extractRegion(id); String imageId = ImageInRegion.extractImageId(id); // The id of the image can be an id or a slug. Use the corresponding method of the API depending on what is // provided. If it can be parsed as a number, use the method to get by ID. Otherwise, get by slug. Integer numericId = Ints.tryParse(imageId); Image image = numericId == null ? api.imageApi().get(imageId) : api.imageApi().get(numericId); return image == null ? null : ImageInRegion.create(image, region); }
public void testDeleteImageReturns404() throws InterruptedException { server.enqueue(response404()); api.imageApi().delete(1); assertEquals(server.getRequestCount(), 1); assertSent(server, "DELETE", "/images/1"); }
@Override public Iterable<ImageInRegion> listImages() { // Images can claim to be available in a region that is currently marked as "unavailable". We shouldn't return // the images scoped to those regions. final Set<String> availableRegionsIds = newHashSet(transform(listLocations(), new Function<Region, String>() { @Override public String apply(Region input) { return input.slug(); } })); // Public images re globally available, but non-public ones can only be available in certain regions. // For these kind of images, return one instance of an ImageInRegion for each region where the image is // available. This way we can properly scope global and concrete images so they can be properly looked up. return concat(filter(api.imageApi().list().concat().transform(new Function<Image, Iterable<ImageInRegion>>() { @Override public Iterable<ImageInRegion> apply(final Image image) { return transform(image.regions(), new Function<String, ImageInRegion>() { @Override public ImageInRegion apply(String region) { return availableRegionsIds.contains(region) ? ImageInRegion.create(image, region) : null; } }); } }), notNull())); }
public void testGetImageReturns404() throws InterruptedException { server.enqueue(response404()); Image image = api.imageApi().get(1); assertNull(image); assertEquals(server.getRequestCount(), 1); assertSent(server, "GET", "/images/1"); }
@Test(groups = "live", dependsOnMethods = "testPowerOn") public void testSnapshots() { Action action = api().snapshot(dropletId, prefix + dropletId + "-snapshot"); assertActionCompleted(action.id()); List<Snapshot> snapshots = api().listSnapshots(dropletId).concat().toList(); assertEquals(snapshots.size(), 1, "Must contain 1 snapshot"); for (Snapshot snapshot : snapshots) { try { api.imageApi().delete(snapshot.id()); } catch (Exception ex) { getAnonymousLogger().warning("Could not delete snapshot: " + snapshot.id()); } } }
@Override public Iterable<ImageInRegion> listImages() { // Images can claim to be available in a region that is currently marked as "unavailable". We shouldn't return // the images scoped to those regions. final Set<String> availableRegionsIds = newHashSet(transform(listLocations(), new Function<Region, String>() { @Override public String apply(Region input) { return input.slug(); } })); // Public images re globally available, but non-public ones can only be available in certain regions. // For these kind of images, return one instance of an ImageInRegion for each region where the image is // available. This way we can properly scope global and concrete images so they can be properly looked up. return concat(filter(api.imageApi().list().concat().transform(new Function<Image, Iterable<ImageInRegion>>() { @Override public Iterable<ImageInRegion> apply(final Image image) { return transform(image.regions(), new Function<String, ImageInRegion>() { @Override public ImageInRegion apply(String region) { return availableRegionsIds.contains(region) ? ImageInRegion.create(image, region) : null; } }); } }), notNull())); }
public void testGetImageUsingSlugReturns404() throws InterruptedException { server.enqueue(response404()); Image image = api.imageApi().get("foo"); assertNull(image); assertEquals(server.getRequestCount(), 1); assertSent(server, "GET", "/images/foo"); }
public void testDeleteImage() throws InterruptedException { server.enqueue(response204()); api.imageApi().delete(1); assertEquals(server.getRequestCount(), 1); assertSent(server, "DELETE", "/images/1"); }
@Override public Iterable<ImageInRegion> listImages() { // Images can claim to be available in a region that is currently marked as "unavailable". We shouldn't return // the images scoped to those regions. final Set<String> availableRegionsIds = newHashSet(transform(listLocations(), new Function<Region, String>() { @Override public String apply(Region input) { return input.slug(); } })); // Public images re globally available, but non-public ones can only be available in certain regions. // For these kind of images, return one instance of an ImageInRegion for each region where the image is // available. This way we can properly scope global and concrete images so they can be properly looked up. return concat(filter(api.imageApi().list().concat().transform(new Function<Image, Iterable<ImageInRegion>>() { @Override public Iterable<ImageInRegion> apply(final Image image) { return transform(image.regions(), new Function<String, ImageInRegion>() { @Override public ImageInRegion apply(String region) { return availableRegionsIds.contains(region) ? ImageInRegion.create(image, region) : null; } }); } }), notNull())); }
public void testGetImageUsingSlug() throws InterruptedException { server.enqueue(jsonResponse("/image.json")); Image image = api.imageApi().get("foo"); assertEquals(image, imageFromResource("/image.json")); assertEquals(server.getRequestCount(), 1); assertSent(server, "GET", "/images/foo"); }