def get_source(team_id, source_id): """Get a source from a team. .. :quickref: GET; Get a source from a team. **Example request**: .. sourcecode:: http GET /teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/sources/672d82d7-1970-48cd-a690-20f5daf303cf HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK { "checks": [{ "created_at": "2018-05-17T11:52:25Z", "id": "86ee5bdb-7088-400c-8654-a162f18b0710", "name": "Server Ping", "parameters": { "metric": "depc.tutorial.ping", "threshold": 20 }, "source_id": "672d82d7-1970-48cd-a690-20f5daf303cf", "type": "Threshold", "updated_at": "2018-11-12T17:41:11Z" }], "configuration": {}, "createdAt": "2018-05-16T11:38:46Z", "id": "672d82d7-1970-48cd-a690-20f5daf303cf", "name": "My source", "plugin": "Fake", "updatedAt": "2019-01-15T13:33:22Z" } :resheader Content-Type: application/json :status 200: the list of sources """ if not TeamPermission.is_user(team_id): abort(403) source = SourceController.get( filters={"Source": { "id": source_id, "team_id": team_id }}) return ( jsonify( format_source(source, config=TeamPermission.is_manager(team_id))), 200, )
def list_team_checks(team_id): """List the checks of a team. .. :quickref: GET; List the checks of a team. **Example request**: .. sourcecode:: http GET /teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/checks HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK { 'checks': [{ 'name': 'My check', 'parameters': {'metric': 'foo', 'threshold': 100}, 'type': 'Threshold' }] } :resheader Content-Type: application/json :status 200: the list of checks """ if not TeamPermission.is_user(team_id): abort(403) checks = CheckController.list_team_checks(team_id) return jsonify({"checks": [format_check(c) for c in checks]}), 200
def delete_source(team_id, source_id): """Delete a source. .. :quickref: DELETE; Delete a source. **Example request**: .. sourcecode:: http DELETE /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/sources/e2c1c635-2d7c-4881-83d1-e4e7027ac7a2 HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK {} :resheader Content-Type: application/json :status 200: the source has been deleted """ if not TeamPermission.is_manager(team_id): abort(403) SourceController.delete( filters={"Source": { "id": source_id, "team_id": team_id }}) return jsonify({}), 200
def delete_rule(team_id, rule_id): """Delete a rule. .. :quickref: DELETE; Delete a rule. **Example request**: .. sourcecode:: http DELETE /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/rules/ff130e9b-d226-4465-9612-a93e12799091 HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK {} :resheader Content-Type: application/json :status 200: the rule has been deleted """ if not TeamPermission.is_manager_or_editor(team_id): abort(403) RuleController.delete( filters={"Rule": { "id": rule_id, "team_id": team_id }}) return jsonify({}), 200
def delete_node(team_id, label, node): """Delete a node. .. :quickref: DELETE; Delete a node. **Example request**: .. sourcecode:: http DELETE /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/labels/Website/nodes/example.com HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK {} :resheader Content-Type: application/json :param detach: Remove the relations of the node :status 200: the node has been deleted """ if not TeamPermission.is_manager(team_id): abort(403) return jsonify( DependenciesController.delete_node( team_id=team_id, label=label, node=node, detach=request.args.get("detach", False), ))
def count_node_dependencies(team_id, label, node): """Count the dependencies of a node. .. :quickref: GET; Count the dependencies of a node. **Example request**: .. sourcecode:: http GET /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/labels/MyLabel/nodes/mynode/count HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK { 'count': 10 } :resheader Content-Type: application/json :status 200: the number of dependencies """ if not TeamPermission.is_user(team_id): abort(403) return jsonify( DependenciesController.count_node_dependencies(team_id, label, node))
def get_labels(team_id): """ .. :quickref: GET; Lorem ipsum.""" if not TeamPermission.is_user(team_id): abort(403) return jsonify(DependenciesController.get_labels(team_id))
def get_current_config(team_id): """ .. :quickref: GET; Lorem ipsum.""" if not TeamPermission.is_user(team_id): abort(403) config = ConfigController.get_current_config(team_id=team_id) return jsonify(config), 200
def count_node_dependencies(team_id, label, node): """ .. :quickref: GET; Lorem ipsum.""" if not TeamPermission.is_user(team_id): abort(403) return jsonify( DependenciesController.count_node_dependencies(team_id, label, node))
def put_rule_checks(team_id, rule_id): """ .. :quickref: PUT; Lorem ipsum.""" if not TeamPermission.is_manager_or_editor(team_id): abort(403) payload = get_payload() rule = RuleController.update_checks(rule_id=rule_id, checks_id=payload["checks"]) return jsonify({"checks": [format_check(r) for r in rule["checks"]]}), 200
def post_source_variable(team_id, source_id): """ .. :quickref: POST; Lorem ipsum.""" if not TeamPermission.is_manager_or_editor(team_id): abort(403) payload = get_payload() payload.update({"team_id": team_id, "source_id": source_id}) variable = VariableController.create(payload) return jsonify(format_variable(variable)), 200
def delete_source_check(team_id, source_id, check_id): """ .. :quickref: DELETE; Lorem ipsum.""" if not TeamPermission.is_manager_or_editor(team_id): abort(403) check = CheckController.delete( filters={"Check": {"id": check_id, "source_id": source_id}} ) return jsonify(format_check(check)), 200
def revert_config(team_id, config_id): """ .. :quickref: PUT; Lorem ipsum.""" if not TeamPermission.is_manager(team_id): abort(403) config = ConfigController.revert_config(team_id=team_id, config_id=config_id) if not config: abort(404) return jsonify(config), 200
def get_team_worst_qos(team_id): """ .. :quickref: GET; Lorem ipsum.""" if not TeamPermission.is_user(team_id): abort(403) label = request.args.get("label", None) date = request.args.get("date", arrow.now().shift(days=-1).format("YYYY-MM-DD")) return jsonify(WorstController.get_daily_worst_items(team_id, label, date))
def put_source_check(team_id, source_id, check_id): """ .. :quickref: PUT; Lorem ipsum.""" if not TeamPermission.is_manager_or_editor(team_id): abort(403) payload = get_payload() check = CheckController.update( payload, {"Check": {"id": check_id, "source_id": source_id}} ) return jsonify(format_check(check)), 200
def list_configs(team_id): """ .. :quickref: GET; Lorem ipsum.""" if not TeamPermission.is_user(team_id): abort(403) configs = ConfigController.list(filters={"Config": { "team_id": team_id }}, order_by="updated_at", reverse=True) return jsonify(configs), 200
def put_config(team_id): """ .. :quickref: POST; Lorem ipsum.""" if not TeamPermission.is_manager(team_id): abort(403) payload = request.get_json(force=True) current_conf = ConfigController.create({ "team_id": team_id, "data": payload }) return jsonify(current_conf), 200
def execute_rule(team_id, rule_id): """ .. :quickref: POST; Lorem ipsum.""" if not TeamPermission.is_user(team_id): abort(403) payload = get_payload() # Does the team owns the rule RuleController.get({"Rule": {"id": rule_id, "team_id": team_id}}) result = RuleController.execute(rule_id, **payload) return jsonify({"result": result}), 200
def get_node_dependencies(team_id, label, node): """ .. :quickref: GET; Lorem ipsum.""" if not TeamPermission.is_user(team_id): abort(403) return jsonify( DependenciesController.get_node_dependencies( team_id=team_id, label=label, node=node, filter_on_config=request.args.get("with_config", False), ))
def delete_node(team_id, label, node): """ .. :quickref: DELETE; Lorem ipsum.""" if not TeamPermission.is_manager(team_id): abort(403) return jsonify( DependenciesController.delete_node( team_id=team_id, label=label, node=node, detach=request.args.get("detach", False), ))
def get_impacted_nodes(team_id, label, node): """Get the nodes impacted by a given node. .. :quickref: GET; Get the nodes impacted by a given node. **Example request**: .. sourcecode:: http GET /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/labels/Apache/nodes/apache2/impacted?impactedLabel=Offer&skip=0&limit=25&ts=1564645111 HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK [ { "active": true, "from": null, "name": "premium", "to": null } ] :param impactedLabel: impacted nodes for the given label :param skip: skip the given number of values (default is 0) :param limit: limit to the given number of values (default is 25) :param ts: unix timestamp to check if the nodes are active or not at this timestamp :resheader Content-Type: application/json :status 200: the array of impacted nodes """ if not TeamPermission.is_user(team_id): abort(403) return jsonify( DependenciesController.get_impacted_nodes( team_id, label, node, request.args.get("impactedLabel", None), request.args.get("skip", 0), request.args.get("limit", 25), request.args.get("ts", None), ) )
def get_labels(team_id): """Get the labels of a team. .. :quickref: GET; Get the labels of a team. **Example request**: .. sourcecode:: http GET /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/labels HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK [ { "name": "Filer", "nodes_count": 2, "qos_query": "rule.Servers" }, { "name": "Website", "nodes_count": 2, "qos_query": "operation.AND[Filer, Apache]" }, { "name": "Apache", "nodes_count": 2, "qos_query": "rule.Servers" }, { "name": "Offer", "nodes_count": 1, "qos_query": "aggregation.AVERAGE[Website]" } ] :resheader Content-Type: application/json :status 200: the list of labels """ if not TeamPermission.is_user(team_id): abort(403) return jsonify(DependenciesController.get_labels(team_id))
def get_label_nodes(team_id, label): """ .. :quickref: GET; Lorem ipsum.""" if not TeamPermission.is_user(team_id): abort(403) return jsonify( DependenciesController.get_label_nodes( team_id, label, request.args.get("name", None), request.args.get("limit", None), request.args.get("random", False), ))
def post_source_check(team_id, source_id): """Add a new check. .. :quickref: POST; Add a new check. **Example request**: .. sourcecode:: http POST /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/sources/e2c1c635-2d7c-4881-83d1-e4e7027ac7a2/checks HTTP/1.1 Host: example.com Accept: application/json { "name": "My check", "type": "Threshold", "parameters": { "metric": "foo", "threshold": 100 } } **Example response**: .. sourcecode:: http HTTP/1.1 201 CREATED { 'name': 'My check', 'parameters': { 'metric': 'foo', 'threshold': 100 }, 'type': 'Threshold' } :resheader Content-Type: application/json :status 201: the created check """ if not TeamPermission.is_manager_or_editor(team_id): abort(403) payload = get_payload() payload["source_id"] = source_id source = CheckController.create(payload) return jsonify(format_check(source)), 201
def get_impacted_nodes_all(team_id, label, node): """Get a JSON payload containing all nodes impacted by a given node. .. :quickref: GET; Get a JSON payload containing all nodes impacted by a given node. **Example request**: .. sourcecode:: http GET /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/labels/Apache/nodes/apache2/impacted/all?impactedLabel=Offer&ts=1564645344 HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK [ { "active": true, "from": null, "name": "premium", "to": null } ] :param impactedLabel: impacted nodes for the given label :param ts: unix timestamp to check if the nodes are active or not at this timestamp :param inactive: return inactive impacted nodes in the downloaded file or not (default is False) :resheader Content-Type: application/json :status 200: the array of impacted nodes """ if not TeamPermission.is_user(team_id): abort(403) json_string = DependenciesController.get_impacted_nodes_all( team_id, label, node, request.args.get("impactedLabel", None), request.args.get("ts", None), request.args.get("inactive", False), ) return jsonify({"data": json_string})
def get_config(team_id, config_id): """ .. :quickref: GET; Lorem ipsum.""" if not TeamPermission.is_user(team_id): abort(403) config = ConfigController.get( filters={"Config": { "team_id": team_id, "id": config_id }}) if not config: abort(404) return jsonify(config), 200
def put_source(team_id, source_id): """Edit an existing source. .. :quickref: PUT; Edit an existing source. **Example request**: .. sourcecode:: http PUT /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/sources/e2c1c635-2d7c-4881-83d1-e4e7027ac7a2 HTTP/1.1 Host: example.com Accept: application/json { "name": "My edited source" } **Example response**: .. sourcecode:: http HTTP/1.1 200 OK { "checks": [], "configuration": {}, "createdAt": "2019-01-21T14:08:22Z", "id": "e2c1c635-2d7c-4881-83d1-e4e7027ac7a2", "name": "My edited source", "plugin": "Fake", "updatedAt": "2019-01-21T14:21:24Z" } :resheader Content-Type: application/json :status 200: the edited source """ if not TeamPermission.is_manager(team_id): abort(403) payload = get_payload() source = SourceController.update( payload, {"Source": { "id": source_id, "team_id": team_id }}) return jsonify(format_source(source, True)), 200
def post_source(team_id): """Add a new source. .. :quickref: POST; Add a new source. **Example request**: .. sourcecode:: http POST /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/sources HTTP/1.1 Host: example.com Accept: application/json { "name": "My source", "plugin": "Fake", "configuration": {} } **Example response**: .. sourcecode:: http HTTP/1.1 201 CREATED { "checks": [], "configuration": {}, "createdAt": "2019-01-21T14:08:22Z", "id": "e2c1c635-2d7c-4881-83d1-e4e7027ac7a2", "name": "My source", "plugin": "Fake", "updatedAt": "2019-01-21T14:08:22Z" } :resheader Content-Type: application/json :status 201: the created source """ if not TeamPermission.is_manager(team_id): abort(403) payload = get_payload() payload["team_id"] = team_id source = SourceController.create(payload) return jsonify(format_source(source, True)), 201
def put_rule(team_id, rule_id): """Edit an existing rule. .. :quickref: PUT; Edit an existing rule. **Example request**: .. sourcecode:: http PUT /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/rules/ff130e9b-d226-4465-9612-a93e12799091 HTTP/1.1 Host: example.com Accept: application/json { "name": "My edited rule" } **Example response**: .. sourcecode:: http HTTP/1.1 200 OK { "checks": [], "createdAt": "2018-05-17T12:01:09Z", "description": "Compute the QOS of our servers", "id": "ff130e9b-d226-4465-9612-a93e12799091", "name": "My edited rule", "updatedAt": "2018-11-09T15:33:06Z" } :resheader Content-Type: application/json :status 200: the edited rule """ if not TeamPermission.is_manager_or_editor(team_id): abort(403) payload = get_payload() rule = RuleController.update(payload, {"Rule": { "id": rule_id, "team_id": team_id }}) return jsonify(format_rule(rule)), 200
def list_check_variables(team_id, source_id, check_id): """ .. :quickref: GET; Lorem ipsum.""" if not TeamPermission.is_user(team_id): abort(403) variables = VariableController.list( filters={ "Variable": { "team_id": team_id, "rule_id": None, "source_id": source_id, "check_id": check_id, } }) return jsonify([format_variable(v) for v in variables]), 200