[Kimchi-devel] [PATCHv1 1/5] Storage volume upload: Dispatch volume create to right handler

Aline Manera alinefm at linux.vnet.ibm.com
Mon Sep 1 16:17:31 UTC 2014


On 09/01/2014 08:50 AM, lvroyce0210 at gmail.com wrote:
> From: Royce Lv <lvroyce at linux.vnet.ibm.com>
>
> As we are starting to support upload and download to create volume,
> they need to be distinguished from previous creating through libvirt api.
> Adding a dispatcher to support this.
>
> Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
> ---
>   src/kimchi/i18n.py                 |  2 ++
>   src/kimchi/model/storagevolumes.py | 14 ++++++++++++++
>   2 files changed, 16 insertions(+)
>
> diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
> index 2eae7e8..bbec591 100644
> --- a/src/kimchi/i18n.py
> +++ b/src/kimchi/i18n.py
> @@ -185,6 +185,8 @@ messages = {
>       "KCHVOL0015E": _("Storage volume format not supported"),
>       "KCHVOL0016E": _("Storage volume requires a volume name"),
>       "KCHVOL0017E": _("Unable to update database with storage volume information due error: %(err)s"),
> +    "KCHVOL0018E": _("Only one of %(param)s can be specified"),
> +    "KCHVOL0019E": _("Creating volume from %(param)s is not supported"),
>
>       "KCHIFACE0001E": _("Interface %(name)s does not exist"),
>
> diff --git a/src/kimchi/model/storagevolumes.py b/src/kimchi/model/storagevolumes.py
> index b60884c..fc63a16 100644
> --- a/src/kimchi/model/storagevolumes.py
> +++ b/src/kimchi/model/storagevolumes.py
> @@ -44,6 +44,20 @@ class StorageVolumesModel(object):
>           self.objstore = kargs['objstore']
>
>       def create(self, pool_name, params):
> +        vol_source = ['file', 'url', 'capacity']
> +
> +        if sum(1 for p in vol_source if p in params) != 1:
> +            raise InvalidParameter("KCHVOL0018E", {'param': str(vol_source)})

str(vol_source) will generate a string like "[' file', 'url', 'capacity']"
I suggest to use ", ".join(a) to generate a string like "file, url, 
capacity"

> +
> +        for p in vol_source:
> +            if p in params:

As just one option is allowed you don't need a "for" statement
You should be able to do:

+                try:
+                    create_func = getattr(self, "_create_volume_with_" +*vol_source[0]*)
+                except AttributeError:
+                    raise InvalidParameter("KCHVOL0019E", {'param':*vol_source[0]*})
+                return create_func(pool_name, params)



> +                try:
> +                    create_func = getattr(self, "_create_volume_with_" + p)
> +                except AttributeError:
> +                    raise InvalidParameter("KCHVOL0019E", {'param': p})
> +                return create_func(pool_name, params)
> +
> +    def _create_volume_with_capacity(self, pool_name, params):
>           vol_xml = """
>           <volume>
>             <name>%(name)s</name>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ovirt.org/pipermail/kimchi-devel/attachments/20140901/f11294d6/attachment.html>


More information about the Kimchi-devel mailing list