diff --git a/neutron/api/api_common.py b/neutron/api/api_common.py index 0b37344a169..53ca78f9517 100644 --- a/neutron/api/api_common.py +++ b/neutron/api/api_common.py @@ -15,6 +15,7 @@ import functools +from neutron_lib.api import attributes from neutron_lib.db import model_base from neutron_lib import exceptions from oslo_config import cfg @@ -75,6 +76,7 @@ def get_filters_from_dict(data, attr_info, skips=None): becomes: {'check': [u'a', u'b'], 'name': [u'Bob']} """ + attributes.populate_project_info(attr_info) is_empty_string_supported = is_empty_string_filtering_supported() skips = skips or [] res = {} @@ -189,6 +191,7 @@ def get_sorts(request, attr_info): Return as: [(key1, value1), (key2, value2)] """ + attributes.populate_project_info(attr_info) sort_keys = list_args(request, "sort_key") sort_dirs = list_args(request, "sort_dir") if len(sort_keys) != len(sort_dirs): diff --git a/neutron/tests/functional/pecan_wsgi/test_controllers.py b/neutron/tests/functional/pecan_wsgi/test_controllers.py index f8854623e51..dedf3dea832 100644 --- a/neutron/tests/functional/pecan_wsgi/test_controllers.py +++ b/neutron/tests/functional/pecan_wsgi/test_controllers.py @@ -66,7 +66,8 @@ class TestRootController(test_functional.PecanFunctionalTest): manager.NeutronManager.set_controller_for_resource( _SERVICE_PLUGIN_COLLECTION, FakeServicePluginController(_SERVICE_PLUGIN_COLLECTION, - _SERVICE_PLUGIN_RESOURCE)) + _SERVICE_PLUGIN_RESOURCE, + resource_info={'foo': {}})) def _test_method_returns_code(self, method, code=200): api_method = getattr(self.app, method) diff --git a/neutron/tests/unit/api/v2/test_base.py b/neutron/tests/unit/api/v2/test_base.py index 616093242e5..f71962fa235 100644 --- a/neutron/tests/unit/api/v2/test_base.py +++ b/neutron/tests/unit/api/v2/test_base.py @@ -1546,11 +1546,29 @@ class ListArgsTestCase(base.BaseTestCase): self.assertEqual([], api_common.list_args(request, 'fields')) +class SortingTestCase(base.BaseTestCase): + def test_get_sorts(self): + path = '/?sort_key=foo&sort_dir=desc&sort_key=bar&sort_dir=asc' + request = webob.Request.blank(path) + attr_info = {'foo': {'key': 'val'}, 'bar': {'key': 'val'}} + expect_val = [('foo', False), ('bar', True)] + actual_val = api_common.get_sorts(request, attr_info) + self.assertEqual(expect_val, actual_val) + + def test_get_sorts_with_project_id(self): + path = '/?sort_key=project_id&sort_dir=desc' + request = webob.Request.blank(path) + attr_info = {'tenant_id': {'key': 'val'}} + expect_val = [('project_id', False)] + actual_val = api_common.get_sorts(request, attr_info) + self.assertEqual(expect_val, actual_val) + + class FiltersTestCase(base.BaseTestCase): def test_all_skip_args(self): path = '/?fields=4&fields=3&fields=2&fields=1' request = webob.Request.blank(path) - self.assertEqual({}, api_common.get_filters(request, None, + self.assertEqual({}, api_common.get_filters(request, {}, ["fields"])) @mock.patch('neutron.api.api_common.is_empty_string_filtering_supported', @@ -1575,6 +1593,17 @@ class FiltersTestCase(base.BaseTestCase): actual_val = api_common.get_filters(request, {}) self.assertEqual(expect_val, actual_val) + def test_attr_info_with_project_info_populated(self): + path = '/?foo=4&bar=3&baz=2&qux=1' + request = webob.Request.blank(path) + attr_info = {'tenant_id': {'key': 'val'}} + expect_val = {'foo': ['4'], 'bar': ['3'], 'baz': ['2'], 'qux': ['1']} + actual_val = api_common.get_filters(request, attr_info) + self.assertEqual(expect_val, actual_val) + expect_attr_info = {'tenant_id': {'key': 'val'}, + 'project_id': {'key': 'val'}} + self.assertEqual(expect_attr_info, attr_info) + def test_attr_info_without_conversion(self): path = '/?foo=4&bar=3&baz=2&qux=1' request = webob.Request.blank(path)