def _update_bi_pack(params, must_exist: bool): user.need_permission("wato.edit") user.need_permission("wato.bi_rules") user.need_permission("wato.bi_admin") bi_packs = get_cached_bi_packs() bi_packs.load_config() pack_id = params["pack_id"] existing_pack = bi_packs.get_pack(pack_id) if existing_pack and not must_exist: _bailout_with_message("This pack_id already exists: %s" % pack_id) if not existing_pack and must_exist: _bailout_with_message("This pack_id does not exist: %s" % pack_id) pack_config = {} if existing_pack: # Serialize the old pack # Rules and aggregations will be transferred to the new pack pack_config.update(existing_pack.serialize()) pack_config["id"] = pack_id pack_config.update(BIPackEndpointSchema().dump(params["body"])) new_pack = BIAggregationPack(pack_config) bi_packs.add_pack(new_pack) bi_packs.save_config() return constructors.serve_json(BIPackEndpointSchema().dump(new_pack.serialize()))
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 _update_bi_aggregation(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() aggregation_config = params["body"] try: target_pack = bi_packs.get_pack_mandatory(aggregation_config["pack_id"]) except MKGeneralException: _bailout_with_message("Unknown bi_pack: %s" % aggregation_config["pack_id"]) aggregation_id = params["aggregation_id"] aggregation_exists = bool(bi_packs.get_aggregation(aggregation_id)) if aggregation_exists and not must_exist: _bailout_with_message("This aggregation_id already exists: %s" % aggregation_id) if not aggregation_exists and must_exist: _bailout_with_message("This aggregation_id does not exist: %s" % aggregation_id) aggregation_config["id"] = aggregation_id bi_aggregation = BIAggregation(aggregation_config) target_pack.add_aggregation(bi_aggregation) bi_packs.save_config() data = {"pack_id": bi_aggregation.pack_id} data.update(bi_aggregation.schema()().dump(bi_aggregation)) return constructors.serve_json(data)
def convert_to_vs(value): if value.get("rule_id") is None: return None bi_pack = get_cached_bi_packs().get_pack_of_rule(value["rule_id"]) if bi_pack is None: return None return ( (bi_pack.id, value["rule_id"]), value["params"]["arguments"], )
def delete_bi_rule(params): """Delete BI rule""" bi_packs = get_cached_bi_packs() bi_packs.load_config() try: bi_rule = bi_packs.get_rule_mandatory(params["rule_id"]) except KeyError: _bailout_with_message("Unknown bi_rule: %s" % params["rule_id"]) bi_packs.delete_rule(bi_rule.id) bi_packs.save_config() return Response(status=204)
def delete_bi_aggregation(params): """Delete a BI aggregation""" bi_packs = get_cached_bi_packs() bi_packs.load_config() try: bi_aggregation = bi_packs.get_aggregation_mandatory(params["aggregation_id"]) except KeyError: _bailout_with_message("Unknown bi_aggregation: %s" % params["aggregation_id"]) bi_packs.delete_aggregation(bi_aggregation.id) bi_packs.save_config() return Response(status=204)
def get_bi_aggregation(params): """Get BI Aggregation""" bi_packs = get_cached_bi_packs() bi_packs.load_config() try: bi_aggregation = bi_packs.get_aggregation_mandatory(params["aggregation_id"]) except KeyError: _bailout_with_message("Unknown bi_aggregation: %s" % params["aggregation_id"]) data = {"pack_id": bi_aggregation.pack_id} data.update(BIAggregationSchema().dump(bi_aggregation)) return constructors.serve_json(data)
def get_bi_rule(params): """Get BI Rule""" bi_packs = get_cached_bi_packs() bi_packs.load_config() bi_rule = bi_packs.get_rule(params["rule_id"]) if bi_rule is None: _bailout_with_message("Unknown bi_rule: %s" % params["rule_id"]) assert bi_rule is not None data = {"pack_id": bi_rule.pack_id} data.update(BIRuleSchema().dump(bi_rule)) return constructors.serve_json(data)
def get_bi_rule(params): """Show a BI rule""" bi_packs = get_cached_bi_packs() bi_packs.load_config() try: bi_rule = bi_packs.get_rule_mandatory(params["rule_id"]) except KeyError: _bailout_with_message("Unknown bi_rule: %s" % params["rule_id"]) data = {"pack_id": bi_rule.pack_id} data.update(BIRuleSchema().dump(bi_rule)) return constructors.serve_json(data)
def get_bi_aggregation(params): """Get BI Aggregation""" bi_packs = get_cached_bi_packs() bi_packs.load_config() bi_aggregation = bi_packs.get_aggregation(params["aggregation_id"]) if bi_aggregation is None: _bailout_with_message("Unknown bi_aggregation: %s" % params["aggregation_id"]) assert bi_aggregation is not None data = {"pack_id": bi_aggregation.pack_id} data.update(BIAggregationSchema().dump(bi_aggregation)) return constructors.serve_json(data)
def get_bi_aggregation(params): """Get a BI aggregation""" user.need_permission("wato.bi_rules") bi_packs = get_cached_bi_packs() bi_packs.load_config() try: bi_aggregation = bi_packs.get_aggregation_mandatory(params["aggregation_id"]) except MKGeneralException: _bailout_with_message("Unknown bi_aggregation: %s" % params["aggregation_id"]) data = {"pack_id": bi_aggregation.pack_id} data.update(BIAggregationSchema().dump(bi_aggregation)) return constructors.serve_json(data)
def get_bi_rule(params): """Show a BI rule""" user.need_permission("wato.bi_rules") bi_packs = get_cached_bi_packs() bi_packs.load_config() try: bi_rule = bi_packs.get_rule_mandatory(params["rule_id"]) except MKGeneralException: _bailout_with_message("Unknown bi_rule: %s" % params["rule_id"]) data = {"pack_id": bi_rule.pack_id} data.update(BIRuleSchema().dump(bi_rule)) return constructors.serve_json(data)
def _validate_rule_call(cls, value, varprefix): (_pack_id, rule_id), arguments = value bi_rule = get_cached_bi_packs().get_rule(rule_id) if bi_rule is None: raise MKUserError(varprefix + "_1_0", _("The target rule is no longer available")) rule_params = bi_rule.params.arguments if len(arguments) != len(rule_params): raise MKUserError( varprefix + "_1_0", _("The rule you selected needs %d argument(s) (%s), " "but you configured %d arguments.") % (len(rule_params), ', '.join(rule_params), len(arguments)))
def _allowed_rule_choices(cls): # TODO: cache choices = [] for pack_id, bi_pack in sorted(get_cached_bi_packs().get_packs().items()): if may_use_rules_in_pack(bi_pack): pack_choices = [(rule_id, "%s (%s)" % (bi_rule.title, rule_id)) for rule_id, bi_rule in bi_pack.get_rules().items()] choices.append( (pack_id, "%s (%s)" % (bi_pack.title, bi_pack.id), DropdownChoice( choices=sorted(pack_choices), empty_text=_("There are no configured rules in this aggregation pack"), on_change="cmk.bi.update_argument_hints();", ))) return choices
def get_bi_pack(params): """Get a BI pack and its rules and aggregations""" user.need_permission("wato.bi_rules") bi_packs = get_cached_bi_packs() bi_packs.load_config() bi_pack = bi_packs.get_pack(params["pack_id"]) if bi_pack is None: _bailout_with_message("This pack_id does not exist: %s" % params["pack_id"]) assert bi_pack is not None uri = constructors.object_href("bi_pack", bi_pack.id) domain_members = {} for (name, entities) in [ ("aggregation", bi_pack.get_aggregations()), ("rule", bi_pack.get_rules()), ]: elements = entities.values() domain_members["%ss" % name] = constructors.object_collection( name=name, domain_type="bi_" + name, # type: ignore[arg-type] entries=[ constructors.link_rel( rel=".../value", parameters={"collection": "items"}, href=constructors.object_href( "bi_" + name, element.id # type: ignore[arg-type] ), ) for element in elements ], base=uri, ) extensions = { "title": bi_pack.title, "contact_groups": bi_pack.contact_groups, "public": bi_pack.public, } domain_object = constructors.domain_object( domain_type="bi_pack", identifier=bi_pack.id, title=bi_pack.title, extensions=extensions, members=domain_members, ) return constructors.serve_json(domain_object)
def put_bi_rule(params): """Save BI Rule""" bi_packs = get_cached_bi_packs() 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)) return constructors.serve_json(data)
def _allowed_rule_choices(cls): # TODO: cache choices = [] for pack_id, bi_pack in sorted( get_cached_bi_packs().get_packs().items()): if may_use_rules_in_pack(bi_pack): pack_choices = [ (rule_id, "%s (%s)" % (bi_rule.title, rule_id)) for rule_id, bi_rule in bi_pack.get_rules().items() ] choices.append( (pack_id, "%s (%s)" % (bi_pack.title, bi_pack.id), DropdownChoice( choices=sorted(pack_choices), on_change="cmk.bi.update_argument_hints();"))) return choices
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 _get_node_data_of_bi_aggregator( self, node: Dict[str, Any]) -> Dict[str, Any]: bi_packs = bi.get_cached_bi_packs() node_data: Dict[str, Any] = {} node_data["node_type"] = "bi_aggregator" bi_rule = bi_packs.get_rule_mandatory(node["rule_id"]) node_data["rule_id"] = { "pack": bi_rule.pack_id, "rule": bi_rule.id, "function": node["rule_id"][2] # TODO: fix visualization of function } if "rule_layout_style" in node: node_data["rule_layout_style"] = node["rule_layout_style"] if "aggregation_id" in node: node_data["aggregation_id"] = node["aggregation_id"] return node_data
def delete_bi_pack(params): """Delete BI pack""" bi_packs = get_cached_bi_packs() bi_packs.load_config() pack_id = params["pack_id"] try: target_pack = bi_packs.get_pack_mandatory(pack_id) except KeyError: _bailout_with_message("Unknown bi_pack: %s" % pack_id) num_rules = target_pack.num_rules() if num_rules > 0: _bailout_with_message( "Cannot delete bi_pack %s. It contains %d rules, which might be used in other packs" % (pack_id, num_rules)) bi_packs.delete_pack(pack_id) bi_packs.save_config() return Response(status=204)
def get_bi_packs(params): """Show all BI packs""" user.need_permission("wato.bi_rules") bi_packs = get_cached_bi_packs() bi_packs.load_config() packs = [ constructors.collection_item( domain_type="bi_pack", identifier=pack.id, title=pack.title, ) for pack in bi_packs.packs.values() ] collection_object = constructors.collection_object( domain_type="bi_pack", value=packs, links=[constructors.link_rel("self", constructors.collection_href("bi_pack"))], ) return constructors.serve_json(collection_object)
def put_bi_aggregation(params): """Save BI Aggregation""" bi_packs = get_cached_bi_packs() bi_packs.load_config() aggregation_config = params["body"] try: target_pack = bi_packs.get_pack_mandatory(aggregation_config["pack_id"]) except KeyError: _bailout_with_message("Unknown bi_pack: %s" % aggregation_config["pack_id"]) aggregation_config["id"] = params["aggregation_id"] bi_aggregation = BIAggregation(aggregation_config) target_pack.add_aggregation(bi_aggregation) bi_packs.save_config() data = {"pack_id": bi_aggregation.pack_id} data.update(bi_aggregation.schema()().dump(bi_aggregation)) return constructors.serve_json(data)
def get_bi_packs(params): """Show all BI Packs""" bi_packs = get_cached_bi_packs() bi_packs.load_config() packs = [ constructors.collection_item( domain_type='bi_pack', obj={ 'id': pack.id, 'title': pack.title, }, ) for pack in bi_packs.packs.values() ] collection_object = constructors.collection_object( domain_type='bi_pack', value=packs, links=[constructors.link_rel('self', constructors.collection_href('bi_pack'))], ) return constructors.serve_json(collection_object)
def get_bi_packs(params): """Show all BI packs""" bi_packs = get_cached_bi_packs() bi_packs.load_config() packs = [ constructors.collection_item( domain_type="bi_pack", obj={ "id": pack.id, "title": pack.title, }, ) for pack in bi_packs.packs.values() ] collection_object = constructors.collection_object( domain_type="bi_pack", value=packs, links=[constructors.link_rel("self", constructors.collection_href("bi_pack"))], ) return constructors.serve_json(collection_object)
def get_bi_pack(params): """Get BI Pack""" bi_packs = get_cached_bi_packs() bi_packs.load_config() bi_pack = bi_packs.get_pack(params["pack_id"]) if bi_pack is None: _bailout_with_message("Unknown bi_pack: %s" % params["pack_id"]) assert bi_pack is not None uri = constructors.object_href('bi_pack', bi_pack.id) domain_members = {} for (name, entities) in [("aggregation", bi_pack.get_aggregations()), ("rule", bi_pack.get_rules())]: elements = entities.values() # type: ignore[attr-defined] domain_members["%ss" % name] = constructors.object_collection( name=name, domain_type="bi_" + name, # type: ignore[arg-type] entries=[ constructors.link_rel( rel='.../value', parameters={'collection': "items"}, href=constructors.object_href( "bi_" + name # type: ignore[arg-type] , element.id), ) for element in elements ], base=uri, ) domain_object = constructors.domain_object(domain_type='bi_pack', identifier=bi_pack.id, title=bi_pack.title, members=domain_members) return constructors.serve_json(domain_object)
def _rename_host_in_bi(oldname, newname): return BIHostRenamer().rename_host(oldname, newname, get_cached_bi_packs())