[Kimchi-devel] [PATCH v3 2/4] Add Name Existence Check for Debug Report when Create

Hongliang Wang hlwang at linux.vnet.ibm.com
Fri Jun 13 09:16:55 UTC 2014


Prevent user to overwrite an existing debug report with same name.

Signed-off-by: Hongliang Wang <hlwang at linux.vnet.ibm.com>
---
 src/kimchi/control/base.py       | 6 +++---
 src/kimchi/i18n.py               | 1 +
 src/kimchi/model/debugreports.py | 5 ++++-
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/kimchi/control/base.py b/src/kimchi/control/base.py
index a110ac2..bca6186 100644
--- a/src/kimchi/control/base.py
+++ b/src/kimchi/control/base.py
@@ -27,7 +27,7 @@ from kimchi.control.utils import parse_request, validate_method
 from kimchi.control.utils import validate_params
 from kimchi.exception import InvalidOperation, InvalidParameter
 from kimchi.exception import KimchiException
-from kimchi.exception import MissingParameter, NotFoundError,  OperationFailed
+from kimchi.exception import MissingParameter, NotFoundError,  OperationFailed, ResourceAlreadyExists
 
 
 class Resource(object):
@@ -84,6 +84,8 @@ class Resource(object):
                 raise cherrypy.HTTPError(400, e.message)
             except InvalidOperation, e:
                 raise cherrypy.HTTPError(400, e.message)
+            except ResourceAlreadyExists, e:
+                raise cherrypy.HTTPError(409, e.message)
             except OperationFailed, e:
                 raise cherrypy.HTTPError(500, e.message)
             except NotFoundError, e:
@@ -276,8 +278,6 @@ class Collection(object):
             raise cherrypy.HTTPError(400, e.message)
         except NotFoundError, e:
             raise cherrypy.HTTPError(404, e.message)
-        except ResourceAlreadyExists, e:
-            raise cherrypy.HTTPError(409, e.message)
         except OperationFailed, e:
             raise cherrypy.HTTPError(500, e.message)
         except KimchiException, e:
diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
index 452ede2..90ecc17 100644
--- a/src/kimchi/i18n.py
+++ b/src/kimchi/i18n.py
@@ -207,6 +207,7 @@ messages = {
     "KCHDR0005E": _("Unable to generate debug report %(name)s. Details: %(err)s"),
     "KCHDR0006E": _("You should give a name for the debug file report."),
     "KCHDR0007E": _("Name should be a string. Only letters, digits and hyphen ('-') are allowed."),
+    "KCHDR0008E": _("The debug report with specified name \"%(name)s\" already exists. Please use another one."),
 
     "KCHSR0001E": _("Storage server %(server)s was not used by Kimchi"),
 
diff --git a/src/kimchi/model/debugreports.py b/src/kimchi/model/debugreports.py
index cd31b31..7994fec 100644
--- a/src/kimchi/model/debugreports.py
+++ b/src/kimchi/model/debugreports.py
@@ -26,7 +26,7 @@ import subprocess
 import time
 
 from kimchi import config
-from kimchi.exception import KimchiException, NotFoundError, OperationFailed
+from kimchi.exception import KimchiException, NotFoundError, OperationFailed, ResourceAlreadyExists
 from kimchi.model.tasks import TaskModel
 from kimchi.utils import add_task, kimchi_log
 from kimchi.utils import run_command
@@ -42,6 +42,9 @@ class DebugReportsModel(object):
         # Generate a name with time and millisec precision, if necessary
         if ident is None or ident == "":
             ident = 'report-' + str(int(time.time() * 1000))
+        else:
+            if ident in self.get_list():
+                raise ResourceAlreadyExists("KCHDR0008E", {"name": ident})
         taskid = self._gen_debugreport_file(ident)
         return self.task.lookup(taskid)
 
-- 
1.8.1.4




More information about the Kimchi-devel mailing list