Fix for Volumev2 Pagination

Constrcuted volume pagination properly with 'volumes_links' in
services.py aggregate function. Used extra parameters and changed
the tests accordingly. Added a test for volumes pagination.

Change-Id: I3755b11a274aa66fe3096233cb161ca41080464c
Closes-bug: 1646523
This commit is contained in:
wjdan.alharthi 2016-12-02 19:23:41 +00:00
parent 04e83a8064
commit 12a27eac33
3 changed files with 22 additions and 14 deletions

View File

@ -212,8 +212,9 @@ class RequestHandler(object):
return flask.Response(
services.aggregate(responses,
self.action[0],
request.args.to_dict(),
request.base_url,
self.service_type,
params=request.args.to_dict(),
path=request.base_url,
detailed=self.detailed),
200,
content_type=responses['default'].headers['content-type']

View File

@ -46,7 +46,8 @@ def construct_url(service_provider, service_type,
}
def aggregate(responses, key, params=None, path=None, detailed=True):
def aggregate(responses, key, service_type,
params=None, path=None, detailed=True):
"""Combine responses from several clusters into one response."""
if params:
limit = int(params.get('limit', 0))
@ -103,7 +104,13 @@ def aggregate(responses, key, params=None, path=None, detailed=True):
response['start'] = '%s?%s' % (path, parse.urlencode(params))
if end < last:
params['marker'] = response[key][-1]['id']
response['next'] = '%s?%s' % (path, parse.urlencode(params))
if service_type == 'image':
response['next'] = '%s?%s' % (path, parse.urlencode(params))
elif service_type == 'volume':
response['volumes_links'] = [
{"href": '%s?%s' % (path, parse.urlencode(params)),
"rel": "next"}
]
return json.dumps(response)

View File

@ -79,18 +79,18 @@ class TestServices(testcase.TestCase):
def test_aggregate_key(self):
# Aggregate 'images'
response = json.loads(services.aggregate(IMAGES, 'images'))
response = json.loads(services.aggregate(IMAGES, 'images', 'image'))
self.assertEqual(IMAGES_IN_SAMPLE, len(response['images']))
# Aggregate 'volumes'
response = json.loads(services.aggregate(VOLUMES, 'volumes'))
response = json.loads(services.aggregate(VOLUMES, 'volumes', 'volume'))
self.assertEqual(VOLUMES_IN_SAMPLE, len(response['volumes']))
def test_aggregate_limit(self):
params = {
'limit': 1
}
response = json.loads(services.aggregate(IMAGES, 'images',
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
self.assertEqual(1, len(response['images']))
@ -99,7 +99,7 @@ class TestServices(testcase.TestCase):
params = {
'sort': 'size:asc'
}
response = json.loads(services.aggregate(IMAGES, 'images',
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
self.assertEqual(response['images'][0]['id'], SMALLEST_IMAGE)
@ -110,7 +110,7 @@ class TestServices(testcase.TestCase):
'sort_dir': 'asc',
'limit': 1
}
response = json.loads(services.aggregate(IMAGES, 'images',
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
# Ensure the smallest is first and there is only 1 entry.
@ -132,7 +132,7 @@ class TestServices(testcase.TestCase):
'sort': 'updated_at:asc',
'limit': 2
}
response = json.loads(services.aggregate(IMAGES, 'images',
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
# Check the first and second are the correct ids.
@ -155,7 +155,7 @@ class TestServices(testcase.TestCase):
'sort': 'updated_at:desc',
'limit': 1
}
response = json.loads(services.aggregate(IMAGES, 'images',
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
# Check the id and size
@ -178,7 +178,7 @@ class TestServices(testcase.TestCase):
'limit': 1,
'marker': EARLIEST_IMAGE
}
response = json.loads(services.aggregate(IMAGES, 'images',
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
# Ensure we skipped the first one
@ -210,7 +210,7 @@ class TestServices(testcase.TestCase):
'marker': EARLIEST_IMAGE
}
response = json.loads(services.aggregate(IMAGES, 'images',
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
# Ensure we skipped the first one
@ -233,7 +233,7 @@ class TestServices(testcase.TestCase):
'marker': LATEST_IMAGE
}
response = json.loads(services.aggregate(IMAGES, 'images',
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
# Ensure we skipped the first one