[Kimchi-devel] [PATCHv9 01/10] Support params for GET method

Aline Manera alinefm at linux.vnet.ibm.com
Thu Jan 23 12:59:23 UTC 2014


Reviewed-by: Aline Manera <alinefm at linux.vnet.ibm.com>

On 01/22/2014 02:13 PM, lvroyce at linux.vnet.ibm.com wrote:
> From: Royce Lv <lvroyce at linux.vnet.ibm.com>
>
> GET filter parameter will be splited in two types:
>    1. flag_filter: pass to libvirt api and filter when resource query.
>           flag_filters start with "_"
>    2. field_filter: take effect in filter out final result.
>           field_filters are keys of dict returned by 'GET'
> Then we can call it like:
>      GET /collection?filter_field=value
>
> Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
> ---
>   src/kimchi/control/base.py           | 31 ++++++++++++++++++++++++-------
>   src/kimchi/control/storagepools.py   |  4 ++--
>   src/kimchi/control/storagevolumes.py |  2 +-
>   3 files changed, 27 insertions(+), 10 deletions(-)
>
> diff --git a/src/kimchi/control/base.py b/src/kimchi/control/base.py
> index 185c8d8..55678b1 100644
> --- a/src/kimchi/control/base.py
> +++ b/src/kimchi/control/base.py
> @@ -212,10 +212,10 @@ class Collection(object):
>
>           return res.get()
>
> -    def _get_resources(self):
> +    def _get_resources(self, flag_filter):
>           try:
>               get_list = getattr(self.model, model_fn(self, 'get_list'))
> -            idents = get_list(*self.model_args)
> +            idents = get_list(*self.model_args, **flag_filter)
>               res_list = []
>               for ident in idents:
>                   # internal text, get_list changes ident to unicode for sorted
> @@ -234,19 +234,36 @@ class Collection(object):
>               args = self.resource_args + [ident.decode("utf-8")]
>               return self.resource(self.model, *args)
>
> -    def get(self):
> -        resources = self._get_resources()
> +    def filter_data(self, resources, fields_filter):
>           data = []
>           for res in resources:
> -            data.append(res.data)
> +            if all(key in res.data and res.data[key] == val \
> +                   for key, val in fields_filter.iteritems()):
> +                data.append(res.data)
> +        return data
> +
> +    def get(self, filter_params):
> +        def _split_filter(params):
> +            flag_filter = dict()
> +            fields_filter = params
> +            for key, val in params.items():
> +                if key.startswith('_'):
> +                    flag_filter[key] = fields_filter.pop(key)
> +            return flag_filter, fields_filter
> +
> +        flag_filter, fields_filter = _split_filter(filter_params)
> +        resources = self._get_resources(flag_filter)
> +        data = self.filter_data(resources, fields_filter)
>           return kimchi.template.render(get_class_name(self), data)
>
>       @cherrypy.expose
> -    def index(self, *args):
> +    def index(self, *args, **kwargs):
>           method = validate_method(('GET', 'POST'))
>           if method == 'GET':
>               try:
> -                return self.get()
> +                filter_params = cherrypy.request.params
> +                validate_params(filter_params, self, 'get_list')
> +                return self.get(filter_params)
>               except InvalidOperation, param:
>                   error = "Invalid operation: '%s'" % param
>                   raise cherrypy.HTTPError(400, error)
> diff --git a/src/kimchi/control/storagepools.py b/src/kimchi/control/storagepools.py
> index 06b7196..35e7664 100644
> --- a/src/kimchi/control/storagepools.py
> +++ b/src/kimchi/control/storagepools.py
> @@ -63,9 +63,9 @@ class StoragePools(Collection):
>
>           return resp
>
> -    def _get_resources(self):
> +    def _get_resources(self, filter_params):
>           try:
> -            res_list = super(StoragePools, self)._get_resources()
> +            res_list = super(StoragePools, self)._get_resources(filter_params)
>               # Append reserved pools
>               isos = getattr(self, ISO_POOL_NAME)
>               isos.lookup()
> diff --git a/src/kimchi/control/storagevolumes.py b/src/kimchi/control/storagevolumes.py
> index d541807..cd15bcc 100644
> --- a/src/kimchi/control/storagevolumes.py
> +++ b/src/kimchi/control/storagevolumes.py
> @@ -70,7 +70,7 @@ class IsoVolumes(Collection):
>           super(IsoVolumes, self).__init__(model)
>           self.pool = pool
>
> -    def get(self):
> +    def get(self, filter_params):
>           res_list = []
>           try:
>               get_list = getattr(self.model, model_fn(self, 'get_list'))




More information about the Kimchi-devel mailing list