def _update_bi_rule(params, must_exist: bool): user.need_permission("wato.edit") user.need_permission("wato.bi_rules") bi_packs = get_cached_bi_packs() bi_packs.load_config() rule_config = params["body"] try: target_pack = bi_packs.get_pack_mandatory(rule_config["pack_id"]) except KeyError: _bailout_with_message("Unknown bi_pack: %s" % rule_config["pack_id"]) rule_id = params["rule_id"] rule_exists = bool(bi_packs.get_rule(rule_id)) if rule_exists and not must_exist: _bailout_with_message("This rule_id already exists: %s" % rule_id) if not rule_exists and must_exist: _bailout_with_message("This rule_id does not exist: %s" % rule_id) rule_config["id"] = rule_id bi_rule = BIRule(rule_config) target_pack.add_rule(bi_rule) bi_packs.save_config() data = {"pack_id": bi_rule.pack_id} data.update(bi_rule.schema()().dump(bi_rule)) return constructors.serve_json(data)
def dummy_bi_rule(): rule_id = "dummy_rule" try: node_schema = BINodeGenerator.schema()().dump({}) node_schema["action"]["host_regex"] = "heute_clone" schema_config = BIRule.schema()().dump({"id": rule_id}) schema_config["nodes"].append(node_schema) yield BIRule(schema_config) finally: bi_rule_id_registry.unregister(rule_id)
def put_bi_rule(params): """Save BI Rule""" bi_packs.load_config() target_pack = bi_packs.get_pack(params["body"]["pack_id"]) if target_pack is None: _bailout_with_message("Unknown bi_pack: %s" % params["body"]["pack_id"]) assert target_pack is not None bi_rule = BIRule(params["body"]) target_pack.add_rule(bi_rule) bi_packs.save_config() data = {"pack_id": bi_rule.pack_id} data.update(bi_rule.schema()().dump(bi_rule).data) return constructors.serve_json(data)
def put_bi_rule(params): """Save BI Rule""" bi_packs = get_cached_bi_packs() bi_packs.load_config() try: target_pack = bi_packs.get_pack_mandatory(params["body"]["pack_id"]) except KeyError: _bailout_with_message("Unknown bi_pack: %s" % params["body"]["pack_id"]) bi_rule = BIRule(params["body"]) target_pack.add_rule(bi_rule) bi_packs.save_config() data = {"pack_id": bi_rule.pack_id} data.update(bi_rule.schema()().dump(bi_rule)) return constructors.serve_json(data)
def create_trees_from_schema(cls, schema_config: Dict[str, Any]) -> BICompiledAggregation: branches = [BIRule.create_tree_from_schema(config) for config in schema_config["branches"]] aggregation_id = schema_config["id"] computation_options = BIAggregationComputationOptions(schema_config["computation_options"]) aggregation_visualization = schema_config["aggregation_visualization"] groups = BIAggregationGroups(schema_config["groups"]) return BICompiledAggregation(aggregation_id, branches, computation_options, aggregation_visualization, groups)
def __init__(self, pack_config: Dict[str, Any]): super().__init__() self.id = pack_config["id"] self.title = pack_config["title"] self.comment = pack_config.get("comment", "") self.contact_groups = pack_config["contact_groups"] self.public = pack_config["public"] self.rules = {x["id"]: BIRule(x, self.id) for x in pack_config.get("rules", [])} self.aggregations = { x["id"]: BIAggregation(x, self.id) for x in pack_config.get("aggregations", []) }
def _rule_uses_rule(self, bi_rule: BIRule, check_rule_id: str, level: int = 0) -> int: for bi_node in bi_rule.get_nodes(): if isinstance(bi_node.action, BICallARuleAction): node_rule_id = bi_node.action.rule_id if node_rule_id == check_rule_id: # Rule is directly being used return level + 1 # Check if lower rules use it bi_subrule = self.get_rule_mandatory(node_rule_id) lv = self._rule_uses_rule(bi_subrule, check_rule_id, level + 1) if lv != -1: return lv return -1