Skip to content

Commit

Permalink
Merge pull request #1226 from jyothisaroja/cached_images
Browse files Browse the repository at this point in the history
Implement listCachedImages() for Glance V2 Images
  • Loading branch information
gondor authored Dec 18, 2018
2 parents 559c914 + c89cba0 commit a03fc58
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.openstack4j.common.RestService;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.common.Payload;
import org.openstack4j.model.image.v2.CachedImage;
import org.openstack4j.model.image.v2.Image;
import org.openstack4j.model.image.v2.ImageUpdate;
import org.openstack4j.model.image.v2.Member;
Expand Down Expand Up @@ -32,6 +33,13 @@ public interface ImageService extends RestService {
* @return list of images fitered by filteringParams
*/
List<? extends Image> list(Map<String, String> filteringParams);

/**
* List images currently in the glance image cache.
*
* @return list of cached images or empty list if the cache is empty or null if the cache is not enabled.
*/
List<? extends CachedImage> listCachedImages() ;

/**
* Show details for an image by imageid.
Expand Down
41 changes: 41 additions & 0 deletions core/src/main/java/org/openstack4j/model/image/v2/CachedImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.openstack4j.model.image.v2;

import org.openstack4j.model.ModelEntity;
import java.util.Date;

/**
* @author zdagjyo on 13/11/2018.
* @see https://docs.openstack.org/developer/glance/cache.html
*/
public interface CachedImage extends ModelEntity {

/**
*
* @return the image id of the cached image
*/
String getImageId();

/**
*
* @return date when this image was last accessed in the cache
*/
Date getLastAccessed();

/**
*
* @return date when the image was last modified in the cache
*/
Date getLastModified();

/**
*
* @return nr of cache hits
*/
Integer getHits();

/**
*
* @return the image size
*/
Long getSize();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.openstack4j.openstack.image.v2.domain;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.base.Objects;
import org.openstack4j.model.image.v2.CachedImage;
import org.openstack4j.openstack.common.CustomEpochToDateDeserializer;
import org.openstack4j.openstack.common.ListResult;

import java.util.Date;
import java.util.List;

/**
* @author zdagjyo on 13/11/2018.
*/
public class CachedGlanceImage implements CachedImage {

private static final long serialVersionUID = 1L;

@JsonProperty("image_id")
private String imageId;

private Integer hits;

@JsonProperty("last_accessed")
@JsonDeserialize(using = CustomEpochToDateDeserializer.class)
private Date lastAccessed;

@JsonDeserialize(using = CustomEpochToDateDeserializer.class)
@JsonProperty("last_modified")
private Date lastModified;

private Long size;

@Override
public String getImageId() {
return imageId;
}

@Override
public Date getLastAccessed() {
return lastAccessed;
}

@Override
public Date getLastModified() {
return lastModified;
}

@Override
public Integer getHits() {
return hits;
}

@Override
public Long getSize() {
return size;
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return Objects.toStringHelper(this).omitNullValues()
.add("id", imageId).add("size", size).add("hits", hits).add("lastAccessed", lastAccessed)
.add("lastModified", lastModified).addValue("\n")
.toString();
}

public static class CachedImages extends ListResult<CachedGlanceImage> {

private static final long serialVersionUID = 1L;
@JsonProperty("cached_images")
private List<CachedGlanceImage> cachedImages;

@Override
protected List<CachedGlanceImage> value() {
return cachedImages;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jsonpatch.diff.JsonDiff;
import org.openstack4j.api.Apis;
import org.openstack4j.api.exceptions.ResponseException;
import org.openstack4j.api.image.v2.ImageService;
import org.openstack4j.api.image.v2.TaskService;
import org.openstack4j.core.transport.ExecutionOptions;
import org.openstack4j.core.transport.HttpResponse;
import org.openstack4j.core.transport.propagation.PropagateOnStatus;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.common.Payload;
import org.openstack4j.model.image.v2.CachedImage;
import org.openstack4j.model.image.v2.Image;
import org.openstack4j.model.image.v2.ImageUpdate;
import org.openstack4j.model.image.v2.Member;
import org.openstack4j.openstack.image.v2.domain.CachedGlanceImage.CachedImages;
import org.openstack4j.openstack.image.v2.domain.GlanceImage;
import org.openstack4j.openstack.image.v2.domain.GlanceImageUpdate;
import org.openstack4j.openstack.image.v2.domain.GlanceMember;
Expand Down Expand Up @@ -52,6 +57,20 @@ public List<? extends Image> list() {
public List<? extends Image> list(Map<String, String> filteringParams) {
return get(GlanceImage.Images.class, uri("/images")).params(filteringParams).execute().getList();
}

/**
* {@inheritDoc}
*/
@Override
public List<? extends CachedImage> listCachedImages() {
try {
return get(CachedImages.class, uri("/cached_images"))
.execute(ExecutionOptions.<CachedImages>create(PropagateOnStatus.on(404))).getList();
}
catch (ResponseException e) {
return null;
}
}

/**
* {@inheritDoc}
Expand Down

0 comments on commit a03fc58

Please sign in to comment.