summaryrefslogtreecommitdiff
path: root/glanceclient/v1/images.py
diff options
context:
space:
mode:
authorBrian Waldon <bcwaldon@gmail.com>2012-07-14 04:39:27 +0000
committerBrian Waldon <bcwaldon@gmail.com>2012-07-19 13:26:37 -0700
commit0f628b19cbbe53ea29de02f4ae84bc893138820f (patch)
tree1428b64b98747ac4a96f6f8ae5ba6947f5591fdf /glanceclient/v1/images.py
parent570e64d91f3b85ef6c4586f7b3a59183e5bb1d3e (diff)
downloadpython-glanceclient-0f628b19cbbe53ea29de02f4ae84bc893138820f.tar.gz
Add pagination to v1 image-list
* Use recursive generator function to make subsequent requests to the v1 detailed images resource * 'limit' continues to act as the absolute limit of images to return from a list call * 'page_size' indicates how many images to ask for in each subsequent pagination request * Expose --page-size through the cli * Convert v1 images tests to use strict url comparison * Drop strict_url_check from FakeAPI kwargs - now the functionality is always active and tests must directly match fixture urls * Fix bug 1024614 Change-Id: Ifa7874d88360e03b5c8aa95bfb9d5e6dc6dc927e
Diffstat (limited to 'glanceclient/v1/images.py')
-rw-r--r--glanceclient/v1/images.py52
1 files changed, 37 insertions, 15 deletions
diff --git a/glanceclient/v1/images.py b/glanceclient/v1/images.py
index 4263baf..027ab23 100644
--- a/glanceclient/v1/images.py
+++ b/glanceclient/v1/images.py
@@ -27,6 +27,8 @@ UPDATE_PARAMS = ('name', 'disk_format', 'container_format', 'min_disk',
CREATE_PARAMS = UPDATE_PARAMS + ('id',)
+DEFAULT_PAGE_SIZE = 20
+
class Image(base.Resource):
def __repr__(self):
@@ -85,25 +87,45 @@ class ImageManager(base.Manager):
resp, body = self.api.raw_request('GET', '/v1/images/%s' % image_id)
return body
- def list(self, limit=None, marker=None, filters=None):
+ def list(self, **kwargs):
"""Get a list of images.
- :param limit: maximum number of images to return. Used for pagination.
- :param marker: id of image last seen by caller. Used for pagination.
+ :param page_size: number of items to request in each paginated request
+ :param limit: maximum number of images to return
+ :param marker: begin returning images that appear later in the image
+ list than that represented by this image id
+ :param filters: dict of direct comparison filters that mimics the
+ structure of an image object
:rtype: list of :class:`Image`
"""
- params = {}
- if limit:
- params['limit'] = int(limit)
- if marker:
- params['marker'] = marker
- if filters:
- properties = filters.pop('properties', {})
- for key, value in properties.items():
- params['property-%s' % key] = value
- params.update(filters)
- query = '?%s' % urllib.urlencode(params) if params else ''
- return self._list('/v1/images/detail%s' % query, "images")
+ limit = kwargs.get('limit')
+
+ def paginate(qp, seen=0):
+ url = '/v1/images/detail?%s' % urllib.urlencode(qp)
+ images = self._list(url, "images")
+ for image in images:
+ seen += 1
+ yield image
+
+ page_size = qp.get('limit')
+ if (page_size and len(images) == page_size and
+ (limit is None or 0 < seen < limit)):
+ qp['marker'] = image.id
+ for image in paginate(qp, seen):
+ yield image
+
+ params = {'limit': kwargs.get('page_size', DEFAULT_PAGE_SIZE)}
+
+ if 'marker' in kwargs:
+ params['marker'] = kwargs['marker']
+
+ filters = kwargs.get('filters', {})
+ properties = filters.pop('properties', {})
+ for key, value in properties.items():
+ params['property-%s' % key] = value
+ params.update(filters)
+
+ return paginate(params)
def delete(self, image):
"""Delete an image."""