Module: check_mk
Branch: master
Commit: 2fafd0f4f638854c4dbcf5e6352eac37f8bd7f1b
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=2fafd0f4f63885…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Feb 21 09:36:28 2017 +0100
4390 FIX Virtual host trees are now identified by manually set IDs
In the past it was possible to delete virtual host trees by accident,
because the virtual host trees were identified by their number in the
list of trees.
The users were also referencing the trees by these IDs. When the trees
were re-sorted, the users suddenly had the wrong trees activated.
Now you have to provide a unique ID for each virtual host tree
Change-Id: I51886cf09ccaa421301d6a092678ce2ec11fd112
---
.werks/4390 | 17 +++++++
web/htdocs/sidebar.py | 14 +++++-
web/htdocs/valuespec.py | 2 +-
web/plugins/sidebar/shipped.py | 32 ++++++++-----
web/plugins/wato/check_mk_configuration.py | 73 ++++++++++++++++++++++--------
5 files changed, 104 insertions(+), 34 deletions(-)
diff --git a/.werks/4390 b/.werks/4390
new file mode 100644
index 0000000..71d4625
--- /dev/null
+++ b/.werks/4390
@@ -0,0 +1,17 @@
+Title: Virtual host trees are now identified by manually set IDs
+Level: 1
+Component: multisite
+Class: fix
+Compatible: compat
+State: unknown
+Version: 1.5.0i1
+Date: 1487666075
+
+In the past it was possible to delete virtual host trees by accident,
+because the virtual host trees were identified by their number in the
+list of trees.
+
+The users were also referencing the trees by these IDs. When the trees
+were re-sorted, the users suddenly had the wrong trees activated.
+
+Now you have to provide a unique ID for each virtual host tree
diff --git a/web/htdocs/sidebar.py b/web/htdocs/sidebar.py
index 57575f6..a81e33a 100644
--- a/web/htdocs/sidebar.py
+++ b/web/htdocs/sidebar.py
@@ -626,12 +626,22 @@ def ajax_switch_masterstate():
html.write(_("Command %s/%d not found") % (html.attrencode(column),
state))
def ajax_tag_tree():
- newconf = int(html.var("conf"))
+ new_tree = html.var("conf")
+
tree_conf = config.user.load_file("virtual_host_tree", {"tree":
0, "cwd": {}})
+
if type(tree_conf) == int:
tree_conf = {"cwd":{}} # convert from old style
- tree_conf["tree"] = newconf
+
+ trees = dict([ (tree["id"], tree) for tree in
+ wato.transform_virtual_host_trees(config.virtual_host_trees) ])
+
+ if new_tree not in trees:
+ raise MKUserError("conf", _("This virtual host tree does not
exist."))
+
+ tree_conf["tree"] = new_tree
config.user.save_file("virtual_host_tree", tree_conf)
+ html.write("OK")
def ajax_tag_tree_enter():
path = html.var("path") and html.var("path").split("|")
or []
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index df33621..6f38f2a 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -2118,7 +2118,7 @@ class DualListChoice(ListChoice):
self._autoheight = False
else:
self._rows = 5
- self._size = kwargs.get("size") # Total with in ex
+ self._size = kwargs.get("size") # Total width in ex
def render_input(self, varprefix, value):
diff --git a/web/plugins/sidebar/shipped.py b/web/plugins/sidebar/shipped.py
index 1b41fbb..d685795 100644
--- a/web/plugins/sidebar/shipped.py
+++ b/web/plugins/sidebar/shipped.py
@@ -1805,11 +1805,14 @@ sidebar_snapins["wiki"] = {
# '----------------------------------------------------------------------'
def compute_tag_tree(taglist):
- sites.live().set_prepend_site(True)
- query = "GET hosts\n" \
- "Columns: host_name filename state num_services_ok num_services_warn
num_services_crit num_services_unknown custom_variables"
- hosts = sites.live().query(query)
- sites.live().set_prepend_site(False)
+ try:
+ sites.live().set_prepend_site(True)
+ query = "GET hosts\n" \
+ "Columns: host_name filename state num_services_ok num_services_warn
num_services_crit num_services_unknown custom_variables"
+ hosts = sites.live().query(query)
+ finally:
+ sites.live().set_prepend_site(False)
+
hosts.sort()
def get_tag_group_value(groupentries, tags):
@@ -2077,7 +2080,12 @@ def render_tag_tree():
if type(tree_conf) == int:
tree_conf = {"tree": tree_conf, "cwd":{}} # convert from old
style
- choices = [ (str(i), v[0]) for i, v in enumerate(config.virtual_host_trees)]
+ trees = dict([ (tree["id"], tree) for tree in
+ wato.transform_virtual_host_trees(config.virtual_host_trees) ])
+
+ choices = sorted([ (tree["id"], tree["title"]) for tree in
trees.values() ],
+ key=lambda x: x[1])
+
html.begin_form("vtree")
# Give chance to change one level up, if we are in a subtree
@@ -2086,19 +2094,19 @@ def render_tag_tree():
upurl = "javascript:virtual_host_tree_enter('%s')" %
"|".join(cwd[:-1])
html.icon_button(upurl, _("Go up one tree level"), "back")
- html.select("vtree", choices, str(tree_conf["tree"]),
onchange='virtual_host_tree_changed(this)')
+ html.select("vtree", choices, "%s" % tree_conf["tree"],
onchange='virtual_host_tree_changed(this)')
html.br()
html.end_form()
html.final_javascript(virtual_host_tree_js)
try:
- title, taggroups = config.virtual_host_trees[tree_conf["tree"]]
- except IndexError:
+ tag_groups = trees[tree_conf["tree"]]["tag_groups"]
+ except KeyError:
# Fallback to first host tree in case the wanted does not exist (anymore)
- title, taggroups = config.virtual_host_trees[0]
+ tag_groups = trees[choices[0][0]]["tag_groups"]
- tree = compute_tag_tree(taggroups)
- render_tag_tree_level(taggroups, [], cwd, _("Virtual Host Tree"), tree)
+ tree = compute_tag_tree(tag_groups)
+ render_tag_tree_level(tag_groups, [], cwd, _("Virtual Host Tree"), tree)
sidebar_snapins["tag_tree"] = {
"title" : _("Virtual Host Tree"),
diff --git a/web/plugins/wato/check_mk_configuration.py
b/web/plugins/wato/check_mk_configuration.py
index 2e0322e..3d49ace 100644
--- a/web/plugins/wato/check_mk_configuration.py
+++ b/web/plugins/wato/check_mk_configuration.py
@@ -24,6 +24,8 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+import re
+
import cmk.paths
# .--Global Settings-----------------------------------------------------.
@@ -333,29 +335,62 @@ def virtual_host_tree_choices():
for l in range(1, 7)
]
+def transform_virtual_host_trees(trees):
+ def id_from_title(title):
+ return re.sub("[^-a-zA-Z0-9_]+", "", title.lower())
+
+ for index, tree in enumerate(trees):
+ if type(tree) == tuple:
+ trees[index] = {
+ "id" : id_from_title(tree[0]),
+ "title" : tree[0],
+ "tag_groups" : tree[1],
+ }
+
+ return sorted(trees, key = lambda x: x["title"])
+
+
+def validate_virtual_host_trees(value, varprefix):
+ tree_ids = set()
+ for tree in value:
+ if tree["id"] in tree_ids:
+ raise MKUserError(varprefix, _("The ID needs to be unique."))
+ tree_ids.add(tree["id"])
+
register_configvar(group,
"virtual_host_trees",
- ListOf(
- Tuple(
- elements = [
- TextUnicode(
- title = _("Title of the tree"),
- allow_empty = False,
- ),
- DualListChoice(
- allow_empty = False,
- custom_order = True,
- choices = virtual_host_tree_choices,
- rows = 10,
- )
- ]
+ Transform(
+ ListOf(
+ Dictionary(
+ elements = [
+ ("id", ID(
+ title = _("ID"),
+ allow_empty = False,
+ )),
+ ("title", TextUnicode(
+ title = _("Title of the tree"),
+ allow_empty = False,
+ )),
+ ("tag_groups", DualListChoice(
+ allow_empty = False,
+ custom_order = True,
+ choices = virtual_host_tree_choices,
+ rows = 10,
+ size = 80,
+ )),
+ ],
+ optional_keys = [],
+ ),
+ add_label = _("Create new virtual host tree configuration"),
+ title = _("Virtual Host Trees"),
+ help = _("Here you can define tree configurations for the snapin
<i>Virtual Host-Trees</i>. "
+ "These trees organize your hosts based on their values in
certain host tag groups. "
+ "Each host tag group you select will create one level in the
tree."),
+ validate = validate_virtual_host_trees,
+ movable = False,
),
- add_label = _("Create new virtual host tree configuration"),
- title = _("Virtual Host Trees"),
- help = _("Here you can define tree configurations for the snapin
<i>Virtual Host-Trees</i>. "
- "These trees organize your hosts based on their values in certain
host tag groups. "
- "Each host tag group you select will create one level in the
tree."),
+ forth = transform_virtual_host_trees,
),
domain = "multisite",
)