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

Jose Ricardo Ziviani joserz at linux.vnet.ibm.com
Thu Apr 7 19:07:55 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 | 42 +++++++++++++++++++++++++++++-------------
 2 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/i18n.py b/i18n.py
index 1061aeb..f10133e 100644
--- a/i18n.py
+++ b/i18n.py
@@ -157,6 +157,7 @@ messages = {
     "KCHTMPL0001E": _("Template %(name)s already exists"),
     "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"),
+    "KCHTMPL0005E": _("Template %(name)s does not exist."),
     "KCHTMPL0006E": _("Invalid parameter '%(param)s' specified for CDROM."),
     "KCHTMPL0007E": _("Network %(network)s specified for template %(template)s is not active"),
     "KCHTMPL0008E": _("Template name must be a string"),
@@ -182,6 +183,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 9294be2..1e6ad30 100644
--- a/model/templates.py
+++ b/model/templates.py
@@ -49,7 +49,7 @@ class TemplatesModel(object):
         self.objstore = kargs['objstore']
         self.conn = kargs['conn']
 
-    def create(self, params):
+    def _validate_template_params(self, params):
         name = params.get('name', '').strip()
         iso = params.get('cdrom')
         # check search permission
@@ -89,13 +89,18 @@ class TemplatesModel(object):
             if 'volume' in disk.keys():
                 self.template_volume_validate(volume, disk['pool'])
 
+        return t
+
+    def create(self, params):
+        template = self._validate_template_params(params)
+
         # Store template on objectstore
         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,
+                session.store('template', name, template.info,
                               get_kimchi_version())
         except InvalidOperation:
             raise
@@ -171,31 +176,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:
@@ -205,13 +209,25 @@ class TemplateModel(object):
                 raise InvalidParameter("KCHTMPL0003E", {'network': net_name,
                                                         'template': name})
 
-        self.delete(name)
+        template = self.templates._validate_template_params(edit_template)
+
         try:
-            ident = self.templates.create(new_t)
-        except:
-            ident = self.templates.create(old_t)
+            with self.objstore as session:
+                if name not in session.get_list('template'):
+                    raise InvalidOperation("KCHTMPL0005E", {'name': name})
+
+                if template.info['name'] != name:
+                    session.delete('template', name)
+
+                session.store('template', template.info['name'],
+                              template.info, get_kimchi_version())
+
+        except InvalidOperation:
             raise
-        return ident
+        except Exception, e:
+            raise OperationFailed('KCHTMPL0032E', {'err': e.message})
+
+        return template.info['name']
 
 
 def validate_memory(memory):
-- 
1.9.1




More information about the Kimchi-devel mailing list