diff --git a/octavia/api/common/pagination.py b/octavia/api/common/pagination.py index 110a60ef7a..cf2b888dff 100644 --- a/octavia/api/common/pagination.py +++ b/octavia/api/common/pagination.py @@ -169,7 +169,7 @@ class PaginationHelper: # TODO(rm_work) Do we need to know when there are more vs exact? # We safely know if we have a full page, but it might include the # last element or it might not, it is unclear - if len(model_list) >= self.limit: + if self.limit is None or len(model_list) >= self.limit: next_attr.append("marker={}".format(model_list[-1].get('id'))) next_link = { "rel": "next", diff --git a/octavia/tests/unit/api/common/test_pagination.py b/octavia/tests/unit/api/common/test_pagination.py index 1d80c3fa2f..2ad6f2fb24 100644 --- a/octavia/tests/unit/api/common/test_pagination.py +++ b/octavia/tests/unit/api/common/test_pagination.py @@ -228,8 +228,9 @@ class TestPaginationHelper(base.TestCase): links = helper._make_links(model_list) self.assertEqual(links[0].rel, "previous") self.assertEqual( - links[1].href, - "{base_uri}{path}?limit={limit}&marker={marker}".format( + links[0].href, + ("{base_uri}{path}?limit={limit}&marker={marker}" + "&page_reverse=True").format( base_uri=api_base_uri, path=request_mock.path, limit=params['limit'], @@ -243,3 +244,69 @@ class TestPaginationHelper(base.TestCase): path=request_mock.path, limit=params['limit'], marker=member1.id)) + + @mock.patch('octavia.api.common.pagination.request') + def test_make_links_with_zero_limit(self, request_mock): + request_mock.path = "/lbaas/v2/pools/1/members" + request_mock.path_url = "http://localhost" + request_mock.path + api_base_uri = "https://127.0.0.1" + conf = self.useFixture(oslo_fixture.Config(cfg.CONF)) + conf.config(group='api_settings', api_base_uri=api_base_uri) + member1 = models.Member() + member1.id = uuidutils.generate_uuid() + model_list = [member1] + + params = {'limit': 0, 'marker': member1.id} + helper = pagination.PaginationHelper(params) + links = helper._make_links(model_list) + self.assertEqual(links[0].rel, "previous") + self.assertEqual( + links[0].href, + ("{base_uri}{path}?limit={limit}&marker={marker}" + "&page_reverse=True").format( + base_uri=api_base_uri, + path=request_mock.path, + limit=None, + marker=member1.id + )) + self.assertEqual(links[1].rel, "next") + self.assertEqual( + links[1].href, + "{base_uri}{path}?limit={limit}&marker={marker}".format( + base_uri=api_base_uri, + path=request_mock.path, + limit=None, + marker=member1.id)) + + @mock.patch('octavia.api.common.pagination.request') + def test_make_links_with_negative_limit(self, request_mock): + request_mock.path = "/lbaas/v2/pools/1/members" + request_mock.path_url = "http://localhost" + request_mock.path + api_base_uri = "https://127.0.0.1" + conf = self.useFixture(oslo_fixture.Config(cfg.CONF)) + conf.config(group='api_settings', api_base_uri=api_base_uri) + member1 = models.Member() + member1.id = uuidutils.generate_uuid() + model_list = [member1] + + params = {'limit': -1, 'marker': member1.id} + helper = pagination.PaginationHelper(params) + links = helper._make_links(model_list) + self.assertEqual(links[0].rel, "previous") + self.assertEqual( + links[0].href, + ("{base_uri}{path}?limit={limit}&marker={marker}" + "&page_reverse=True").format( + base_uri=api_base_uri, + path=request_mock.path, + limit=None, + marker=member1.id + )) + self.assertEqual(links[1].rel, "next") + self.assertEqual( + links[1].href, + "{base_uri}{path}?limit={limit}&marker={marker}".format( + base_uri=api_base_uri, + path=request_mock.path, + limit=None, + marker=member1.id)) diff --git a/releasenotes/notes/Fix-limit-pagination-less-or-equal-zero-93a33f1318ea34e5.yaml b/releasenotes/notes/Fix-limit-pagination-less-or-equal-zero-93a33f1318ea34e5.yaml new file mode 100644 index 0000000000..3ea3a967a3 --- /dev/null +++ b/releasenotes/notes/Fix-limit-pagination-less-or-equal-zero-93a33f1318ea34e5.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fix the issue, when "limit" parameter in request less or equal 0. + Now it returns resources according pagination_max_limit as expected, + instead of error.