def objects(): links = Link.query.all() return _render_template('objects_overview.html', node_fields=node_public_properties, nodes=Node.query.all(), link_fields=link_public_properties, links=Link.query.all())
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)
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)
def tacacs_server(): if request.method == 'POST': tacacs_client = TACACSClient(request.form['ip_address'], request.form['port'], request.form['password'], request.form['timeout']) return _render_template('tacacs_server.html', form=TacacsServer(request.form))
def create_account(): if request.method == 'GET': form = CreateAccountForm(request.form) return _render_template('login/create_account.html', form=form) else: login_form = LoginForm(request.form) user = User(**request.form) db.session.add(user) db.session.commit() return redirect(url_for('users_blueprint.login'))
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)
def view(view_type): view_filter = lambda _: True labels = {'node': 'name', 'link': 'name'} if request.method == 'POST': # retrieve labels labels = { 'node': request.form['node_label'], 'link': request.form['link_label'] } def view_filter(obj): # if the property field is not empty in the form, and the # property is a public property, we check that the value of # the object matches the user input for all properties return all( # if the node-regex property is not in the request, the # regex box is unticked and we only check that the values # are equal. str(value) == request.form[obj.class_type + property] if not obj.class_type + property + 'regex' in request.form # if it is ticked, we use re.search to check that the value # of the node property matches the regular expression. else search(request.form[obj.class_type + property], str(value)) for property, value in obj.__dict__.items() # we consider only public properties if property in obj.get_properties() # providing that the property field in the form is not empty # (empty field <==> property ignored) and request.form[obj.class_type + property]) return _render_template( '{}_view.html'.format(view_type), form=FilteringForm(request.form), labels=labels, node_table={ obj: OrderedDict([(property, getattr(obj, property)) for property in type_to_public_properties[obj.type]]) for obj in filter(view_filter, Node.query.all()) }, link_table={ obj: OrderedDict([(property, getattr(obj, property)) for property in type_to_public_properties[obj.type]]) for obj in filter(view_filter, Link.query.all()) })
def manage_users(): add_user_form = AddUser(request.form) delete_user_form = DeleteUser(request.form) if 'add_user' in request.form: user = User(**request.form) current_app.database.session.add(user) elif 'delete_user' in request.form: selection = delete_user_form.data['users'] current_app.database.session.query(User).filter(User.username.in_(selection))\ .delete(synchronize_session='fetch') if request.method == 'POST': current_app.database.session.commit() all_users = User.choices() delete_user_form.users.choices = all_users return _render_template('manage_users.html', add_user_form=add_user_form, delete_user_form=delete_user_form)
def login(): if request.method == 'POST': username, password = str(request.form['username']), str( request.form['password']) user = db.session.query(User).filter_by(username=username).first() if user and password == user.password: flask_login.login_user(user) return redirect(url_for('base_blueprint.dashboard')) elif tacacs_client.authenticate(username, password, TAC_PLUS_AUTHEN_TYPE_ASCII).valid: user = User(username=username, password=password) db.session.add(user) db.session.commit() flask_login.login_user(user) return redirect(url_for('base_blueprint.dashboard')) return render_template('errors/page_403.html') if not flask_login.current_user.is_authenticated: login_form = LoginForm(request.form) return _render_template('login/login.html', login_form=login_form) return redirect(url_for('base_blueprint.dashboard'))
def create_objects(): add_node_form = AddNode(request.form) add_nodes_form = AddNodes(request.form) add_link_form = AddLink(request.form) if 'add_node' in request.form: # print(request.form, request.form['type']) node = node_class[request.form['type']](**request.form) current_app.database.session.add(node) elif 'add_nodes' in request.form: filename = request.files['file'].filename if 'file' in request.files and allowed_file(filename, 'nodes'): filename = secure_filename(filename) filepath = join(current_app.config['UPLOAD_FOLDER'], filename) request.files['file'].save(filepath) book = open_workbook(filepath) for obj_type, cls in object_class.items(): try: sheet = book.sheet_by_name(obj_type) # if the sheet cannot be found, there's nothing to import except XLRDError: continue properties = sheet.row_values(0) for row_index in range(1, sheet.nrows): kwargs = dict(zip(properties, sheet.row_values(row_index))) if obj_type in link_class: source = current_app.database.session.query(Node)\ .filter_by(name=kwargs.pop('source'))\ .first() destination = current_app.database.session.query(Node)\ .filter_by(name=kwargs.pop('destination'))\ .first() new_obj = link_class[obj_type]( source_id=source.id, destination_id=destination.id, source=source, destination=destination, **kwargs) else: new_obj = node_class[obj_type](**kwargs) current_app.database.session.add(new_obj) else: flash('no file submitted') elif 'add_link' in request.form: source = current_app.database.session.query(Node)\ .filter_by(name=request.form['source'])\ .first() destination = current_app.database.session.query(Node)\ .filter_by(name=request.form['destination'])\ .first() new_link = EthernetLink(source_id=source.id, destination_id=destination.id, source=source, destination=destination) current_app.database.session.add(new_link) if request.method == 'POST': current_app.database.session.commit() all_nodes = Node.choices() add_link_form.source.choices = add_link_form.destination.choices = all_nodes return _render_template('create_object.html', add_node_form=add_node_form, add_nodes_form=add_nodes_form, add_link_form=add_link_form)
def users(): return _render_template('users_overview.html', fields=user_search_properties, users=User.query.all())