def test_remove_removed_check_plugins_from_ignored_checks( uc: update_config.UpdateConfig) -> None: ruleset = Ruleset("ignored_checks", {}) ruleset.from_config( Folder(""), [ { "id": "1", "condition": {}, "options": { "disabled": False }, "value": ["a", "b", "mgmt_c"], }, { "id": "2", "condition": {}, "options": { "disabled": False }, "value": ["d", "e"], }, { "id": "3", "condition": {}, "options": { "disabled": False }, "value": ["mgmt_f"], }, { "id": "4", "condition": {}, "options": { "disabled": False }, "value": ["a", "g"], }, ], ) rulesets = RulesetCollection() rulesets.set_rulesets({"ignored_checks": ruleset}) uc._remove_removed_check_plugins_from_ignored_checks( rulesets, { CheckPluginName("b"), CheckPluginName("d"), CheckPluginName("e"), CheckPluginName("f"), }, ) leftover_rules = [ rule for (_folder, idx, rule) in rulesets.get("ignored_checks").get_rules() ] assert len(leftover_rules) == 2 assert leftover_rules[0].id == "1" assert leftover_rules[1].id == "4" assert leftover_rules[0].value == ["a", "mgmt_c"] assert leftover_rules[1].value == ["a", "g"]
def _instantiate_ruleset(ruleset_name, param_value) -> Ruleset: ruleset = Ruleset(ruleset_name, {}) rule = Rule.from_ruleset_defaults(Folder(""), ruleset) rule.value = param_value ruleset.append_rule(Folder(""), rule) assert ruleset.get_rules() return ruleset
def _instantiate_ruleset(ruleset_name, param_value): ruleset = Ruleset(ruleset_name, {}) rule = Rule(Folder(''), ruleset) rule.value = param_value ruleset.append_rule(Folder(''), rule) assert ruleset.get_rules() return ruleset
def _update_rule_of_host(self, ruleset: Ruleset, service_patterns: HostOrServiceConditions, value: Any) -> List[CREFolder]: folder = self._host.folder() rule = self._get_rule_of_host(ruleset, value) if rule: for service_condition in service_patterns: if service_condition not in rule.conditions.service_description: rule.conditions.service_description.append( service_condition) elif service_patterns: rule = Rule.from_ruleset_defaults(folder, ruleset) conditions = RuleConditions(folder.path()) conditions.host_name = [self._host.name()] # mypy is wrong here vor some reason: # Invalid index type "str" for "Union[Dict[str, str], str]"; expected type "Union[int, slice]" [index] conditions.service_description = sorted(service_patterns, key=lambda x: x["$regex"]) rule.update_conditions(conditions) rule.value = value ruleset.prepend_rule(folder, rule) if rule: return [rule.folder] return []
def _serialize_ruleset(ruleset: Ruleset) -> DomainObject: members = {} if ruleset.num_rules() > 0: members["rules"] = constructors.collection_property( name="rules", value=[], base="", ) return constructors.domain_object( domain_type="ruleset", identifier=ruleset.name, title=ruleset.title() or ruleset.name, editable=False, deletable=False, members=members, extensions={ "name": ruleset.name, "title": ruleset.title(), "item_type": ruleset.item_type(), "item_name": ruleset.item_name(), "item_enum": ruleset.item_enum(), "match_type": ruleset.match_type(), "help": strip_tags(ruleset.help()), "number_of_rules": ruleset.num_rules(), }, )
def test__transform_replaced_wato_rulesets_and_params( uc: update_config.UpdateConfig, ruleset_name: RulesetName, param_value: RuleValue, new_ruleset_name: RulesetName, transformed_param_value: RuleValue, ) -> None: all_rulesets = RulesetCollection() # checkmk: all_rulesets are loaded via # all_rulesets = cmk.gui.watolib.rulesets.AllRulesets() all_rulesets.set_rulesets({ ruleset_name: _instantiate_ruleset(ruleset_name, param_value), new_ruleset_name: Ruleset(new_ruleset_name, {}), }) uc._transform_replaced_wato_rulesets(all_rulesets) uc._transform_wato_rulesets_params(all_rulesets) assert not all_rulesets.exists(ruleset_name) rules = all_rulesets.get(new_ruleset_name).get_rules() assert len(rules) == 1 rule = rules[0] assert len(rule) == 3 assert rule[2].value == transformed_param_value
def _save_service_enable_disable_rules(self, services, value): if not services: return rulesets = AllRulesets() rulesets.load() try: ruleset = rulesets.get("ignored_services") except KeyError: ruleset = Ruleset( "ignored_services", ruleset_matcher.get_tag_to_group_map(active_config.tags)) modified_folders = [] service_patterns: HostOrServiceConditions = [ service_description_to_condition(s) for s in services ] modified_folders += self._remove_from_rule_of_host(ruleset, service_patterns, value=not value) # Check whether or not the service still needs a host specific setting after removing # the host specific setting above and remove all services from the service list # that are fine without an additional change. for service in list(services): value_without_host_rule = ruleset.analyse_ruleset( self._host.name(), service, service)[0] if (not value and value_without_host_rule in [None, False]) or value == value_without_host_rule: services.remove(service) service_patterns = [ service_description_to_condition(s) for s in services ] modified_folders += self._update_rule_of_host(ruleset, service_patterns, value=value) for folder in modified_folders: rulesets.save_folder(folder)
def _get_custom_check_origin_rule(self, ruleset: Ruleset, hostname: str, svc_desc: str) -> Optional[_Tuple[CREFolder, int, Rule]]: # We could use the outcome of _setting instead of the outcome of # the automation call in the future _setting, rules = ruleset.analyse_ruleset(self._hostname, svc_desc_or_item=None, svc_desc=None) for rule_folder, rule_index, rule in rules: if rule.is_disabled(): continue if rule.value["service_description"] != self._service: continue return rule_folder, rule_index, rule return None
def test__transform_discovery_disabled_services( uc: update_config.UpdateConfig, ruleset_spec: Sequence[RuleSpec], expected_ruleset: Sequence[RuleSpec], ) -> None: ruleset = Ruleset("ignored_services", {}) ruleset.from_config(Folder(""), ruleset_spec) assert ruleset.get_rules() rulesets = RulesetCollection() rulesets.set_rulesets({"ignored_services": ruleset}) uc._transform_discovery_disabled_services(rulesets) folder_rules = ruleset.get_folder_rules(Folder("")) assert [r.to_config() for r in folder_rules] == expected_ruleset
def test__transform_discovery_disabled_services( ruleset_spec, expected_ruleset, ): ruleset = Ruleset("ignored_services", {}) ruleset.from_config(Folder(''), ruleset_spec) assert ruleset.get_rules() rulesets = RulesetCollection() rulesets.set_rulesets({"ignored_services": ruleset}) uc = update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace()) uc._transform_discovery_disabled_services(rulesets) folder_rules = ruleset.get_folder_rules(Folder('')) assert [r.to_config() for r in folder_rules] == expected_ruleset