Pecan: filter items need type conversion

In order for filtering to work properly on GET requests, filter
values should be converted to the appropriate type specified in
neutron.api.v2.attributes.RESOURCE_ATTRIBUTE_MAP.

Without this change queries on boolean attributes, for instance,
won't work as the value to filter on will be passed to the plugin
as a string.

Closes Bug: #1537924

Change-Id: I0a697a01f272f94378928ecc099232e3b2d11f1f
This commit is contained in:
Salvatore Orlando 2016-01-25 14:23:13 -08:00
parent e85fc565bb
commit f392a04051
3 changed files with 16 additions and 5 deletions

View File

@ -17,7 +17,6 @@ import functools
from oslo_config import cfg
from oslo_log import log as logging
import six
from six.moves.urllib import parse
from webob import exc
@ -30,7 +29,13 @@ LOG = logging.getLogger(__name__)
def get_filters(request, attr_info, skips=None):
"""Extracts the filters from the request string.
return get_filters_from_dict(request.GET.dict_of_lists(),
attr_info,
skips)
def get_filters_from_dict(data, attr_info, skips=None):
"""Extracts the filters from a dict of query parameters.
Returns a dict of lists for the filters:
check=a&check=b&name=Bob&
@ -39,7 +44,7 @@ def get_filters(request, attr_info, skips=None):
"""
skips = skips or []
res = {}
for key, values in six.iteritems(request.GET.dict_of_lists()):
for key, values in data.items():
if key in skips:
continue
values = [v for v in values if v]

View File

@ -15,6 +15,7 @@
import pecan
from pecan import request
from neutron.api import api_common
from neutron.pecan_wsgi.controllers import utils
@ -79,8 +80,11 @@ class CollectionsController(utils.NeutronPecanController):
# after making sure policy enforced fields remain
kwargs.pop('fields', None)
_listify = lambda x: x if isinstance(x, list) else [x]
filters = {k: _listify(v) for k, v in kwargs.items()}
# TODO(kevinbenton): convert these using api_common.get_filters
filters = api_common.get_filters_from_dict(
{k: _listify(v) for k, v in kwargs.items()},
self._resource_info,
skips=['fields', 'sort_key', 'sort_dir',
'limit', 'marker', 'page_reverse'])
lister = getattr(self.plugin, 'get_%s' % self.collection)
neutron_context = request.context['neutron_context']
return {self.collection: lister(neutron_context, filters=filters)}

View File

@ -15,6 +15,7 @@
import pecan
from neutron.api.v2 import attributes as api_attributes
from neutron import manager
# Utility functions for Pecan controllers.
@ -40,6 +41,7 @@ class NeutronPecanController(object):
# Ensure dashes are always replaced with underscores
self.collection = collection and collection.replace('-', '_')
self.resource = resource and resource.replace('-', '_')
self._resource_info = api_attributes.get_collection_info(collection)
self._plugin = None
@property