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
This commit is contained in:
+37
-15
@@ -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."""
|
||||
|
||||
@@ -36,6 +36,8 @@ import glanceclient.v1.images
|
||||
@utils.arg('--property-filter', metavar='<KEY=VALUE>',
|
||||
help="Filter images by a user-defined image property.",
|
||||
action='append', dest='properties', default=[])
|
||||
@utils.arg('--page-size', metavar='<SIZE>', default=None, type=int,
|
||||
help='Number of images to request in each paginated request.')
|
||||
def do_image_list(gc, args):
|
||||
"""List images."""
|
||||
filter_keys = ['name', 'status', 'container_format', 'disk_format',
|
||||
@@ -47,7 +49,10 @@ def do_image_list(gc, args):
|
||||
property_filter_items = [p.split('=', 1) for p in args.properties]
|
||||
filters['properties'] = dict(property_filter_items)
|
||||
|
||||
images = gc.images.list(filters=filters)
|
||||
kwargs = {'filters': filters}
|
||||
if args.page_size is not None:
|
||||
kwargs['page_size'] = args.page_size
|
||||
images = gc.images.list(**kwargs)
|
||||
columns = ['ID', 'Name', 'Disk Format', 'Container Format',
|
||||
'Size', 'Status']
|
||||
utils.print_list(images, columns)
|
||||
|
||||
Reference in New Issue
Block a user