Exemplo n.º 1
0
def test_object_filtering(user_client):
    create_from_file(user_client, 'europe.xls')
    user_client.post('/objects/object_filtering', data=filter_objects1)
    assert len(Node.visible_choices()) == 21
    assert len(Link.visible_choices()) == 20
    user_client.post('/objects/object_filtering', data=filter_objects2)
    assert len(Node.visible_choices()) == 12
    assert len(Link.visible_choices()) == 4
Exemplo n.º 2
0
def napalm_configuration():
    form = NapalmConfigurationForm(request.form)
    # update the list of available users / nodes by querying the database
    form.nodes.choices = Node.choices()
    if 'send' in request.form:
        # if user does not select file, browser also
        # submit a empty part without filename
        filename = request.files['file'].filename
        # retrieve the raw script: we will use it as-is or update it depending
        # on whether a Jinja2 template was uploaded by the user or not
        raw_script = request.form['raw_script']
        if 'file' in request.files and filename:
            if allowed_file(filename, 'netmiko'):
                filename = secure_filename(filename)
                filepath = join(app.config['UPLOAD_FOLDER'], filename)
                with open(filepath, 'r') as f:
                    parameters = load(f)
                template = Template(raw_script)
                script = template.render(**parameters)
            else:
                flash('file {}: format not allowed'.format(filename))
        else:
            script = raw_script
        nodes_info = []
        for name in form.data['nodes']:
            obj = get_obj(current_app, Node, name=name)
            nodes_info.append((obj.ip_address, obj.operating_system.lower()))
        napalm_task = NapalmConfigTask(script, current_user, nodes_info,
                                       **form.data)
        current_app.database.session.add(napalm_task)
        current_app.database.session.commit()
        return redirect(url_for('scheduling_blueprint.task_management'))
    return _render_template('napalm_configuration.html', form=form)
Exemplo n.º 3
0
def netmiko():
    form = NetmikoForm(request.form)
    # update the list of available nodes by querying the database
    form.nodes.choices = Node.choices()
    if 'send' in request.form or 'create_task' in request.form:
        # if user does not select file, browser also
        # submit a empty part without filename
        filename = request.files['file'].filename
        # retrieve the raw script: we will use it as-is or update it depending
        # on whether a Jinja2 template was uploaded by the user or not
        raw_script = request.form['raw_script']
        if 'file' in request.files and filename:
            if allowed_file(filename, 'netmiko'):
                filename = secure_filename(filename)
                filepath = join(app.config['UPLOAD_FOLDER'], filename)
                with open(filepath, 'r') as f:
                    parameters = load(f)
                template = Template(raw_script)
                script = template.render(**parameters)
            else:
                flash('file {}: format not allowed'.format(filename))
        else:
            script = raw_script
        # we have a list of hostnames, we convert it to a list of IP addresses
        # note: we have to use list of strings as we cannot pass actual objects
        # to an AP Scheduler job.
        node_ips = switch_properties(current_app, Node, form.data['nodes'],
                                     'name', 'ip_address')
        netmiko_task = NetmikoTask(script, current_user, node_ips, **form.data)
        current_app.database.session.add(netmiko_task)
        current_app.database.session.commit()
        return redirect(url_for('scheduling_blueprint.task_management'))
    return _render_template('netmiko.html', form=form)
Exemplo n.º 4
0
def napalm_getters():
    form = NapalmGettersForm(request.form)
    # update the list of available nodes by querying the database
    form.nodes.choices = Node.visible_choices()
    if 'create_task' in request.form:
        targets = get_targets(form.data['nodes'])
        napalm_task = NapalmGettersTask(current_user, targets, **form.data)
        db.session.add(napalm_task)
        db.session.commit()
    return render_template('napalm_getters.html', form=form)
Exemplo n.º 5
0
def workflows():
    scheduling_form = SchedulingForm(request.form)
    scheduling_form.nodes.choices = Node.choices()
    scheduling_form.pools.choices = Pool.choices()
    return render_template('workflow_management.html',
                           names=pretty_names,
                           fields=('name', 'description'),
                           workflows=Workflow.query.all(),
                           form=WorkflowCreationForm(request.form),
                           scheduling_form=scheduling_form)
Exemplo n.º 6
0
def view(view_type):
    add_node_form = AddNode(request.form)
    add_link_form = AddLink(request.form)
    all_nodes = Node.choices()
    add_link_form.source.choices = add_link_form.destination.choices = all_nodes
    view_options_form = ViewOptionsForm(request.form)
    google_earth_form = GoogleEarthForm(request.form)
    scheduling_form = SchedulingForm(request.form)
    scheduling_form.scripts.choices = Script.choices()
    scheduling_form.workflows.choices = Workflow.choices()
    labels = {'node': 'name', 'link': 'name'}
    if 'view_options' in request.form:
        # update labels
        labels = {
            'node': request.form['node_label'],
            'link': request.form['link_label']
        }
    # for the sake of better performances, the view defaults to markercluster
    # if there are more than 2000 nodes
    view = 'leaflet' if len(Node.query.all()) < 2000 else 'markercluster'
    if 'view' in request.form:
        view = request.form['view']
    # we clean the session's selected nodes
    session['selection'] = []
    # name to id
    name_to_id = {node.name: id for id, node in enumerate(Node.query.all())}
    return render_template(
        '{}_view.html'.format(view_type),
        filters=Filter.query.all(),
        view=view,
        scheduling_form=scheduling_form,
        view_options_form=view_options_form,
        google_earth_form=google_earth_form,
        add_node_form=add_node_form,
        add_link_form=add_link_form,
        labels=labels,
        names=pretty_names,
        subtypes=node_subtypes,
        name_to_id=name_to_id,
        node_table={
            obj: OrderedDict([
                (property, getattr(obj, property))
                for property in type_to_public_properties[obj.type]
            ])
            for obj in Node.query.all()
        },
        link_table={
            obj: OrderedDict([
                (property, getattr(obj, property))
                for property in type_to_public_properties[obj.type]
            ])
            for obj in Link.query.all()
        })
Exemplo n.º 7
0
def scripts():
    scheduling_form = SchedulingForm(request.form)
    scheduling_form.nodes.choices = Node.choices()
    scheduling_form.filters.choices = Filter.choices()
    return render_template(
        'script_management.html',
        fields=('name', 'type'),
        type_to_form={t: s(request.form)
                      for t, s in type_to_form.items()},
        names=pretty_names,
        scheduling_form=scheduling_form,
        scripts=Script.query.all())
Exemplo n.º 8
0
def napalm_configuration():
    form = NapalmConfigurationForm(request.form)
    # update the list of available nodes / script by querying the database
    form.nodes.choices = Node.visible_choices()
    form.script.choices = Script.choices()
    if 'create_task' in request.form:
        targets = get_targets(form.data['nodes'])
        task = NapalmConfigTask(current_user, targets, **form.data)
        db.session.add(task)
        db.session.commit()
        return redirect(url_for('scheduling_blueprint.task_management'))
    return render_template('napalm_configuration.html', form=form)
Exemplo n.º 9
0
def napalm_getters():
    form = NapalmGettersForm(request.form)
    # update the list of available users / nodes by querying the database
    form.nodes.choices = Node.choices()
    if 'query' in request.form:
        nodes_info = []
        for name in form.data['nodes']:
            obj = get_obj(current_app, Node, name=name)
            nodes_info.append((obj.ip_address, obj.operating_system.lower()))
        napalm_task = NapalmGettersTask(current_user, nodes_info, **form.data)
        current_app.database.session.add(napalm_task)
        current_app.database.session.commit()
    return _render_template('napalm_getters.html', form=form)