Module: check_mk
Branch: master
Commit: 9b786d12f97d057f04b13af92502d4b7679560b4
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=9b786d12f97d05…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Sat Mar 30 22:39:05 2019 +0100
Rewrite affected rule conditions when editing predefined condition
The rules always copy the conditions of the predefined condition
for their own. In the moment the conditions of a predefined condition
are changed all rules refering to it need to be rewritten to adapt
the changes.
Change-Id: Ibc196041c5236005a666ece488069518860de6f0
---
cmk/gui/plugins/wato/utils/simple_modes.py | 5 ++++-
cmk/gui/wato/pages/predefined_conditions.py | 35 ++++++++++++++++++++++++++++-
cmk/gui/wato/pages/rulesets.py | 5 +----
cmk/gui/watolib/rulesets.py | 6 +++++
4 files changed, 45 insertions(+), 6 deletions(-)
diff --git a/cmk/gui/plugins/wato/utils/simple_modes.py
b/cmk/gui/plugins/wato/utils/simple_modes.py
index 007e6af..6ad1f4b 100644
--- a/cmk/gui/plugins/wato/utils/simple_modes.py
+++ b/cmk/gui/plugins/wato/utils/simple_modes.py
@@ -411,10 +411,13 @@ class SimpleEditMode(SimpleWatoModeBase):
"edit", self._entry,
_("Edited the %s '%s'") %
(self._mode_type.name_singular(), self._ident))
- self._store.save(entries)
+ self._save(entries)
return self._mode_type.list_mode_name()
+ def _save(self, entries):
+ self._store.save(entries)
+
def page(self):
html.begin_form("edit", method="POST")
html.prevent_password_auto_completion()
diff --git a/cmk/gui/wato/pages/predefined_conditions.py
b/cmk/gui/wato/pages/predefined_conditions.py
index 6f812cf..ffc39a5 100644
--- a/cmk/gui/wato/pages/predefined_conditions.py
+++ b/cmk/gui/wato/pages/predefined_conditions.py
@@ -40,7 +40,8 @@ from cmk.gui.valuespec import (
from cmk.gui.plugins.wato.check_mk_configuration import RulespecGroupUserInterface
from cmk.gui.wato.pages.rulesets import VSExplicitConditions, RuleConditions
-from cmk.gui.watolib.rulesets import AllRulesets, SearchedRulesets
+from cmk.gui.watolib.rulesets import AllRulesets, SearchedRulesets, FolderRulesets
+from cmk.gui.watolib.hosts_and_folders import Folder
from cmk.gui.watolib.rulespecs import ServiceRulespec
from cmk.gui.watolib.groups import load_contact_group_information
from cmk.gui.watolib.predefined_conditions import PredefinedConditionStore
@@ -234,6 +235,38 @@ class ModeEditPredefinedCondition(SimpleEditMode):
)),
]
+ def _save(self, entries):
+ old_path =
self._store.load_for_reading()[self._ident]["conditions"]["folder_path"]
+ conditions = RuleConditions(**entries[self._ident]["conditions"])
+
+ super(ModeEditPredefinedCondition, self)._save(entries)
+
+ if old_path != conditions.folder_path:
+ # TODO: Move rules to other folder (See EditRuleMode.action)
+ pass
+
+ self._rewrite_rules_for(conditions)
+
+ def _rewrite_rules_for(self, conditions):
+ # type: (RuleConditions) -> None
+ """Apply changed predefined condition to rules
+
+ After updating a predefined condition it is necessary to rewrite the
+ rules.mk the predefined condition refers to. Rules in this file may refer to
+ the changed predefined condition. Since the conditions are only applied to the
+ rules while saving them this step is needed.
+ """
+ folder = Folder(conditions.folder_path)
+ rulesets = FolderRulesets(folder)
+ rulesets.load()
+
+ for ruleset in rulesets.get_rulesets().values():
+ for rule in ruleset.get_folder_rules(folder):
+ if rule.predefined_condition_id() == self._ident:
+ rule.update_conditions(conditions)
+
+ rulesets.save()
+
def _contact_group_choices(self, only_own=False):
contact_groups = load_contact_group_information()
diff --git a/cmk/gui/wato/pages/rulesets.py b/cmk/gui/wato/pages/rulesets.py
index 114b8bf..b7d3293 100644
--- a/cmk/gui/wato/pages/rulesets.py
+++ b/cmk/gui/wato/pages/rulesets.py
@@ -1200,10 +1200,7 @@ class EditRuleMode(WatoMode):
self._rule.rule_options["predefined_condition_id"] = condition_id
# CONDITION
- conditions = self._get_rule_conditions_from_vars()
- self._rule.tag_specs = conditions.host_tags
- self._rule.host_list = conditions.host_list
- self._rule.item_list = conditions.item_list
+ self._rule.update_conditions(self._get_rule_conditions_from_vars())
# VALUE
if self._ruleset.valuespec():
diff --git a/cmk/gui/watolib/rulesets.py b/cmk/gui/watolib/rulesets.py
index 5bc6969..2ef06e3 100644
--- a/cmk/gui/watolib/rulesets.py
+++ b/cmk/gui/watolib/rulesets.py
@@ -925,6 +925,12 @@ class Rule(object):
#TODO: Once we switched the rule format to be dict base, we can move this key to
the conditions dict
return self.rule_options.get("predefined_condition_id")
+ def update_conditions(self, conditions):
+ self.tag_specs = conditions.host_tags
+ self.host_list = conditions.host_list
+ if self.ruleset.item_type():
+ self.item_list = conditions.item_list
+
def is_discovery_rule_of(self, host):
return self.host_list == [host.name()] \
and self.tag_specs == [] \