1+ from rest_framework .views import APIView
2+ from rest_framework .response import Response
13from django .shortcuts import render
24from django .views .generic import TemplateView , UpdateView , DeleteView
35from django .urls import reverse_lazy
46from backend .views .utils import AdminRequired
57from backend .DAL .DAO .apiDAO import *
8+ from backend .DAL .DAO .logDAO import *
69from backend .DAL .models .api import ApiKey
10+ from backend .integrations .ansible import *
11+
12+
713
814class ApiPageView (AdminRequired , TemplateView ):
915 template_name = 'api.html'
@@ -20,6 +26,15 @@ def post(self, request ,*args, **kwargs):
2026 title = request .POST .get ('title' ),
2127 )
2228 result = "The key have been successfully upgraded."
29+ createLog (
30+ user = f"{ request .user } " ,
31+ date = datetime .now ().strftime ("%d-%m-%Y" ),
32+ hour = datetime .now ().strftime ("%H:%M:%S" ),
33+ switch = "Unused function" ,
34+ playbook = "Unused function" ,
35+ host = "Unused function" ,
36+ output = "The user generated an API access key." ,
37+ )
2338 return self .render_to_response (self .get_context_data (result = result ))
2439
2540class ApiDeleteView (AdminRequired , DeleteView ):
@@ -31,4 +46,28 @@ def get_context_data(self, **kwargs):
3146 context = super ().get_context_data (** kwargs )
3247 context ['settings' ] = 1
3348 context ['page' ] = "Delete Key"
34- return context
49+ return context
50+
51+
52+ class ApiResponseView (APIView ):
53+
54+ def execute_command (self , playbook , host , switch , username , password ):
55+ # Adicionar verificações de segurança aqui
56+ try :
57+ self .ansible .write_ansible_playbook (string = playbook , switch = str (switch ))
58+ self .ansible .write_ansible_host (string = host , switch = str (switch ), username = username , password = password )
59+ output = self .ansible .run_ansible ()
60+ return output
61+ except :
62+ return "Error when trying to run ansible"
63+
64+ def post (self , request , format = None ):
65+ data = request .data
66+ key = searchApi (data ['key' ])
67+ if key == False :
68+ return Response ("Key not found, access denied" )
69+ try :
70+ output = execute_command (playbook = data ['command' ], host = data ['host' ], switch = data ['switch' ], username = data ['username' ], password = data ['password' ])
71+ return Response (output )
72+ except :
73+ return Response ("Please verify the variables used, remembering that the following fields are required: key, command, host, switch, username, password." )
0 commit comments