[Kimchi-devel] [PATCH 2/4] Add Name Existence Check for Debug Report when Create/Rename
Aline Manera
alinefm at linux.vnet.ibm.com
Thu Jun 12 18:03:31 UTC 2014
On 06/12/2014 04:07 AM, Hongliang Wang wrote:
> Prevent user overwrite an existing debug report with same name.
>
> Signed-off-by: Hongliang Wang <hlwang at linux.vnet.ibm.com>
> ---
> src/kimchi/i18n.py | 1 +
> src/kimchi/model/debugreports.py | 12 +++++++++++-
> 2 files changed, 12 insertions(+), 1 deletion(-)
>
> 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..3aa0fdc 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:
> + self._check_existence(ident)
> +
> taskid = self._gen_debugreport_file(ident)
> return self.task.lookup(taskid)
>
> @@ -54,6 +57,13 @@ class DebugReportsModel(object):
>
> return name_lists
> + def _check_existence(self, name):
> + path = config.get_debugreports_path()
> + file_pattern = os.path.join(path, name + '.*')
> + exists = [f for f in glob.glob(file_pattern) if os.path.isfile(f)]
> + if exists:
> + raise ResourceAlreadyExists("KCHDR0008E", {"name": name})
> +
You can do:
if name in self.get_list():
raise InvalidParameter()
> def _gen_debugreport_file(self, name):
> gen_cmd = self.get_system_report_tool()
>
More information about the Kimchi-devel
mailing list