Beispiel #1
0
def test_build_query_nodes(app):
    with app.app_context():
        query = DependenciesController()._build_query_nodes(topic='acme',
                                                            label='Foo')
    assert query == "MATCH (n:acme_Foo) WITH n RETURN n.name"

    with app.app_context():
        query = DependenciesController()._build_query_nodes(topic='acme',
                                                            label='Foo',
                                                            random=True)
    assert query == ("MATCH (n:acme_Foo) WITH n"
                     ", rand() as r ORDER BY r "
                     "RETURN n.name")

    with app.app_context():
        query = DependenciesController()._build_query_nodes(topic='acme',
                                                            label='Foo',
                                                            random=True,
                                                            name="bar")
    assert query == ("MATCH (n:acme_Foo) WITH n"
                     ", rand() as r ORDER BY r "
                     "WHERE n.name CONTAINS 'bar' "
                     "RETURN n.name")

    with app.app_context():
        query = DependenciesController()._build_query_nodes(topic='acme',
                                                            label='Foo',
                                                            random=True,
                                                            name="bar",
                                                            limit=1234)
    assert query == ("MATCH (n:acme_Foo) WITH n"
                     ", rand() as r ORDER BY r "
                     "WHERE n.name CONTAINS 'bar' "
                     "RETURN n.name "
                     "LIMIT 1234")
Beispiel #2
0
def test_are_path_elements_all_active(app):
    path_elements_mock_ko_invalid_node = {
        "nodes": [website01_node, server02_node],
        "relationships": [rel_web1_ser2_relationship]
    }

    path_elements_mock_ko_invalid_rel = {
        "nodes": [website02_node, server02_node],
        "relationships": [rel_web2_ser2_relationship]
    }

    path_elements_mock_ok = {
        "nodes": [website03_node, server02_node],
        "relationships": [rel_web3_ser2_relationship]
    }

    with app.app_context():
        bool_result = DependenciesController._are_path_elements_all_active(
            path_elements_mock_ko_invalid_node, 1566338400)
    assert bool_result is False

    with app.app_context():
        bool_result = DependenciesController._are_path_elements_all_active(
            path_elements_mock_ko_invalid_rel, 1566338400)
    assert bool_result is False

    with app.app_context():
        bool_result = DependenciesController._are_path_elements_all_active(
            path_elements_mock_ok, 1566338400)
    assert bool_result is True
Beispiel #3
0
def test_is_impacted_node_active(app):
    with app.app_context():
        bool_result = DependenciesController._is_impacted_node_active(
            impacted_node_data, 1566424800)
    assert bool_result is True

    with app.app_context():
        bool_result = DependenciesController._is_impacted_node_active(
            impacted_node_data, 1566252000)
    assert bool_result is False
Beispiel #4
0
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),
        ))
Beispiel #5
0
def test_build_dependencies_query_multiple_deps(method, app, create_team,
                                                create_rule, create_config):
    team_id = str(create_team('My team')['id'])
    create_rule('Servers', team_id)
    create_config(
        team_id, {
            'ServerA': {
                'qos': 'rule.Servers'
            },
            'ServerB': {
                'qos': 'rule.Servers'
            },
            'Cluster': {
                'qos': '{0}.AND[ServerA, ServerB]'.format(method)
            }
        })

    with app.app_context():
        query = DependenciesController()._build_dependencies_query(
            team_id=team_id,
            topic='acme',
            label='Cluster',
            node='cluster.ovh.net',
            filter_on_config=True)
    assert query == ("MATCH(n:acme_Cluster{name: 'cluster.ovh.net'}) "
                     "OPTIONAL MATCH (n)-[r]->(m) "
                     "WHERE 'acme_ServerA' IN LABELS(m) "
                     "OR 'acme_ServerB' IN LABELS(m) "
                     "RETURN n,r,m ORDER BY m.name LIMIT 10")
Beispiel #6
0
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))
Beispiel #7
0
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))
Beispiel #8
0
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))
Beispiel #9
0
def test_build_query_count_nodes(app):
    with app.app_context():
        query = DependenciesController()._build_query_count_nodes(
            topic='acme', labels=['Foo'])
    assert query == (
        "MATCH (n:acme_Foo) WITH 'Foo' AS Label, count(n) AS Count "
        "RETURN Label, Count ")

    with app.app_context():
        query = DependenciesController()._build_query_count_nodes(
            topic='acme', labels=['Foo', 'Bar', 'Baz'])
    assert query == (
        "MATCH (n:acme_Foo) WITH 'Foo' AS Label, count(n) AS Count "
        "RETURN Label, Count "
        "UNION MATCH (n:acme_Bar) WITH 'Bar' AS Label, count(n) AS Count "
        "RETURN Label, Count "
        "UNION MATCH (n:acme_Baz) WITH 'Baz' AS Label, count(n) AS Count "
        "RETURN Label, Count ")
Beispiel #10
0
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),
        )
    )
Beispiel #11
0
def test_build_dependencies_query_without_config(app, create_team, create_rule,
                                                 create_config):
    team_id = str(create_team('My team')['id'])

    with app.app_context():
        query = DependenciesController()._build_dependencies_query(
            team_id=team_id,
            topic='acme',
            label='Server',
            node='server.ovh.net',
            filter_on_config=False)
    assert query == ("MATCH(n:acme_Server{name: 'server.ovh.net'}) "
                     "OPTIONAL MATCH (n)-[r]->(m) "
                     "RETURN n,r,m ORDER BY m.name LIMIT 10")
Beispiel #12
0
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),
        ))
Beispiel #13
0
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),
        ))
Beispiel #14
0
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))
Beispiel #15
0
def test_build_impacted_nodes_queries(app):
    with app.app_context():
        query = DependenciesController()._build_impacted_nodes_queries(
            topic="acme",
            label="Server",
            node="server02",
            impacted_label="Website",
            skip=0,
            limit=25,
            count=False)
    assert query == "MATCH p = (n:acme_Website)-[*]->(:acme_Server{name: 'server02'}) " \
                    "WITH *, relationships(p) AS r_list WITH *, nodes(p) AS n_sub_list RETURN DISTINCT n " \
                    "AS impacted_node, collect({ relationships: r_list, nodes: n_sub_list }) " \
                    "AS all_path_elements ORDER BY n.name SKIP 0 LIMIT 25"

    with app.app_context():
        query = DependenciesController()._build_impacted_nodes_queries(
            topic="acme",
            label="Server",
            node="server02",
            impacted_label="Website",
            count=True)
    assert query == "MATCH (n:acme_Website)-[*]->(:acme_Server{name: 'server02'}) " \
                    "RETURN count(DISTINCT n) AS count"
Beispiel #16
0
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),
        ))
Beispiel #17
0
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})
Beispiel #18
0
def test_build_dependencies_query_with_impacted_nodes(app, create_team,
                                                      create_rule,
                                                      create_config):
    team_id = str(create_team('My team')['id'])
    create_rule('Servers', team_id)
    create_config(team_id, {'Server': {'qos': 'rule.Servers'}})

    with app.app_context():
        query = DependenciesController()._build_dependencies_query(
            team_id=team_id,
            topic='acme',
            label='Server',
            node='server.ovh.net',
            filter_on_config=True,
            impacted=True)
    assert query == ("MATCH(n:acme_Server{name: 'server.ovh.net'}) "
                     "OPTIONAL MATCH (n)<-[r]-(m) "
                     "RETURN n,r,m ORDER BY m.name LIMIT 10")
Beispiel #19
0
def get_label_nodes(team_id, label):
    """Get the nodes of a label.

    .. :quickref: GET; Get the nodes of a label.

    **Example request**:

    .. sourcecode:: http

      GET /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/labels/Website/nodes HTTP/1.1
      Host: example.com
      Accept: application/json

    **Example response**:

    .. sourcecode:: http

      HTTP/1.1 200 OK

      [
        "example-one.com",
        "example-two.com"
      ]

    :param name: Filter the list by name
    :param limit: Limit the number of results
    :param random: Return random items
    :resheader Content-Type: application/json
    :status 200: the list of nodes
    """
    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),
        )
    )
Beispiel #20
0
def get_impacted_nodes_count(team_id, label, node):
    """Count the total number of nodes impacted by a given node.

    .. :quickref: GET; Count the total number of nodes impacted by a given node.

    **Example request**:

    .. sourcecode:: http

      GET /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/labels/Website/nodes/example.com/impacted/count?impactedLabel=Offer HTTP/1.1
      Host: example.com
      Accept: application/json

    **Example response**:

    .. sourcecode:: http

      HTTP/1.1 200 OK

      {
        "count": 1
      }

    :param impactedLabel: impacted nodes for the given label
    :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_count(
            team_id, label, node, request.args.get("impactedLabel", None)
        )
    )
Beispiel #21
0
def test_compute_impacted_nodes_from_data(app):
    with app.app_context():
        computed_impacted_nodes = DependenciesController._compute_impacted_nodes_from_data(
            impacted_websites_by_server02, 1566424800, True)
    assert computed_impacted_nodes == [{
        'active': True,
        'name': 'website01',
        'to': None,
        'from': 1566424800
    }, {
        'active': False,
        'name': 'website02',
        'to': None,
        'from': 1566079200
    }, {
        'active': True,
        'name': 'website03',
        'to': None,
        'from': 1566079200
    }]

    with app.app_context():
        computed_impacted_nodes = DependenciesController._compute_impacted_nodes_from_data(
            impacted_websites_by_server02, 1566338400, True)
    assert computed_impacted_nodes == [{
        'active': False,
        'name': 'website01',
        'to': None,
        'from': 1566424800
    }, {
        'active': False,
        'name': 'website02',
        'to': None,
        'from': 1566079200
    }, {
        'active': True,
        'name': 'website03',
        'to': None,
        'from': 1566079200
    }]

    with app.app_context():
        computed_impacted_nodes = DependenciesController._compute_impacted_nodes_from_data(
            impacted_websites_by_server02, 1566252000, True)
    assert computed_impacted_nodes == [{
        'active': False,
        'name': 'website01',
        'to': None,
        'from': 1566424800
    }, {
        'active': True,
        'name': 'website02',
        'to': None,
        'from': 1566079200
    }, {
        'active': False,
        'name': 'website03',
        'to': None,
        'from': 1566079200
    }]

    with app.app_context():
        computed_impacted_nodes = DependenciesController._compute_impacted_nodes_from_data(
            impacted_websites_by_server02, 1566079200, True)
    assert computed_impacted_nodes == [{
        'active': False,
        'name': 'website01',
        'to': None,
        'from': 1566424800
    }, {
        'active': False,
        'name': 'website02',
        'to': None,
        'from': 1566079200
    }, {
        'active': False,
        'name': 'website03',
        'to': None,
        'from': 1566079200
    }]

    with app.app_context():
        computed_impacted_nodes = DependenciesController._compute_impacted_nodes_from_data(
            impacted_websites_by_server02, 1566424800, False)
    assert computed_impacted_nodes == [{
        'active': True,
        'name': 'website01',
        'to': None,
        'from': 1566424800
    }, {
        'active': True,
        'name': 'website03',
        'to': None,
        'from': 1566079200
    }]
Beispiel #22
0
def get_node_dependencies(team_id, label, node):
    """Get the dependencies of a node.

    .. :quickref: GET; Get the dependencies of a node.

    **Example request**:

    .. sourcecode:: http

      GET /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

      {
        "dependencies": {
          "Apache": [{
            "name": "apache1",
            "old": false,
            "periods": [0]
          }],
          "Filer": [{
            "name": "filer1",
            "old": false,
            "periods": [0]
          }],
          "Website": [{
              "name": "example.com"
           }]
        },
        "graph": {
          "nodes": [{
            "id": 8226314,
            "label": "example.com",
            "title": "Website"
          }, {
            "id": 8226318,
            "label": "apache1",
            "title": "Apache"
          }, {
            "id": 8226316,
            "label": "filer1",
            "title": "Filer"
          }],
          "relationships": [{
            "arrows": "to",
            "from": 8226314,
            "id": 13558159,
            "periods": [0],
            "to": 8226318
          }, {
            "arrows": "to",
            "from": 8226314,
            "id": 13558157,
            "periods": [0],
            "to": 8226316
          }]
        }
      }

    :param day: Filter by day (default is today)
    :param config: Only include labels used in configuration
    :param inactive: Include the inactive nodes
    :resheader Content-Type: application/json
    :status 200: the list of dependencies
    """
    if not TeamPermission.is_user(team_id):
        abort(403)

    return jsonify(
        DependenciesController.get_node_dependencies(
            team_id=team_id,
            label=label,
            node=node,
            day=request.args.get("day",
                                 arrow.utcnow().format("YYYY-MM-DD")),
            filter_on_config=request.args.get("config", False),
            include_inactive=request.args.get("inactive", False),
        ))