[Kimchi-devel] [PATCH v4] [Kimchi] Issue #920: Template is removed if an ISO doesn't exist

Jose Ricardo Ziviani joserz at linux.vnet.ibm.com
Tue Apr 19 04:05:36 UTC 2016


 - This commit changes the way the template update works. Now, it
   really updates the existing template instead of creating a new
   one (and deleting the old one)

Signed-off-by: Jose Ricardo Ziviani <joserz at linux.vnet.ibm.com>
---
 i18n.py                |  2 ++
 model/templates.py     | 35 ++++++++++++++++++++++-------------
 tests/test_template.py |  6 +++++-
 3 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/i18n.py b/i18n.py
index 7cce796..14ae166 100644
--- a/i18n.py
+++ b/i18n.py
@@ -155,6 +155,7 @@ messages = {
     "KCHVMIF0011E": _("Cannot change MAC address of a running virtual machine"),
 
     "KCHTMPL0001E": _("Template %(name)s already exists"),
+    "KCHTMPL0002E": _("Source media %(path)s not found"),
     "KCHTMPL0003E": _("Network '%(network)s' specified for template %(template)s does not exist"),
     "KCHTMPL0004E": _("Storage pool %(pool)s specified for template %(template)s does not exist"),
     "KCHTMPL0006E": _("Invalid parameter '%(param)s' specified for CDROM."),
@@ -181,6 +182,7 @@ messages = {
     "KCHTMPL0029E": _("Disk format must be 'raw', for logical, iscsi, and scsi pools."),
     "KCHTMPL0030E": _("Memory expects an object with one or both parameters: 'current' and 'maxmemory'"),
     "KCHTMPL0031E": _("Memory value (%(mem)sMiB) must be equal or lesser than maximum memory value (%(maxmem)sMiB)"),
+    "KCHTMPL0032E": _("Unable to update template due error: %(err)s"),
 
     "KCHPOOL0001E": _("Storage pool %(name)s already exists"),
     "KCHPOOL0002E": _("Storage pool %(name)s does not exist"),
diff --git a/model/templates.py b/model/templates.py
index a02099c..431cae0 100644
--- a/model/templates.py
+++ b/model/templates.py
@@ -72,8 +72,7 @@ class TemplatesModel(object):
 
         # image does not exists: raise error
         elif not os.path.exists(path):
-            raise InvalidParameter("Unable to find file %(path)s" %
-                                   {"path": path})
+            raise InvalidParameter("KCHTMPL0002E", {'path': path})
 
         # create magic object to discover file type
         file_type = magic.open(magic.MAGIC_NONE)
@@ -204,31 +203,30 @@ class TemplateModel(object):
             raise OperationFailed('KCHTMPL0021E', {'err': e.message})
 
     def update(self, name, params):
-        old_t = self.lookup(name)
-        new_t = copy.copy(old_t)
+        edit_template = self.lookup(name)
 
         # Merge graphics settings
         graph_args = params.get('graphics')
         if graph_args:
-            graphics = dict(new_t['graphics'])
+            graphics = dict(edit_template['graphics'])
             graphics.update(graph_args)
             params['graphics'] = graphics
 
         # Merge cpu_info settings
         new_cpu_info = params.get('cpu_info')
         if new_cpu_info:
-            cpu_info = dict(new_t['cpu_info'])
+            cpu_info = dict(edit_template['cpu_info'])
             cpu_info.update(new_cpu_info)
             params['cpu_info'] = cpu_info
 
         # Fix memory values, because method update does not work recursively
         new_mem = params.get('memory')
         if new_mem is not None:
-            params['memory'] = copy.copy(old_t.get('memory'))
+            params['memory'] = copy.copy(edit_template.get('memory'))
             params['memory'].update(new_mem)
             validate_memory(params['memory'])
 
-        new_t.update(params)
+        edit_template.update(params)
 
         for net_name in params.get(u'networks', []):
             try:
@@ -238,13 +236,24 @@ class TemplateModel(object):
                 raise InvalidParameter("KCHTMPL0003E", {'network': net_name,
                                                         'template': name})
 
-        self.delete(name)
         try:
-            ident = self.templates.save_template(new_t)
-        except:
-            ident = self.templates.save_template(old_t)
+            # make sure the new template will be created
+            t = LibvirtVMTemplate(edit_template, scan=True, conn=self.conn)
+            t.cpuinfo_validate()
+            t._validate_memory()
+
+            # remove the current one
+            self.delete(name)
+
+            # save the template
+            return self.templates.save_template(edit_template)
+
+        except InvalidOperation:
             raise
-        return ident
+        except Exception, e:
+            raise OperationFailed('KCHTMPL0032E', {'err': e.message})
+
+        return params['name']
 
 
 def validate_memory(memory):
diff --git a/tests/test_template.py b/tests/test_template.py
index 158bbeb..f0ef030 100644
--- a/tests/test_template.py
+++ b/tests/test_template.py
@@ -256,7 +256,11 @@ class TemplateTests(unittest.TestCase):
         self.assertEquals(1024, update_tmpl['memory']['maxmemory'])
 
         # Update cdrom
-        cdrom_data = {'cdrom': '/tmp/mock2.iso'}
+        cdrom_data = {'cdrom': 'inexistent.iso'}
+        resp = self.request(new_tmpl_uri, json.dumps(cdrom_data), 'PUT')
+        self.assertEquals(400, resp.status)
+
+        cdrom_data = {'cdrom': '/tmp/existent.iso'}
         resp = self.request(new_tmpl_uri, json.dumps(cdrom_data), 'PUT')
         self.assertEquals(200, resp.status)
         update_tmpl = json.loads(resp.read())
-- 
1.9.1




More information about the Kimchi-devel mailing list