[Kimchi-devel] [PATCH][Kimchi 2/4] Fix checking duplicate template before creating it

Aline Manera alinefm at linux.vnet.ibm.com
Mon Apr 4 16:23:47 UTC 2016



On 03/31/2016 10:47 AM, Ramon Medeiros wrote:
> Template was being check if duplicated after calling for VMTemplate,
> which can make unecessary work. Check it before creating the class.
>
> Signed-off-by: Ramon Medeiros <ramonn at linux.vnet.ibm.com>
> ---
>   model/templates.py | 11 +++++++----
>   1 file changed, 7 insertions(+), 4 deletions(-)
>
> diff --git a/model/templates.py b/model/templates.py
> index 92705b6..28c8a08 100644
> --- a/model/templates.py
> +++ b/model/templates.py
> @@ -52,6 +52,12 @@ class TemplatesModel(object):
>       def create(self, params):
>           name = params.get('name', '').strip()

>           iso = params.get('cdrom')

cdrom should be removed. The single way to create a template is by 
specifying a source_media value.

> +
> +        # template with the same name already exists: raise exception
> +        with self.objstore as session:
> +            if name in session.get_list('template'):
> +                raise InvalidOperation("KCHTMPL0001E", {'name': name})
> +

The template name is optional. So once user has send an empty name or no 
value for it, Kimchi should determine one name automatically.

Following this patch, in this scenario, you are allowing user creates a 
template name with an empty string.

>           # check search permission
>           if iso and iso.startswith('/') and os.path.exists(iso):
>               st_mode = os.stat(iso).st_mode
> @@ -72,8 +78,7 @@ class TemplatesModel(object):
>                   raise InvalidParameter("KCHTMPL0003E", {'network': net_name,
>                                                           'template': name})
>           # Creates the template class with necessary information
> -        # Checkings will be done while creating this class, so any exception
> -        # will be raised here
> +
>           t = LibvirtVMTemplate(params, scan=True, conn=self.conn)
>
>           # Validate cpu info
> @@ -93,8 +98,6 @@ class TemplatesModel(object):
>           name = params['name']
>           try:
>               with self.objstore as session:
> -                if name in session.get_list('template'):
> -                    raise InvalidOperation("KCHTMPL0001E", {'name': name})
>                   session.store('template', name, t.info,
>                                 get_kimchi_version())
>           except InvalidOperation:




More information about the Kimchi-devel mailing list