[Kimchi-devel] [PATCH 08/15] Move all resources related to storage pools to control/storagepools.py

Rodrigo Trujillo rodrigo.trujillo at linux.vnet.ibm.com
Mon Dec 30 17:41:38 UTC 2013


Reviewed-by: Rodrigo Trujillo <rodrigo.trujillo at linux.vnet.ibm.com>

On 12/26/2013 07:48 PM, Aline Manera wrote:
> From: Aline Manera <alinefm at br.ibm.com>
>
> StoragePools(Collection), StoragePool(Resource) and IsoPool(Resource) were moved
> to a new - control/storagepools.py
> That way we can easily know where storage pool resource is implemented.
>
> Signed-off-by: Aline Manera <alinefm at br.ibm.com>
> ---
>   src/kimchi/control/storagepools.py |  127 ++++++++++++++++++++++++++++++++++++
>   src/kimchi/controller.py           |   89 -------------------------
>   src/kimchi/root.py                 |    3 +-
>   3 files changed, 129 insertions(+), 90 deletions(-)
>   create mode 100644 src/kimchi/control/storagepools.py
>
> diff --git a/src/kimchi/control/storagepools.py b/src/kimchi/control/storagepools.py
> new file mode 100644
> index 0000000..466b4b6
> --- /dev/null
> +++ b/src/kimchi/control/storagepools.py
> @@ -0,0 +1,127 @@
> +#
> +# Project Kimchi
> +#
> +# Copyright IBM, Corp. 2013
> +#
> +# Authors:
> +#  Adam Litke <agl at linux.vnet.ibm.com>
> +#  Aline Manera <alinefm at linux.vnet.ibm.com>
> +#  Bing Bu Cao <mars at linux.vnet.ibm.com>
> +#  Royce Lv <lvroyce at linux.vnet.ibm.com>
> +#
> +# This library is free software; you can redistribute it and/or
> +# modify it under the terms of the GNU Lesser General Public
> +# License as published by the Free Software Foundation; either
> +# version 2.1 of the License, or (at your option) any later version.
> +#
> +# This library is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +# Lesser General Public License for more details.
> +#
> +# You should have received a copy of the GNU Lesser General Public
> +# License along with this library; if not, write to the Free Software
> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
> +
> +import cherrypy
> +
> +
> +from kimchi.control.base import Collection, Resource
> +from kimchi.control.storagevolumes import IsoVolumes, StorageVolumes
> +from kimchi.control.utils import get_class_name, model_fn, parse_request
> +from kimchi.model import ISO_POOL_NAME
> +
> +
> +class StoragePools(Collection):
> +    def __init__(self, model):
> +        super(StoragePools, self).__init__(model)
> +        self.resource = StoragePool
> +        isos = IsoPool(model)
> +        isos.exposed = True
> +        setattr(self, ISO_POOL_NAME, isos)
> +
> +    def create(self, *args):
> +        try:
> +            create = getattr(self.model, model_fn(self, 'create'))
> +        except AttributeError:
> +            error = 'Create is not allowed for %s' % get_class_name(self)
> +            raise cherrypy.HTTPError(405, error)
> +
> +        params = parse_request()
> +        args = self.model_args + [params]
> +        name = create(*args)
> +        args = self.resource_args + [name]
> +        res = self.resource(self.model, *args)
> +        resp = res.get()
> +
> +        if 'task_id' in res.data:
> +            cherrypy.response.status = 202
> +        else:
> +            cherrypy.response.status = 201
> +
> +        return resp
> +
> +    def _get_resources(self):
> +        try:
> +            res_list = super(StoragePools, self)._get_resources()
> +            # Append reserved pools
> +            isos = getattr(self, ISO_POOL_NAME)
> +            isos.lookup()
> +            res_list.append(isos)
> +        except AttributeError:
> +            pass
> +
> +        return res_list
> +
> +
> +class StoragePool(Resource):
> +    def __init__(self, model, ident):
> +        super(StoragePool, self).__init__(model, ident)
> +        self.update_params = ["autostart"]
> +        self.uri_fmt = "/storagepools/%s"
> +        self.activate = self.generate_action_handler('activate')
> +        self.deactivate = self.generate_action_handler('deactivate')
> +
> +    @property
> +    def data(self):
> +        res = {'name': self.ident,
> +               'state': self.info['state'],
> +               'capacity': self.info['capacity'],
> +               'allocated': self.info['allocated'],
> +               'available': self.info['available'],
> +               'path': self.info['path'],
> +               'source': self.info['source'],
> +               'type': self.info['type'],
> +               'nr_volumes': self.info['nr_volumes'],
> +               'autostart': self.info['autostart']}
> +
> +        val = self.info.get('task_id')
> +        if val:
> +            res['task_id'] = val
> +
> +        return res
> +
> +    def _cp_dispatch(self, vpath):
> +        if vpath:
> +            subcollection = vpath.pop(0)
> +            if subcollection == 'storagevolumes':
> +                # incoming text, from URL, is not unicode, need decode
> +                return StorageVolumes(self.model, self.ident.decode("utf-8"))
> +
> +
> +class IsoPool(Resource):
> +    def __init__(self, model):
> +        super(IsoPool, self).__init__(model, ISO_POOL_NAME)
> +
> +    @property
> +    def data(self):
> +        return {'name': self.ident,
> +                'state': self.info['state'],
> +                'type': self.info['type']}
> +
> +    def _cp_dispatch(self, vpath):
> +        if vpath:
> +            subcollection = vpath.pop(0)
> +            if subcollection == 'storagevolumes':
> +                # incoming text, from URL, is not unicode, need decode
> +                return IsoVolumes(self.model, self.ident.decode("utf-8"))
> diff --git a/src/kimchi/controller.py b/src/kimchi/controller.py
> index a856efd..535f816 100644
> --- a/src/kimchi/controller.py
> +++ b/src/kimchi/controller.py
> @@ -129,95 +129,6 @@ class StorageVolumes(Collection):
>           self.model_args = [self.pool, ]
>
>
> -class StoragePool(Resource):
> -    def __init__(self, model, ident):
> -        super(StoragePool, self).__init__(model, ident)
> -        self.update_params = ["autostart"]
> -        self.uri_fmt = "/storagepools/%s"
> -        self.activate = self.generate_action_handler('activate')
> -        self.deactivate = self.generate_action_handler('deactivate')
> -
> -    @property
> -    def data(self):
> -        res = {'name': self.ident,
> -               'state': self.info['state'],
> -               'capacity': self.info['capacity'],
> -               'allocated': self.info['allocated'],
> -               'available': self.info['available'],
> -               'path': self.info['path'],
> -               'source': self.info['source'],
> -               'type': self.info['type'],
> -               'nr_volumes': self.info['nr_volumes'],
> -               'autostart': self.info['autostart']}
> -        val = self.info.get('task_id')
> -        if val:
> -            res['task_id'] = val
> -        return res
> -
> -
> -    def _cp_dispatch(self, vpath):
> -        if vpath:
> -            subcollection = vpath.pop(0)
> -            if subcollection == 'storagevolumes':
> -                # incoming text, from URL, is not unicode, need decode
> -                return StorageVolumes(self.model, self.ident.decode("utf-8"))
> -
> -
> -class IsoPool(Resource):
> -    def __init__(self, model):
> -        super(IsoPool, self).__init__(model, ISO_POOL_NAME)
> -
> -    @property
> -    def data(self):
> -        return {'name': self.ident,
> -                'state': self.info['state'],
> -                'type': self.info['type']}
> -
> -    def _cp_dispatch(self, vpath):
> -        if vpath:
> -            subcollection = vpath.pop(0)
> -            if subcollection == 'storagevolumes':
> -                # incoming text, from URL, is not unicode, need decode
> -                return IsoVolumes(self.model, self.ident.decode("utf-8"))
> -
> -
> -class StoragePools(Collection):
> -    def __init__(self, model):
> -        super(StoragePools, self).__init__(model)
> -        self.resource = StoragePool
> -        isos = IsoPool(model)
> -        isos.exposed = True
> -        setattr(self, ISO_POOL_NAME, isos)
> -
> -    def create(self, *args):
> -        try:
> -            create = getattr(self.model, model_fn(self, 'create'))
> -        except AttributeError:
> -            raise cherrypy.HTTPError(405,
> -                'Create is not allowed for %s' % get_class_name(self))
> -        params = parse_request()
> -        args = self.model_args + [params]
> -        name = create(*args)
> -        args = self.resource_args + [name]
> -        res = self.resource(self.model, *args)
> -        resp = res.get()
> -        if 'task_id' in res.data:
> -            cherrypy.response.status = 202
> -        else:
> -            cherrypy.response.status = 201
> -        return resp
> -
> -    def _get_resources(self):
> -        try:
> -            res_list = super(StoragePools, self)._get_resources()
> -            # Append reserved pools
> -            isos = getattr(self, ISO_POOL_NAME)
> -            isos.lookup()
> -            res_list.append(isos)
> -        except AttributeError:
> -            pass
> -        return res_list
> -
>   class Task(Resource):
>       def __init__(self, model, id):
>           super(Task, self).__init__(model, id)
> diff --git a/src/kimchi/root.py b/src/kimchi/root.py
> index d2aeb9f..6e41190 100644
> --- a/src/kimchi/root.py
> +++ b/src/kimchi/root.py
> @@ -32,6 +32,7 @@ from kimchi.config import get_api_schema_file
>   from kimchi.control.utils import parse_request
>   from kimchi.control.base import Resource
>   from kimchi.control.debugreports import DebugReports
> +from kimchi.control.storagepools import StoragePools
>   from kimchi.control.templates import Templates
>   from kimchi.control.vms import VMs
>   from kimchi.exception import OperationFailed
> @@ -53,7 +54,7 @@ class Root(Resource):
>           Resource.__init__(self, model)
>           self.vms = VMs(model)
>           self.templates = Templates(model)
> -        self.storagepools = controller.StoragePools(model)
> +        self.storagepools = StoragePools(model)
>           self.interfaces = controller.Interfaces(model)
>           self.networks = controller.Networks(model)
>           self.tasks = controller.Tasks(model)




More information about the Kimchi-devel mailing list