summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEndre Karlson <endre.karlson@hp.com>2014-02-19 13:31:47 +0100
committerKiall Mac Innes <kiall@hp.com>2014-02-20 13:54:30 +0000
commit6dc8d4dd638aa6acd7c881808bf2d87113e490d6 (patch)
tree6bbf9ff6b538d23077803020139ab901c84cd449
parentf7750dcca0dc3c42f022fb58170927ecde4b47cd (diff)
downloaddesignate-6dc8d4dd638aa6acd7c881808bf2d87113e490d6.tar.gz
Add support for paging in V2 api for collections
Change-Id: Iab7e16b11e024fe573a40cfdbfc2a1a3a8ca07dc
-rw-r--r--designate/api/v2/controllers/blacklists.py6
-rw-r--r--designate/api/v2/controllers/records.py6
-rw-r--r--designate/api/v2/controllers/recordsets.py6
-rw-r--r--designate/api/v2/controllers/rest.py10
-rw-r--r--designate/api/v2/controllers/tlds.py6
-rw-r--r--designate/api/v2/controllers/zones.py7
-rw-r--r--designate/api/v2/views/base.py7
-rw-r--r--designate/tests/test_api/test_v2/__init__.py25
-rw-r--r--designate/tests/test_api/test_v2/test_blacklists.py17
-rw-r--r--designate/tests/test_api/test_v2/test_recordsets.py19
-rw-r--r--designate/tests/test_api/test_v2/test_tlds.py17
-rw-r--r--designate/tests/test_api/test_v2/test_zones.py19
12 files changed, 69 insertions, 76 deletions
diff --git a/designate/api/v2/controllers/blacklists.py b/designate/api/v2/controllers/blacklists.py
index bdfc64bf..4ea41189 100644
--- a/designate/api/v2/controllers/blacklists.py
+++ b/designate/api/v2/controllers/blacklists.py
@@ -49,15 +49,15 @@ class BlacklistsController(rest.RestController):
context = request.environ['context']
# Extract the pagination params
- #marker = params.pop('marker', None)
- #limit = int(params.pop('limit', 30))
+ marker, limit, sort_key, sort_dir = self._get_paging_params(params)
# Extract any filter params
accepted_filters = ('pattern')
criterion = dict((k, params[k]) for k in accepted_filters
if k in params)
- blacklist = central_api.find_blacklists(context, criterion)
+ blacklist = central_api.find_blacklists(
+ context, criterion, marker, limit, sort_key, sort_dir)
return self._view.list(context, request, blacklist)
diff --git a/designate/api/v2/controllers/records.py b/designate/api/v2/controllers/records.py
index f001fa7f..de132dd8 100644
--- a/designate/api/v2/controllers/records.py
+++ b/designate/api/v2/controllers/records.py
@@ -50,8 +50,7 @@ class RecordsController(rest.RestController):
context = request.environ['context']
# Extract the pagination params
- #marker = params.pop('marker', None)
- #limit = int(params.pop('limit', 30))
+ marker, limit, sort_key, sort_dir = self._get_paging_params(params)
# Extract any filter params.
accepted_filters = ('data', )
@@ -61,7 +60,8 @@ class RecordsController(rest.RestController):
criterion['domain_id'] = zone_id
criterion['recordset_id'] = recordset_id
- records = central_api.find_records(context, criterion)
+ records = central_api.find_records(
+ context, criterion, marker, limit, sort_key, sort_dir)
return self._view.list(context, request, records,
[zone_id, recordset_id])
diff --git a/designate/api/v2/controllers/recordsets.py b/designate/api/v2/controllers/recordsets.py
index 7ee28323..635821b5 100644
--- a/designate/api/v2/controllers/recordsets.py
+++ b/designate/api/v2/controllers/recordsets.py
@@ -52,8 +52,7 @@ class RecordSetsController(rest.RestController):
context = request.environ['context']
# Extract the pagination params
- #marker = params.pop('marker', None)
- #limit = int(params.pop('limit', 30))
+ marker, limit, sort_key, sort_dir = self._get_paging_params(params)
# Extract any filter params.
accepted_filters = ('name', 'type', 'ttl', )
@@ -62,7 +61,8 @@ class RecordSetsController(rest.RestController):
criterion['domain_id'] = zone_id
- recordsets = central_api.find_recordsets(context, criterion)
+ recordsets = central_api.find_recordsets(
+ context, criterion, marker, limit, sort_key, sort_dir)
return self._view.list(context, request, recordsets, [zone_id])
diff --git a/designate/api/v2/controllers/rest.py b/designate/api/v2/controllers/rest.py
index 55db3ee9..01c6a5c8 100644
--- a/designate/api/v2/controllers/rest.py
+++ b/designate/api/v2/controllers/rest.py
@@ -41,6 +41,16 @@ class RestController(pecan.rest.RestController):
Ideally, we get these additions merged upstream.
"""
+ def _get_paging_params(self, params):
+ """
+ Extract any paging parameters
+ """
+ marker = params.pop('marker', None)
+ limit = int(params.pop('limit', 30))
+ sort_key = params.pop('sort_key', None)
+ sort_dir = params.pop('sort_dir', None)
+ return marker, limit, sort_key, sort_dir
+
def _handle_post(self, method, remainder):
'''
Routes ``POST`` actions to the appropriate controller.
diff --git a/designate/api/v2/controllers/tlds.py b/designate/api/v2/controllers/tlds.py
index 2dc430be..f05cd3b4 100644
--- a/designate/api/v2/controllers/tlds.py
+++ b/designate/api/v2/controllers/tlds.py
@@ -46,15 +46,15 @@ class TldsController(rest.RestController):
context = request.environ['context']
# Extract the pagination params
- #marker = params.pop('marker', None)
- #limit = int(params.pop('limit', 30))
+ marker, limit, sort_key, sort_dir = self._get_paging_params(params)
# Extract any filter params.
accepted_filters = ('name')
criterion = dict((k, params[k]) for k in accepted_filters
if k in params)
- tlds = central_api.find_tlds(context, criterion)
+ tlds = central_api.find_tlds(
+ context, criterion, marker, limit, sort_key, sort_dir)
return self._view.list(context, request, tlds)
diff --git a/designate/api/v2/controllers/zones.py b/designate/api/v2/controllers/zones.py
index 3d0c137e..8196c703 100644
--- a/designate/api/v2/controllers/zones.py
+++ b/designate/api/v2/controllers/zones.py
@@ -101,16 +101,15 @@ class ZonesController(rest.RestController):
request = pecan.request
context = request.environ['context']
- # Extract the pagination params
- #marker = params.pop('marker', None)
- #limit = int(params.pop('limit', 30))
+ marker, limit, sort_key, sort_dir = self._get_paging_params(params)
# Extract any filter params.
accepted_filters = ('name', 'email', )
criterion = dict((k, params[k]) for k in accepted_filters
if k in params)
- zones = central_api.find_domains(context, criterion)
+ zones = central_api.find_domains(
+ context, criterion, marker, limit, sort_key, sort_dir)
return self._view.list(context, request, zones)
diff --git a/designate/api/v2/views/base.py b/designate/api/v2/views/base.py
index 661248ee..85b9d9ab 100644
--- a/designate/api/v2/views/base.py
+++ b/designate/api/v2/views/base.py
@@ -100,9 +100,10 @@ class BaseView(object):
"self": self._get_collection_href(request, parents),
}
- if 'marker' in params:
- result['previous'] = self._get_previous_href(request, items,
- parents)
+ # See above
+ #if 'marker' in params:
+ # result['previous'] = self._get_previous_href(request, items,
+ # parents)
if 'limit' in params and int(params['limit']) == len(items):
result['next'] = self._get_next_href(request, items, parents)
diff --git a/designate/tests/test_api/test_v2/__init__.py b/designate/tests/test_api/test_v2/__init__.py
index ed216b3f..1e549f26 100644
--- a/designate/tests/test_api/test_v2/__init__.py
+++ b/designate/tests/test_api/test_v2/__init__.py
@@ -52,3 +52,28 @@ class ApiV2TestCase(ApiTestCase):
self.client = None
super(ApiV2TestCase, self).tearDown()
+
+ def _assert_paging(self, data, url, key=None, limit=5):
+ def _page(marker=None):
+ params = {'limit': limit}
+
+ if marker is not None:
+ params['marker'] = marker
+
+ r = self.client.get(url, params)
+ return r.json[key] if key in r.json else r.json
+
+ page_items = _page()
+
+ x = 0
+ length = len(data)
+ for i in xrange(0, length):
+ assert data[i]['id'] == page_items[x]['id']
+
+ x += 1
+ # Don't bother getting a new page if we're at the last item
+ if x == len(page_items) and i != length - 1:
+ x = 0
+ page_items = _page(page_items[-1:][0]['id'])
+
+ _page(marker=page_items[-1:][0]['id'])
diff --git a/designate/tests/test_api/test_v2/test_blacklists.py b/designate/tests/test_api/test_v2/test_blacklists.py
index 911bf5fa..44ec1331 100644
--- a/designate/tests/test_api/test_v2/test_blacklists.py
+++ b/designate/tests/test_api/test_v2/test_blacklists.py
@@ -39,21 +39,10 @@ class ApiV2BlacklistsTest(ApiV2TestCase):
# Test with 0 blacklists
self.assertEqual(0, len(response.json['blacklists']))
- # Test with 1 blacklist
- self.create_blacklist(fixture=0)
+ data = [self.create_blacklist(
+ pattern='x-%s.org.' % i) for i in xrange(0, 10)]
- response = self.client.get('/blacklists/')
-
- self.assertIn('blacklists', response.json)
- self.assertEqual(1, len(response.json['blacklists']))
-
- # test with 2 blacklists
- self.create_blacklist(fixture=1)
-
- response = self.client.get('/blacklists/')
-
- self.assertIn('blacklists', response.json)
- self.assertEqual(2, len(response.json['blacklists']))
+ self._assert_paging(data, '/blacklists', key='blacklists')
def test_get_blacklist(self):
blacklist = self.create_blacklist(fixture=0)
diff --git a/designate/tests/test_api/test_v2/test_recordsets.py b/designate/tests/test_api/test_v2/test_recordsets.py
index cf7f8649..a10e98c2 100644
--- a/designate/tests/test_api/test_v2/test_recordsets.py
+++ b/designate/tests/test_api/test_v2/test_recordsets.py
@@ -114,21 +114,12 @@ class ApiV2RecordSetsTest(ApiV2TestCase):
# We should start with 0 recordsets
self.assertEqual(0, len(response.json['recordsets']))
- # Test with 1 recordset
- self.create_recordset(self.domain)
+ data = [self.create_recordset(self.domain,
+ name='x-%s.%s' % (i, self.domain['name']))
+ for i in xrange(0, 10)]
- response = self.client.get('/zones/%s/recordsets' % self.domain['id'])
-
- self.assertIn('recordsets', response.json)
- self.assertEqual(1, len(response.json['recordsets']))
-
- # test with 2 recordsets
- self.create_recordset(self.domain, fixture=1)
-
- response = self.client.get('/zones/%s/recordsets' % self.domain['id'])
-
- self.assertIn('recordsets', response.json)
- self.assertEqual(2, len(response.json['recordsets']))
+ url = '/zones/%s/recordsets' % self.domain['id']
+ self._assert_paging(data, url, key='recordsets')
@patch.object(central_service.Service, 'find_recordsets',
side_effect=rpc_common.Timeout())
diff --git a/designate/tests/test_api/test_v2/test_tlds.py b/designate/tests/test_api/test_v2/test_tlds.py
index 847af117..45a9a5ac 100644
--- a/designate/tests/test_api/test_v2/test_tlds.py
+++ b/designate/tests/test_api/test_v2/test_tlds.py
@@ -64,21 +64,8 @@ class ApiV2TldsTest(ApiV2TestCase):
# We should start with 0 tlds
self.assertEqual(0, len(response.json['tlds']))
- # Test with 1 tld
- self.create_tld(fixture=0)
-
- response = self.client.get('/tlds/')
-
- self.assertIn('tlds', response.json)
- self.assertEqual(1, len(response.json['tlds']))
-
- # test with 2 tlds
- self.create_tld(fixture=1)
-
- response = self.client.get('/tlds/')
-
- self.assertIn('tlds', response.json)
- self.assertEqual(2, len(response.json['tlds']))
+ data = [self.create_tld(name='tld%s.' % i) for i in 'abcdefghijklmn']
+ self._assert_paging(data, '/tlds', key='tlds')
def test_get_tld(self):
tld = self.create_tld(fixture=0)
diff --git a/designate/tests/test_api/test_v2/test_zones.py b/designate/tests/test_api/test_v2/test_zones.py
index 1c91c64d..94653f56 100644
--- a/designate/tests/test_api/test_v2/test_zones.py
+++ b/designate/tests/test_api/test_v2/test_zones.py
@@ -129,21 +129,12 @@ class ApiV2ZonesTest(ApiV2TestCase):
# We should start with 0 zones
self.assertEqual(0, len(response.json['zones']))
- # Test with 1 zone
- self.create_domain()
-
- response = self.client.get('/zones/')
-
- self.assertIn('zones', response.json)
- self.assertEqual(1, len(response.json['zones']))
-
- # test with 2 zones
- self.create_domain(fixture=1)
-
- response = self.client.get('/zones/')
+ # We should start with 0 zones
+ self.assertEqual(0, len(response.json['zones']))
- self.assertIn('zones', response.json)
- self.assertEqual(2, len(response.json['zones']))
+ data = [self.create_domain(name='x-%s.com.' % i)
+ for i in 'abcdefghij']
+ self._assert_paging(data, '/zones', key='zones')
@patch.object(central_service.Service, 'find_domains',
side_effect=rpc_common.Timeout())