Module: check_mk
Branch: master
Commit: 4ced77f86893ef547a02c3e9e092ef25527ed79a
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=4ced77f86893ef…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Mar 27 17:36:59 2019 +0100
Cleanup: Synchronous Rule.to_dict_config and Rule.parse_dict_rule()
Both function now deal with optional and mandatory attributes in the
same way.
These dicts will be used as rule representation in the configuration
files in the future. Therefore we need to skip the attributes that are at
their default values to reduce the footprint of individual rule entries
which can be many in larger installations.
Change-Id: I866c79af9a5abe8719ead5b98e19a08c3cb7f647
---
cmk/gui/watolib/rulesets.py | 26 +++++++++++++++----------
tests/unit/cmk/gui/watolib/test_gui_rulesets.py | 3 +++
2 files changed, 19 insertions(+), 10 deletions(-)
diff --git a/cmk/gui/watolib/rulesets.py b/cmk/gui/watolib/rulesets.py
index 1caa08f..264b05f 100644
--- a/cmk/gui/watolib/rulesets.py
+++ b/cmk/gui/watolib/rulesets.py
@@ -600,6 +600,8 @@ class Rule(object):
else:
raise NotImplementedError()
+ # TODO: Would make sense to implement independent parsing for the different
+ # ruleset types
def _parse_dict_rule(self, rule_config):
self.rule_options = rule_config.get("options", {})
@@ -612,9 +614,11 @@ class Rule(object):
else:
self.value = True
- conditions = rule_config.get("conditions", {})
+ conditions = rule_config["conditions"]
self.host_list = conditions.get("host_specs", [])
- self.item_list = conditions.get("service_specs")
+
+ if self.ruleset.item_type():
+ self.item_list = conditions.get("service_specs")
# Remove folder tag from tag list
tag_specs = conditions.get("host_tags", [])
@@ -705,20 +709,22 @@ class Rule(object):
return content
def to_dict_config(self):
- result = {"conditions": {}}
+ result = {
+ "conditions": {},
+ }
- result["options"] = self._rule_options_to_config()
+ rule_options = self._rule_options_to_config()
+ if rule_options:
+ result["options"] = rule_options
if self.ruleset.valuespec():
result["value"] = self.value
- else:
- if self.value:
- result["negate"] = False
- else:
- result["negate"] = True
+ elif self.value is False:
+ result["negate"] = True
result["conditions"]["host_specs"] = self.host_list
- result["conditions"]["host_tags"] = self.tag_specs
+ if self.tag_specs:
+ result["conditions"]["host_tags"] = self.tag_specs
if self.ruleset.item_type():
result["conditions"]["service_specs"] = self.item_list
diff --git a/tests/unit/cmk/gui/watolib/test_gui_rulesets.py
b/tests/unit/cmk/gui/watolib/test_gui_rulesets.py
index 8607401..37b42ef 100644
--- a/tests/unit/cmk/gui/watolib/test_gui_rulesets.py
+++ b/tests/unit/cmk/gui/watolib/test_gui_rulesets.py
@@ -249,6 +249,9 @@ def test_rule_from_config_dict(ruleset_name, rule_spec,
expected_attributes, rul
else:
assert rule.rule_options == {}
+ # test for synchronous to_dict on the way
+ assert rule.to_dict_config() == rule_spec
+
def test_rule_clone():
rule = _rule("clustered_services")