def objects_management(): 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 if request.method == 'POST': file = request.files['file'] if allowed_file(secure_filename(file.filename), {'xls', 'xlsx'}): book = open_workbook(file_contents=file.read()) 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))) kwargs['type'] = obj_type object_factory(**kwargs) db.session.commit() return render_template( 'object_management.html', names=pretty_names, node_fields=node_public_properties, nodes=Node.serialize(), link_fields=link_public_properties, links=Link.serialize(), add_node_form=add_node_form, add_link_form=add_link_form )
def create_script(script_type): script = get_obj(Script, name=request.form['name']) if script: script_factory(script_type, **request.form) db.session.commit() elif script_type in ('netmiko_config', 'napalm_config'): # retrieve the raw script: we will use it as-is or update it # depending on the type of script (jinja2-enabled template or not) real_content = request.form['content'] if request.form['content_type'] != 'simple': file = request.files['file'] filename = secure_filename(file.filename) if allowed_file(filename, {'yaml', 'yml'}): parameters = load(file.read()) template = Template(real_content) real_content = template.render(**parameters) script = { 'netmiko_config': NetmikoConfigScript, 'napalm_config': NapalmConfigScript }[script_type](real_content, **request.form) elif script_type == 'file_transfer': source_file_name = request.form['source_file'] source_file_path = join(current_app.path, 'file_transfer', source_file_name) script = FileTransferScript(source_file_path, **request.form) else: script = { 'ansible_playbook': AnsibleScript, 'napalm_getters': NapalmGettersScript, 'netmiko_validation': NetmikoValidationScript }[script_type](**request.form) db.session.add(script) db.session.commit() return jsonify({})
def create_script(script_type): script = get_obj(Script, name=request.form['name']) # convert ImmutableMultiDict to an actual dictionnary form = dict(request.form) if not script: if script_type in ('netmiko_config', 'napalm_config'): if form['content_type'][0] != 'simple': file = request.files['file'] filename = secure_filename(file.filename) if allowed_file(filename, {'yaml', 'yml'}): parameters = yaml_load(file.read()) template = Template(form['content'][0]) form['content'] = [''.join(template.render(**parameters))] elif script_type == 'file_transfer': source_file_name = form['source_file'][0] source_file_path = join( current_app.path, 'file_transfer', source_file_name ) form['source_file'] = [source_file_path] script = script_factory(script_type, **form) db.session.add(script) db.session.commit() return jsonify(script.serialized)
def import_topology(): objects, file = defaultdict(list), request.files['file'] if allowed_file(secure_filename(file.filename), {'xls', 'xlsx'}): book = open_workbook(file_contents=file.read()) for object_type in ('Device', 'Link'): try: sheet = book.sheet_by_name(object_type) except XLRDError: continue properties = sheet.row_values(0) for row_index in range(1, sheet.nrows): values = dict(zip(properties, sheet.row_values(row_index))) cls, kwargs = process_kwargs(app, **values) objects[object_type].append(factory(cls, **kwargs).serialized) db.session.commit() return jsonify(objects)
def create_script(script_type): script = retrieve(Script, name=request.form['name']) form = dict(request.form.to_dict()) form['getters'] = request.form.getlist('getters') if not script: if script_type in ('netmiko_config', 'napalm_config'): if form['content_type'] != 'simple': file = request.files['file'] filename = secure_filename(file.filename) if allowed_file(filename, {'yaml', 'yml'}): parameters = yaml_load(file.read()) template = Template(form['content']) form['content'] = ''.join(template.render(**parameters)) elif script_type == 'file_transfer': source_file_name = form['source_file'] source_file_path = join(current_app.path, 'file_transfer', source_file_name) form['source_file'] = source_file_path return jsonify(factory(type_to_class[script_type], **form).serialized)