Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
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)
Esempio n. 4
0
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)