def _update_rule_of_host(self, ruleset: watolib.Ruleset, service_patterns: List[Dict[str, str]], value: Any) -> List[watolib.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 = watolib.Rule.create(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"]) # type: ignore[index] rule.update_conditions(conditions) rule.value = value ruleset.prepend_rule(folder, rule) if rule: return [rule.folder] return []
def create_rule(param): """Create rule""" body = param["body"] folder = body["folder"] value = body["value_raw"] rulesets = watolib.FolderRulesets(folder) rulesets.load() try: ruleset = rulesets.get(body["ruleset"]) except KeyError: return problem( status=400, detail=f"Ruleset {body['ruleset']!r} could not be found.", ) try: ruleset.valuespec().validate_value(value, "") except exceptions.MKUserError as exc: if exc.varname is None: title = "A field has a problem" else: field_name = exc.varname.replace("_p_", "") title = f"Problem in (sub-)field {field_name!r}" return problem( status=400, detail=strip_tags(exc.message), title=title, ) rule = watolib.Rule( gen_id(), folder, ruleset, RuleConditions( host_folder=folder, host_tags=body["conditions"].get("host_tag"), host_labels=body["conditions"].get("host_label"), host_name=body["conditions"].get("host_name"), service_description=body["conditions"].get("service_description"), service_labels=body["conditions"].get("service_label"), ), RuleOptions.from_config(body["properties"]), value, ) index = ruleset.append_rule(folder, rule) rulesets.save() # TODO Duplicated code is in pages/rulesets.py:2670- # TODO Move to watolib add_change( "new-rule", _l('Created new rule #%d in ruleset "%s" in folder "%s"') % (index, ruleset.title(), folder.alias_path()), sites=folder.all_site_ids(), diff_text=make_diff_text({}, rule.to_log()), object_ref=rule.object_ref(), ) return serve_json(_serialize_rule(folder, index, rule))
def _update_rule_of_host(self, ruleset, service_patterns, value): 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 = watolib.Rule.create(folder, ruleset) conditions = RuleConditions(folder.path()) conditions.host_name = [self._host.name()] conditions.service_description = sorted(service_patterns) rule.update_conditions(conditions) rule.value = value ruleset.prepend_rule(folder, rule) if rule: return [rule.folder] return []