From e894b3dd9285a1fb711aee63eb96be1cfe44375b Mon Sep 17 00:00:00 2001 From: Yuriy Nesenenko Date: Tue, 25 Aug 2015 13:55:56 +0300 Subject: [PATCH] Parameter osapi_max_limit is always used by default MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Default value max_limit is initialized before a value is read from the config file. Default parameter values are always evaluated when the “def” statement they belong to is executed. So parameter osapi_max_limit is always used by default even it's set in the configuration file. Change-Id: I1414667d30e98b56c5eb1b09b802bab823b37e2d Closes-Bug: #1487510 --- cinder/api/common.py | 12 ++++++++---- cinder/tests/unit/api/test_common.py | 6 ++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cinder/api/common.py b/cinder/api/common.py index 06fee0008..9dc7ed112 100644 --- a/cinder/api/common.py +++ b/cinder/api/common.py @@ -69,7 +69,7 @@ def validate_key_names(key_names_list): return True -def get_pagination_params(params, max_limit=CONF.osapi_max_limit): +def get_pagination_params(params, max_limit=None): """Return marker, limit, offset tuple from request. :param params: `wsgi.Request`'s GET dictionary, possibly containing @@ -85,18 +85,20 @@ def get_pagination_params(params, max_limit=CONF.osapi_max_limit): :max_limit: Max value 'limit' return value can take :returns: Tuple (marker, limit, offset) """ + max_limit = max_limit or CONF.osapi_max_limit limit = _get_limit_param(params, max_limit) marker = _get_marker_param(params) offset = _get_offset_param(params) return marker, limit, offset -def _get_limit_param(params, max_limit=CONF.osapi_max_limit): +def _get_limit_param(params, max_limit=None): """Extract integer limit from request's dictionary or fail. Defaults to max_limit if not present and returns max_limit if present 'limit' is greater than max_limit. """ + max_limit = max_limit or CONF.osapi_max_limit try: limit = int(params.pop('limit', max_limit)) except ValueError: @@ -129,7 +131,7 @@ def _get_offset_param(params): return offset -def limited(items, request, max_limit=CONF.osapi_max_limit): +def limited(items, request, max_limit=None): """Return a slice of items according to requested offset and limit. :param items: A sliceable entity @@ -141,14 +143,16 @@ def limited(items, request, max_limit=CONF.osapi_max_limit): will cause exc.HTTPBadRequest() exceptions to be raised. :kwarg max_limit: The maximum number of items to return from 'items' """ + max_limit = max_limit or CONF.osapi_max_limit marker, limit, offset = get_pagination_params(request.GET.copy(), max_limit) range_end = offset + (limit or max_limit) return items[offset:range_end] -def limited_by_marker(items, request, max_limit=CONF.osapi_max_limit): +def limited_by_marker(items, request, max_limit=None): """Return a slice of items according to the requested marker and limit.""" + max_limit = max_limit or CONF.osapi_max_limit marker, limit, __ = get_pagination_params(request.GET.copy(), max_limit) start_index = 0 diff --git a/cinder/tests/unit/api/test_common.py b/cinder/tests/unit/api/test_common.py index 8050a5e7d..ca7cbf1a7 100644 --- a/cinder/tests/unit/api/test_common.py +++ b/cinder/tests/unit/api/test_common.py @@ -178,10 +178,12 @@ class PaginationParamsTest(test.TestCase): webob.exc.HTTPBadRequest, common.get_pagination_params, req.GET.copy()) - def test_no_params(self): + @mock.patch.object(common, 'CONF') + def test_no_params(self, mock_cfg): """Test no params.""" + mock_cfg.osapi_max_limit = 100 req = webob.Request.blank('/') - expected = (None, CONF.osapi_max_limit, 0) + expected = (None, 100, 0) self.assertEqual(expected, common.get_pagination_params(req.GET.copy())) -- 2.45.2