Module: check_mk
Branch: master
Commit: 8713f1d0c2aebcad9e378787e7e63331b87c8be8
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=8713f1d0c2aebc…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Dec 13 08:59:29 2018 +0100
Host attributes: Extracted some host attribute logic from configure_attributes()
* Moved host attribute topic listing and sorting to dedicated function
and added a test for the topics and their sorting
* Moved host attribute list, filtering and sorting to dedicated function
and added a test for the attributes of a topic and their sorting
CMK-1421
Change-Id: Iefe9890073cb37a4f25933aec05dfbad3c9888ac
---
cmk/gui/plugins/wato/utils/__init__.py | 67 ++++++++------------------------
cmk/gui/watolib.py | 36 +++++++++++++++++
tests/unit/cmk/gui/test_watolib.py | 71 ++++++++++++++++++++++++++++++++++
3 files changed, 124 insertions(+), 50 deletions(-)
diff --git a/cmk/gui/plugins/wato/utils/__init__.py
b/cmk/gui/plugins/wato/utils/__init__.py
index 5dfeab2..52325f2 100644
--- a/cmk/gui/plugins/wato/utils/__init__.py
+++ b/cmk/gui/plugins/wato/utils/__init__.py
@@ -1568,65 +1568,33 @@ def configure_attributes(new,
if without_attributes is None:
without_attributes = []
- # show attributes grouped by topics, in order of their
- # appearance. If only one topic exists, do not show topics
- # Make sure, that the topics "Basic settings" and host tags
- # are always show first.
- # TODO: Clean this up! Implement some explicit sorting
- topics = [None]
- if config.host_tag_groups():
- topics.append(_("Address"))
- topics.append(_("Data sources"))
- topics.append(_("Host tags"))
-
- # The remaining topics are shown in the order of the
- # appearance of the attribute declarations:
- for attr, topic in watolib.all_host_attributes():
- if topic not in topics and attr.is_visible(for_what):
- topics.append(topic)
-
# Collect dependency mapping for attributes (attributes that are only
# visible, if certain host tags are set).
dependency_mapping_tags = {}
dependency_mapping_roles = {}
inherited_tags = {}
- # Hack to sort the address family host tag attribute above the IPv4/v6 addresses
- # TODO: Clean this up by implementing some sort of explicit sorting
- def sort_host_attributes(a, b):
- if a[0].name() == "tag_address_family":
- return -1
- return 0
-
volatile_topics = []
hide_attributes = []
- for topic in topics:
+ for topic, title in watolib.get_sorted_host_attribute_topics(for_what):
topic_is_volatile = True # assume topic is sometimes hidden due to dependencies
- if len(topics) > 1:
- if topic is None:
- title = _("Basic settings")
- else:
- title = _u(topic)
-
- if topic == _("Host tags"):
- topic_id = "wato_host_tags"
- elif topic == _("Address"):
- topic_id = "address"
- elif topic == _("Data sources"):
- topic_id = "data_sources"
- else:
- topic_id = None
- forms.header(
- title,
- isopen=topic in [None, _("Address"), _("Data
sources")],
- table_id=topic_id,
- )
+ if topic == _("Host tags"):
+ topic_id = "wato_host_tags"
+ elif topic == _("Address"):
+ topic_id = "address"
+ elif topic == _("Data sources"):
+ topic_id = "data_sources"
+ else:
+ topic_id = None
- for attr, atopic in sorted(watolib.all_host_attributes(),
cmp=sort_host_attributes):
- if atopic != topic:
- continue
+ forms.header(
+ title,
+ isopen=topic in [None, _("Address"), _("Data sources")],
+ table_id=topic_id,
+ )
+ for attr in watolib.get_sorted_host_attributes_by_topic(topic):
attrname = attr.name()
if attrname in without_attributes:
continue # e.g. needed to skip ipaddress in CSV-Import
@@ -1854,9 +1822,8 @@ def configure_attributes(new,
html.write_text(explanation)
html.close_div()
- if len(topics) > 1:
- if topic_is_volatile:
- volatile_topics.append((topic or _("Basic
settings")).encode('utf-8'))
+ if topic_is_volatile:
+ volatile_topics.append((topic or _("Basic
settings")).encode('utf-8'))
forms.end()
# Provide Javascript world with the tag dependency information
diff --git a/cmk/gui/watolib.py b/cmk/gui/watolib.py
index 7ea6af7..1c0627e 100644
--- a/cmk/gui/watolib.py
+++ b/cmk/gui/watolib.py
@@ -3896,6 +3896,42 @@ def collect_attributes(for_what, do_validate=True,
varprefix=""):
return host
+def get_sorted_host_attribute_topics(for_what):
+ # show attributes grouped by topics, in order of their
+ # appearance. If only one topic exists, do not show topics
+ # Make sure, that the topics "Basic settings" and host tags
+ # are always show first.
+ # TODO: Clean this up! Implement some explicit sorting
+ topics = [None]
+ if config.host_tag_groups():
+ topics.append(_("Address"))
+ topics.append(_("Data sources"))
+ topics.append(_("Host tags"))
+
+ # The remaining topics are shown in the order of the
+ # appearance of the attribute declarations:
+ for attr, topic in all_host_attributes():
+ if topic not in topics and attr.is_visible(for_what):
+ topics.append(topic)
+
+ return [(t, _("Basic settings") if t is None else _u(t)) for t in topics]
+
+
+def get_sorted_host_attributes_by_topic(topic):
+ # Hack to sort the address family host tag attribute above the IPv4/v6 addresses
+ # TODO: Clean this up by implementing some sort of explicit sorting
+ def sort_host_attributes(a, b):
+ if a[0].name() == "tag_address_family":
+ return -1
+ return 0
+
+ attributes = []
+ for attr, atopic in sorted(all_host_attributes(), cmp=sort_host_attributes):
+ if atopic == topic:
+ attributes.append(attr)
+ return attributes
+
+
#.
# Persistenz: Speicherung der Werte
# - WATO speichert seine Variablen für main.mk in conf.d/wato/global.mk
diff --git a/tests/unit/cmk/gui/test_watolib.py b/tests/unit/cmk/gui/test_watolib.py
index e44a9e9..2528956 100644
--- a/tests/unit/cmk/gui/test_watolib.py
+++ b/tests/unit/cmk/gui/test_watolib.py
@@ -1664,3 +1664,74 @@ def test_register_check_parameters(monkeypatch):
rulespec_names = [r.name for r in
watolib.g_rulespecs.get_by_group("static/netblabla")]
assert "static_checks:check_group_name" in rulespec_names
assert len(rulespec_names) == 1
+
+
+(a)pytest.mark.parametrize("for_what"what", [
+ "host",
+ "cluster",
+ "folder",
+ "host_search",
+ "bulk",
+])
+def test_host_attribute_topics(for_what):
+ expected = [
+ (None, u"Basic settings"),
+ (u'Address', u'Address'),
+ (u'Data sources', u'Data sources'),
+ (u'Host tags', u'Host tags'),
+ (u'Management Board', u'Management Board'),
+ ]
+
+ if for_what == "folder":
+ expected = [
+ (None, u"Basic settings"),
+ (u'Address', u'Address'),
+ (u'Data sources', u'Data sources'),
+ (u'Host tags', u'Host tags'),
+ (u'Network Scan', u'Network Scan'),
+ (u'Management Board', u'Management Board'),
+ ]
+
+ assert watolib.get_sorted_host_attribute_topics(for_what=for_what) == expected
+
+
+(a)pytest.mark.parametrize("for_what"what", [
+ "host",
+ "cluster",
+ "folder",
+ "host_search",
+ "bulk",
+])
+def test_host_attributes(for_what):
+ topics = {
+ None: [
+ 'contactgroups',
+ 'alias',
+ 'snmp_community',
+ 'parents',
+ 'site',
+ 'locked_by',
+ 'locked_attributes',
+ ],
+ "Address": [
+ 'ipaddress',
+ 'ipv6address',
+ 'additional_ipv4addresses',
+ 'additional_ipv6addresses',
+ ],
+ "Data sources": [],
+ "Management Board": [
+ 'management_address',
+ 'management_protocol',
+ 'management_snmp_community',
+ 'management_ipmi_credentials',
+ ],
+ "Network Scan": [
+ 'network_scan',
+ 'network_scan_result',
+ ],
+ }
+ for topic, _title in watolib.get_sorted_host_attribute_topics(for_what):
+ names = [a.name() for a in watolib.get_sorted_host_attributes_by_topic(topic)]
+ assert names == topics.get(topic,
+ []), "Expected attributes not specified for topic
%r" % topic