Module: check_mk
Branch: master
Commit: 646f42798d4e852e84e68d121ea6752f1c2c0294
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=646f42798d4e85…
Author: Andreas Umbreit <au(a)mathias-kettner.de>
Date: Thu May 17 10:07:09 2018 +0200
CMK-538: Support multiple folders in Agent Bakery Host Selection
- Refactor FolderChoice factory function from wato.py to real valuespec
in valuespec.py
- Create/Adapt functions that provide a list of multiple folders
including a convenient presentation of their full paths, create a
FullPathFolderChoice valuespec upon them
- Adapt selection for Agent Bakery Host Selection rule and wrap it with
a Transform-Valuespec for proper porting of old configs
Change-Id: Ibb6fa6fbc55260a5b1dff2f2688ff34d6c1b49ac
---
web/htdocs/valuespec.py | 14 ++++++
web/htdocs/wato.py | 124 +++++++++++++++++++++++++++++++-----------------
web/htdocs/watolib.py | 22 ++++++---
3 files changed, 111 insertions(+), 49 deletions(-)
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index 12d2c4e..720fcaa 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -1709,6 +1709,20 @@ class DropdownChoice(ValueSpec):
return True
+class FolderChoice(DropdownChoice):
+ def __init__(self, **kwargs):
+ import watolib
+ kwargs["choices"] = watolib.Folder.folder_choices
+ kwargs.setdefault("title", _("Folder"))
+ DropdownChoice.__init__(self, **kwargs)
+
+class FullPathFolderChoice(DropdownChoice):
+ def __init__(self, **kwargs):
+ import watolib
+ kwargs["choices"] = watolib.Folder.folder_choices_fulltitle
+ kwargs.setdefault("title", _("Folder"))
+ DropdownChoice.__init__(self, **kwargs)
+
# Special conveniance variant for monitoring states
# TODO: Rename to ServiceState() or something like this
class MonitoringState(DropdownChoice):
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 5c78e66..1254259 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -7907,13 +7907,6 @@ class CheckTypeGroupSelection(ElementSelection):
return "<tt>%s</tt>" % value
-
-def FolderChoice(**kwargs):
- kwargs["choices"] = lambda: watolib.Folder.folder_choices()
- kwargs.setdefault("title", _("Folder"))
- return DropdownChoice(**kwargs)
-
-
def vs_notification_bulkby():
return ListChoice(
title = _("Create separate notification bulks based on"),
@@ -8185,54 +8178,99 @@ def vs_notification_rule(userid = None):
)
-def simple_host_rule_match_conditions():
- return [
- ( "match_site",
- DualListChoice(
- title = _("Match site"),
- help = _("This condition makes the rule match only hosts of "
- "the selected sites."),
- choices = config.site_attribute_choices,
- ),
+def site_rule_match_condition():
+ return (
+ "match_site",
+ DualListChoice(
+ title = _("Match site"),
+ help = _("This condition makes the rule match only hosts of "
+ "the selected sites."),
+ choices = config.site_attribute_choices,
),
- ( "match_folder",
- FolderChoice(
- title = _("Match folder"),
- help = _("This condition makes the rule match only hosts that are
managed "
- "via WATO and that are contained in this folder - either
directly "
- "or in one of its subfolders."),
- ),
+ )
+
+
+def single_folder_rule_match_condition():
+ return (
+ "match_folder",
+ FolderChoice(
+ title = _("Match folder"),
+ help = _("This condition makes the rule match only hosts that are
managed "
+ "via WATO and that are contained in this folder - either
directly "
+ "or in one of its subfolders."),
+ ),
+ )
+
+
+def multi_folder_rule_match_condition():
+ return (
+ "match_folders",
+ ListOf(
+ FullPathFolderChoice(
+ title = _("Folder"),
+ help = _("This condition makes the rule match only hosts that are
managed "
+ "via WATO and that are contained in this folder - either
directly "
+ "or in one of its subfolders."),
+ ),
+ add_label = _("Add additional folder"),
+ title = _("Match folders"),
+ movable = False
),
+ )
+
+
+def common_host_rule_match_conditions():
+ return [
( "match_hosttags",
- watolib.HostTagCondition(
- title = _("Match Host Tags"))
+ watolib.HostTagCondition(
+ title = _("Match Host Tags"))
),
( "match_hostgroups",
- userdb.GroupChoice("host",
- title = _("Match Host Groups"),
- help = _("The host must be in one of the selected host groups"),
- allow_empty = False,
- )
+ userdb.GroupChoice("host",
+ title = _("Match Host Groups"),
+ help = _("The host must be in one of the selected host groups"),
+ allow_empty = False,
+ )
),
( "match_hosts",
- ListOfStrings(
- title = _("Match only the following hosts"),
- size = 24,
- orientation = "horizontal",
- allow_empty = False,
- empty_text = _("Please specify at least one host. Disable the option
if you want to allow all hosts."),
- )
+ ListOfStrings(
+ title = _("Match only the following hosts"),
+ size = 24,
+ orientation = "horizontal",
+ allow_empty = False,
+ empty_text = _("Please specify at least one host. Disable the option if
you want to allow all hosts."),
+ )
),
( "match_exclude_hosts",
- ListOfStrings(
- title = _("Exclude the following hosts"),
- size = 24,
- orientation = "horizontal",
- )
- ),
+ ListOfStrings(
+ title = _("Exclude the following hosts"),
+ size = 24,
+ orientation = "horizontal",
+ )
+ )
]
+def simple_host_rule_match_conditions():
+ return [
+ site_rule_match_condition(),
+ single_folder_rule_match_condition()
+ ] + common_host_rule_match_conditions()
+
+
+def multifolder_host_rule_match_conditions():
+ return [
+ site_rule_match_condition(),
+ multi_folder_rule_match_condition()
+ ] + common_host_rule_match_conditions()
+
+
+def transform_simple_to_multi_host_rule_match_conditions(value):
+ if value and "match_folder" in value:
+ value["match_folders"] = [value.pop("match_folder")]
+ return value
+
+
def generic_rule_match_conditions():
return simple_host_rule_match_conditions() + [
( "match_servicegroups",
diff --git a/web/htdocs/watolib.py b/web/htdocs/watolib.py
index 655cdc9..a570767 100644
--- a/web/htdocs/watolib.py
+++ b/web/htdocs/watolib.py
@@ -1130,6 +1130,11 @@ class CREFolder(BaseFolder):
@staticmethod
+ def folder_choices_fulltitle():
+ return Folder.root_folder().recursive_subfolder_choices(current_depth=0,
pretty=False)
+
+
+ @staticmethod
def folder(folder_path):
if folder_path in Folder.all_folders():
return Folder.all_folders()[folder_path]
@@ -1811,15 +1816,20 @@ class CREFolder(BaseFolder):
return choices
- def recursive_subfolder_choices(self, current_depth=0):
- if current_depth:
- title_prefix = (u"\u00a0" * 6 * current_depth) +
u"\u2514\u2500 "
+ def recursive_subfolder_choices(self, current_depth=0, pretty=True):
+ if pretty:
+ if current_depth:
+ title_prefix = (u"\u00a0" * 6 * current_depth) +
u"\u2514\u2500 "
+ else:
+ title_prefix = ""
+ title = HTML(title_prefix + html.attrencode(self.title()))
else:
- title_prefix = ""
- sel = [ (self.path(), HTML(title_prefix + html.attrencode(self.title()))) ]
+ title =
HTML(html.attrencode("/".join(self.title_path_without_root())))
+
+ sel = [(self.path(), title)]
for subfolder in self.visible_subfolders_sorted_by_title():
- sel += subfolder.recursive_subfolder_choices(current_depth + 1)
+ sel += subfolder.recursive_subfolder_choices(current_depth + 1, pretty)
return sel