Fin Projet avant bonus

This commit is contained in:
2025-11-27 14:06:29 +01:00
parent 0b7d66777e
commit bd4bc7cb24
10 changed files with 1254 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
from flask import Flask,jsonify,request,abort, make_response
from flask import Flask,jsonify,request,abort, make_response,send_file
from werkzeug.exceptions import HTTPException
from nornir import InitNornir
import os
@@ -24,6 +24,7 @@ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
from services.devices import ( get_inventory, add_device,get_device_by_name,delete_device,get_device_interfaces,get_device_interfaces_ip,get_device_technical_info)
from services.config import (get_config_by_device,run_config_from_file_by_device)
from services.tasks import (run_show_commands_by_device,run_config_commands_by_device)
from services.snapshots import (get_snapshot_by_name,create_snapshot_by_device,get_snapshots_by_device)
init_nornir()
@app.route("/")
@@ -100,6 +101,32 @@ def get_config(device_name):
result = run_config_commands_by_device(device, commands=commands)
return jsonify(result=result, commands=commands)
@app.route("/devices/<device_name>/snapshots", methods=['GET','POST'])
def snapshot(device_name):
device = get_device_by_name(device_name)
print(device)
if request.method == 'GET':
data = get_snapshots_by_device(device)
return jsonify({
"result": True,
"data": data
})
if request.method == 'POST':
snapshot_path = create_snapshot_by_device(device)
return jsonify({
"result": True,
"data": {
"snapshot_path": snapshot_path
}
})
@app.route("/snapshots/<path:filename>", methods=['GET'])
def snapshot_by_name(filename):
if request.method == 'GET':
try:
return send_file('snapshots/'+filename)
except FileNotFoundError:
abort(make_response(jsonify(message="snapshot not found"), 404))
@app.errorhandler(HTTPException)
def handle_exception(e):