Fix quotas API pagination
Currently quotas-list operation with limit is failing since there is no uuid attribute defined in Quota object. (Please refer to bug#1662935 report for more details) Change-Id: I7ec53f990b1223fe3c72fc7a20fb8261c12e8398 Closes-Bug: #1662935
This commit is contained in:
parent
270faf8050
commit
528dff14f0
|
@ -33,7 +33,7 @@ class Collection(base.APIBase):
|
|||
"""Return whether collection has more items."""
|
||||
return len(self.collection) and len(self.collection) == limit
|
||||
|
||||
def get_next(self, limit, url=None, **kwargs):
|
||||
def get_next(self, limit, url=None, marker_attribute='uuid', **kwargs):
|
||||
"""Return a link to the next subset of the collection."""
|
||||
if not self.has_next(limit):
|
||||
return wtypes.Unset
|
||||
|
@ -42,7 +42,7 @@ class Collection(base.APIBase):
|
|||
q_args = ''.join(['%s=%s&' % (key, kwargs[key]) for key in kwargs])
|
||||
next_args = '?%(args)slimit=%(limit)d&marker=%(marker)s' % {
|
||||
'args': q_args, 'limit': limit,
|
||||
'marker': self.collection[-1].uuid}
|
||||
'marker': getattr(self.collection[-1], marker_attribute)}
|
||||
|
||||
return link.Link.make_link('next', pecan.request.host_url,
|
||||
resource_url, next_args).href
|
||||
|
|
|
@ -79,7 +79,9 @@ class QuotaCollection(collection.Collection):
|
|||
def convert(quotas, limit, **kwargs):
|
||||
collection = QuotaCollection()
|
||||
collection.quotas = [Quota.convert(p) for p in quotas]
|
||||
collection.next = collection.get_next(limit, **kwargs)
|
||||
collection.next = collection.get_next(limit,
|
||||
marker_attribute='id',
|
||||
**kwargs)
|
||||
return collection
|
||||
|
||||
|
||||
|
|
|
@ -100,6 +100,29 @@ class TestQuota(api_base.FunctionalTest):
|
|||
self.assertEqual(1, len(response['quotas']))
|
||||
self.assertEqual('proj-id-1', response['quotas'][0]['project_id'])
|
||||
|
||||
@mock.patch("magnum.common.policy.enforce")
|
||||
@mock.patch("magnum.common.context.make_context")
|
||||
def test_get_all_with_pagination_limit(self, mock_context,
|
||||
mock_policy):
|
||||
mock_context.return_value = self.context
|
||||
quota_list = []
|
||||
for i in range(4):
|
||||
quota = obj_utils.create_test_quota(self.context,
|
||||
project_id="proj-id-"+str(i))
|
||||
quota_list.append(quota)
|
||||
|
||||
self.context.is_admin = True
|
||||
response = self.get_json('/quotas?limit=2&all_tenants=True')
|
||||
self.assertEqual(2, len(response['quotas']))
|
||||
expected = [r.project_id for r in quota_list[:2]]
|
||||
res_proj_ids = [r['project_id'] for r in response['quotas']]
|
||||
self.assertEqual(sorted(expected), sorted(res_proj_ids))
|
||||
self.assertTrue('http://localhost/v1/quotas?' in response['next'])
|
||||
self.assertTrue('sort_key=id' in response['next'])
|
||||
self.assertTrue('sort_dir=asc' in response['next'])
|
||||
self.assertTrue('limit=2' in response['next'])
|
||||
self.assertTrue('marker=%s' % quota_list[1].id in response['next'])
|
||||
|
||||
@mock.patch("magnum.common.policy.enforce")
|
||||
@mock.patch("magnum.common.context.make_context")
|
||||
def test_get_all_admin_all_with_pagination_marker(self, mock_context,
|
||||
|
|
Loading…
Reference in New Issue