Module: check_mk
Branch: master
Commit: cf2f3d124a72b6c7ba0b18cffddfebed081e56ce
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=cf2f3d124a72b6…
Author: Simon Betz <si(a)mathias-kettner.de>
Date: Mon Jun 12 09:21:09 2017 +0200
4782 Prevent deletion of parent host
Change-Id: I08dc31201d1f361344fd3fe3802c96aa7b191ccc
---
.werks/4782 | 13 +++++++++++++
web/htdocs/wato.py | 1 -
web/htdocs/watolib.py | 41 ++++++++++++++++++++++++++++++++++++-----
3 files changed, 49 insertions(+), 6 deletions(-)
diff --git a/.werks/4782 b/.werks/4782
new file mode 100644
index 0000000..37ffaaf
--- /dev/null
+++ b/.werks/4782
@@ -0,0 +1,13 @@
+Title: Prevent deletion of parent host
+Level: 1
+Component: wato
+Compatible: compat
+Edition: cre
+Version: 1.5.0i1
+Date: 1497251963
+Class: feature
+
+If you want to delete hosts which have parents, an error is
+raised and the deletion of these hosts won't be executed.
+Either you have to resolve the parent-child relationships
+or all related hosts are chosen.
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 95d112e..6e07a6e 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -1336,7 +1336,6 @@ def mode_edit_host(phase, new, is_cluster):
return action_edit_host(mode, hostname, is_cluster)
-
# Show outcome of host validation. Do not validate new hosts
errors = None
if new:
diff --git a/web/htdocs/watolib.py b/web/htdocs/watolib.py
index 4e0bdbef..1d1bc97 100644
--- a/web/htdocs/watolib.py
+++ b/web/htdocs/watolib.py
@@ -1801,8 +1801,15 @@ class CREFolder(BaseFolder):
self.need_permission("write")
self.need_unlocked_subfolders()
- # 2. Actual modification
+ # 2. check if hosts have parents
subfolder = self.subfolder(name)
+ hosts_with_children =
self._get_parents_of_hosts(subfolder.all_hosts_recursively().keys())
+ if hosts_with_children:
+ raise MKUserError("delete_host", _("You cannot delete these
hosts: %s") % \
+ ", ".join([_("%s is parent of %s.") %
(parent, ", ".join(children))
+ for parent, children in
sorted(hosts_with_children.items())]))
+
+ # 3. Actual modification
call_hook_folder_deleted(subfolder)
add_change("delete-folder", _("Deleted folder %s") %
subfolder.alias_path(),
obj=self,
@@ -1908,10 +1915,17 @@ class CREFolder(BaseFolder):
self.need_unlocked_hosts()
self.need_permission("write")
- # 2. Delete host specific files (caches, tempfiles, ...)
+ # 2. check if hosts have parents
+ hosts_with_children = self._get_parents_of_hosts(host_names)
+ if hosts_with_children:
+ raise MKUserError("delete_host", _("You cannot delete these
hosts: %s") % \
+ ", ".join([_("%s is parent of %s.") %
(parent, ", ".join(children))
+ for parent, children in
sorted(hosts_with_children.items())]))
+
+ # 3. Delete host specific files (caches, tempfiles, ...)
self._delete_host_files(host_names)
- # 3. Actual modification
+ # 4. Actual modification
for host_name in host_names:
host = self.hosts()[host_name]
del self._hosts[host_name]
@@ -1924,6 +1938,23 @@ class CREFolder(BaseFolder):
self.save_hosts()
+ def _get_parents_of_hosts(self, host_names):
+ # Note: Deletion of chosen hosts which are a parent
+ # is possible if and only if all children are chosen, too.
+ hosts_with_children = {}
+ for child_key, child in Folder.root_folder().all_hosts_recursively().items():
+ for host_name in host_names:
+ if host_name in child.host_names():
+ hosts_with_children.setdefault(host_name, [])
+ hosts_with_children[host_name].append(child_key)
+
+ result = {}
+ for parent, children in hosts_with_children.items():
+ if not set(children) < set(host_names):
+ result.setdefault(parent, children)
+ return result
+
+
# Group the given host names by their site and delete their files
def _delete_host_files(self, host_names):
hosts_by_site = {}
@@ -2209,9 +2240,9 @@ class SearchFolder(BaseFolder):
def delete_hosts(self, host_names):
auth_errors = []
- for folder, host_names in self._group_hostnames_by_folder(host_names):
+ for folder, these_host_names in self._group_hostnames_by_folder(host_names):
try:
- folder.delete_hosts(host_names)
+ folder.delete_hosts(these_host_names)
except MKAuthException, e:
auth_errors.append(_("<li>Cannot delete hosts in folder %s:
%s</li>") % (folder.alias_path(), e))
self._invalidate_search()