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

Ramon Medeiros ramonn at linux.vnet.ibm.com
Thu Mar 31 13:47:54 UTC 2016


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')
+
+        # 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})
+
         # 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:
-- 
2.5.5




More information about the Kimchi-devel mailing list