Exemplo n.º 1
0
def remove_peer_conf(user, data_age):
    peerq = """
        MATCH (peer_group:Node:Peering_Group)
        MATCH (peer_group)<-[r:Uses]-(peering_partner:Peering_Partner)
        WHERE (peer_group.noclook_auto_manage = true) OR (r.noclook_auto_manage = true)
        RETURN collect(distinct peer_group.handle_id) as peer_groups, collect(id(r)) as uses_relationships
        """
    peer_result = nc.query_to_dict(nc.graphdb.manager, peerq)

    for relationship_id in peer_result.get('uses_relationships', []):
        relationship = nc.get_relationship_model(nc.graphdb.manager,
                                                 relationship_id)
        if relationship:
            last_seen, expired = helpers.neo4j_data_age(
                relationship.data, data_age)
            if expired:
                rel_info = helpers.relationship_to_str(relationship)
                helpers.delete_relationship(user, relationship.id)
                logger.warning('Deleted relationship {rel_info}'.format(
                    rel_info=rel_info))
    for handle_id in peer_result.get('peer_groups', []):
        peer_group = nc.get_node_model(nc.graphdb.manager, handle_id)
        if peer_group:
            last_seen, expired = helpers.neo4j_data_age(
                peer_group.data, data_age)
            if expired:
                helpers.delete_node(user, peer_group.handle_id)
                logger.warning('Deleted node {name} ({handle_id}).'.format(
                    name=peer_group.data.get('name'), handle_id=handle_id))
Exemplo n.º 2
0
def remove_router_conf(user, data_age):
    routerq = """
        MATCH (router:Node:Router)
        OPTIONAL MATCH (router)-[:Has*1..]->(physical)<-[:Part_of]-(logical)
        WHERE (physical.noclook_auto_manage = true) OR (logical.noclook_auto_manage = true)
        RETURN collect(distinct physical.handle_id) as physical, collect(distinct logical.handle_id) as logical
        """
    router_result = nc.query_to_dict(nc.graphdb.manager, routerq)
    for handle_id in router_result.get('logical', []):
        logical = nc.get_node_model(nc.graphdb.manager, handle_id)
        if logical:
            last_seen, expired = helpers.neo4j_data_age(logical.data, data_age)
            if expired:
                helpers.delete_node(user, logical.handle_id)
                logger.warning('Deleted logical router: %s (%s).',
                               logical.data.get('name'), handle_id)
    for handle_id in router_result.get('physical', []):
        physical = nc.get_node_model(nc.graphdb.manager, handle_id)
        if physical:
            last_seen, expired = helpers.neo4j_data_age(
                physical.data, data_age)
            if expired:
                helpers.delete_node(user, physical.handle_id)
                logger.warning('Deleted physical router: %s (%s).',
                               physical.data.get('name'), handle_id)
Exemplo n.º 3
0
def cleanup_hardware_v1(router_node, user):
    p = r"^\d+/\d+/\d+$"
    bad_interfaces = re.compile(p)

    # Cleanup ni hardware info v1...
    # Get all ports that are not directly on router
    q = """
        MATCH (n:Router {handle_id: {handle_id}})-[:Has*1..3]->(:Node)-[r:Has]->(port:Port)
        RETURN port.handle_id as handle_id, port.name as name, id(r) as rel_id
        """
    ports = nc.query_to_list(nc.graphdb.manager,
                             q,
                             handle_id=router_node.handle_id)
    for port in ports:
        if bad_interfaces.match(port['name']):
            # delete it!
            helpers.delete_node(user, port['handle_id'])
        else:
            # move it to router
            helpers.set_has(user, router_node, port['handle_id'])
            # Remove from hardware info (pic)
            helpers.delete_relationship(user, port['rel_id'])
            # Scrub interface properties..?
    # Remove hardware info
    q = """
        MATCH (n:Router {handle_id: {handle_id}})-[:Has*]->(hw:Node)
        WHERE NOT hw:Port
        return hw.handle_id as handle_id, hw.name as name
        """
    old_hardware = nc.query_to_list(nc.graphdb.manager,
                                    q,
                                    handle_id=router_node.handle_id)
    for hw in old_hardware:
        helpers.delete_node(user, hw['handle_id'])
Exemplo n.º 4
0
def delete_outgoing_nodes(nodehandler, relation_name, user):
    node = nodehandler.get_node()
    relations = node.get_outgoing_relations()

    for relname, link_nodes in relations.items():
        if relname == relation_name:
            for link_node in link_nodes:
                link_node = link_node['node']
                helpers.delete_node(user, link_node.handle_id)
Exemplo n.º 5
0
def delete_node(node, dry_run):
    if not dry_run:
        user = utils.get_user()
        helpers.delete_node(user, node.handle_id)
    name = u"{}<name='{}', handle_id={}>".format(type(node).__name__,
                                                 node.data.get('name'),
                                                 node.handle_id)
    logger.info('Deleted node {}.'.format(name))
    delete_log.append(name)
Exemplo n.º 6
0
    def test_delete_node_utf8(self):
        nh = self.create_node(u'æøå-ftw', 'site', meta='Location')
        node = nh.get_node()

        self.assertEqual(u'æøå-ftw', nh.node_name)
        self.assertEqual(u'æøå-ftw', node.data.get('name'))

        helpers.delete_node(self.user, nh.handle_id)
        activities = actor_stream(self.user)
        self.assertEqual(1, len(activities))
        self.assertEqual(u'Site æøå-ftw', activities[0].data.get('noclook', {}).get('object_name'))
Exemplo n.º 7
0
def delete_node(request, slug, handle_id):
    """
    Removes the node and all its relationships.
    """
    redirect_url = '/{}'.format(slug)
    nh, node = helpers.get_nh_node(handle_id)
    if nh.node_type.get_label() == 'Unit':
        part_of = node.get_part_of()
        if part_of.get('Part_of'):
            redirect_url = helpers.get_node_url(part_of.get('Part_of')[0]['node'].handle_id)
    try:
        # Redirect to parent if deleted node was a child node
        parent = node.get_parent().get('Has', [])
        if parent:
            redirect_url = helpers.get_node_url(parent[0]['node'].handle_id)
    except AttributeError:
        pass
    helpers.delete_node(request.user, node.handle_id)
    return redirect(redirect_url)