From 528dff14f0eaba23236e7a802f32e8b273cc4163 Mon Sep 17 00:00:00 2001 From: Vijendar Komalla Date: Thu, 9 Feb 2017 15:48:01 -0600 Subject: [PATCH] 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 --- magnum/api/controllers/v1/collection.py | 4 ++-- magnum/api/controllers/v1/quota.py | 4 +++- .../unit/api/controllers/v1/test_quota.py | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/magnum/api/controllers/v1/collection.py b/magnum/api/controllers/v1/collection.py index 347568bd33..c17251e569 100644 --- a/magnum/api/controllers/v1/collection.py +++ b/magnum/api/controllers/v1/collection.py @@ -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 diff --git a/magnum/api/controllers/v1/quota.py b/magnum/api/controllers/v1/quota.py index 52ad51c174..ad4ec357a5 100644 --- a/magnum/api/controllers/v1/quota.py +++ b/magnum/api/controllers/v1/quota.py @@ -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 diff --git a/magnum/tests/unit/api/controllers/v1/test_quota.py b/magnum/tests/unit/api/controllers/v1/test_quota.py index 23fae76b54..2715a1540a 100644 --- a/magnum/tests/unit/api/controllers/v1/test_quota.py +++ b/magnum/tests/unit/api/controllers/v1/test_quota.py @@ -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,