[Kimchi-devel] [PATCH] issue #66: Auto generate template name in backend when none is provided

Daniel H Barboza danielhb at linux.vnet.ibm.com
Mon Mar 3 17:44:21 UTC 2014


Reviewed-by: Daniel Barboza <danielhb at linux.vnet.ibm.com>

On 02/28/2014 12:25 PM, Aline Manera wrote:
> From: Aline Manera <alinefm at br.ibm.com>
>
> The only required parameter to create a template is the cdrom ISO image.
> The name must be create automatically on backend when no one is passed.
> Fix it.
>
> Signed-off-by: Aline Manera <alinefm at br.ibm.com>
> ---
>   src/kimchi/mockmodel.py               |    8 +++++++-
>   src/kimchi/model/templates.py         |    9 ++++++++-
>   ui/js/src/kimchi.template_add_main.js |   16 ----------------
>   3 files changed, 15 insertions(+), 18 deletions(-)
>
> diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
> index 50b5f0e..f99b4cd 100644
> --- a/src/kimchi/mockmodel.py
> +++ b/src/kimchi/mockmodel.py
> @@ -216,7 +216,13 @@ class MockModel(object):
>               raise NotFoundError("KCHTMPL0002E", {'name': name})
>
>       def templates_create(self, params):
> -        name = params['name']
> +        name = params.get('name', '').strip()
> +        if not name:
> +            iso = params['cdrom']
> +            iso_name = os.path.splitext(iso[iso.rfind('/') + 1:])[0]
> +            name = iso_name + str(int(time.time()*1000))
> +            params['name'] = name
> +
>           if name in self._mock_templates:
>               raise InvalidOperation("KCHTMPL0001E", {'name': name})
>
> diff --git a/src/kimchi/model/templates.py b/src/kimchi/model/templates.py
> index 7afb0e0..3062bd7 100644
> --- a/src/kimchi/model/templates.py
> +++ b/src/kimchi/model/templates.py
> @@ -19,6 +19,7 @@
>
>   import copy
>   import os
> +import time
>
>   import libvirt
>
> @@ -34,7 +35,13 @@ class TemplatesModel(object):
>           self.conn = kargs['conn']
>
>       def create(self, params):
> -        name = params['name']
> +        name = params.get('name', '').strip()
> +        if not name:
> +            iso = params['cdrom']
> +            iso_name = os.path.splitext(iso[iso.rfind('/') + 1:])[0]
> +            name = iso_name + str(int(time.time()*1000))
> +            params['name'] = name
> +
>           conn = self.conn.get()
>
>           pool_uri = params.get(u'storagepool', '')
> diff --git a/ui/js/src/kimchi.template_add_main.js b/ui/js/src/kimchi.template_add_main.js
> index 31326df..098a706 100644
> --- a/ui/js/src/kimchi.template_add_main.js
> +++ b/ui/js/src/kimchi.template_add_main.js
> @@ -219,7 +219,6 @@ kimchi.template_add_main = function() {
>               return;
>           }
>           var data = {
> -            "name" : kimchi.get_iso_name(isoFile),
>               "cdrom" : isoFile
>           };
>           addTemplate(data);
> @@ -331,7 +330,6 @@ kimchi.template_add_main = function() {
>               return;
>           }
>           var data = {
> -            "name" : kimchi.get_iso_name(isoUrl),
>               "cdrom" : isoUrl
>           };
>           addTemplate(data);
> @@ -355,7 +353,6 @@ kimchi.template_add_main = function() {
>               var successNum = 0;
>               var addTemplate = function(isoInfo) {
>                   var data = {
> -                    "name" : kimchi.get_iso_name(isoInfo.name),
>                       "os_distro" : isoInfo.os_distro,
>                       "os_version" : isoInfo.os_version,
>                       "cdrom" : isoInfo.path
> @@ -419,16 +416,3 @@ kimchi.is_iso_file = function(isoFile) {
>       }
>       return false;
>   };
> -
> -kimchi.get_iso_name = function(isoPath) {
> -    if ((isoPath.charAt(isoPath.length - 1) == "/") == true) {
> -        isoPath = isoPath.substring(0, isoPath.length - 1)
> -    }
> -    if (/.iso$/.test(isoPath)) {
> -        return isoPath.substring(isoPath.lastIndexOf("/") + 1,
> -                isoPath.lastIndexOf(".")) + new Date().getTime();
> -    } else {
> -        return isoPath.substring(isoPath.lastIndexOf("/") + 1) +
> -        new Date().getTime();
> -    }
> -};




More information about the Kimchi-devel mailing list