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

Ramon Medeiros ramonn at linux.vnet.ibm.com
Wed Dec 23 15:12:04 UTC 2015


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 | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/model/templates.py b/model/templates.py
index c9b11c3..6cef4e0 100644
--- a/model/templates.py
+++ b/model/templates.py
@@ -78,8 +78,11 @@ 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
+        # 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': params["name"]})
+
         t = LibvirtVMTemplate(params, scan=True, conn=self.conn)
 
         # Validate max memory
@@ -98,8 +101,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:
-- 
2.1.0




More information about the Kimchi-devel mailing list