255 lines
11 KiB
Python
255 lines
11 KiB
Python
from fastapi import FastAPI,Depends
|
|
import mysql.connector
|
|
from datetime import datetime
|
|
import Classes.main_classes as CL ## Importiert Classes
|
|
import uvicorn
|
|
import logging
|
|
import auth
|
|
from fastapi.security.api_key import APIKey
|
|
|
|
logging.basicConfig(level=logging.DEBUG) # add this line
|
|
logger = logging.getLogger("foo")
|
|
|
|
## uvicorn.exe main:app --reload Startet den API-Server
|
|
|
|
def database(query,name,user):
|
|
|
|
mydb = mysql.connector.connect(
|
|
host="172.17.1.21",
|
|
port=3306,
|
|
user="root",
|
|
password="N53yBCswuawzBzS445VNAhWVMs3N59Gb9szEsrzXRBzarDqpdETpQeyt5v5CGe",
|
|
database="" + name,
|
|
auth_plugin='mysql_native_password',
|
|
)
|
|
mydb.connect()
|
|
cursor = mydb.cursor()
|
|
if "SELECT" in query:
|
|
if user:
|
|
cursor.execute(query, user)
|
|
return cursor.fetchone()
|
|
else:
|
|
cursor.execute(query, user)
|
|
return cursor.fetchall()
|
|
if "SHOW" in query:
|
|
cursor.execute(query)
|
|
return cursor.fetchall()
|
|
if "INSERT" in query:
|
|
cursor.execute(query,user)
|
|
mydb.commit()
|
|
if "UPDATE" in query:
|
|
cursor.execute(query,user)
|
|
mydb.commit()
|
|
mydb.close()
|
|
|
|
app = FastAPI()
|
|
|
|
@app.get("/")
|
|
async def notify(api_key: APIKey = Depends(auth.get_api_key)):
|
|
return database(f"SELECT * FROM Notifications", "Stines-GmbH","")
|
|
|
|
@app.get("/gettime")
|
|
async def gettime(api_key: APIKey = Depends(auth.get_api_key)):
|
|
return f"Current Time from API-Server http://api.stines.de is {datetime.now()}"
|
|
|
|
@app.post("/ping")
|
|
async def ping(ping: CL.CL_Ping_Set,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"INSERT INTO `Ping-Server` VALUES ('{datetime.now()}','{ping.name}','{ping.ipaddress}')","Stines-GmbH","")
|
|
return f"ADD Ping from {ping.name} to Server"
|
|
|
|
## Get Database from Server
|
|
@app.post("/dbget")
|
|
async def database_get(get: CL.CL_Database_get,api_key: APIKey = Depends(auth.get_api_key)):
|
|
get_database = database("SHOW DATABASES","","")
|
|
if get.name in f"({get_database},)":
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
@app.post("/info/getvolume")
|
|
async def getvolume(get: CL.CL_GET_VOLUMES,api_key: APIKey = Depends(auth.get_api_key)):
|
|
## Anpassung zum neune Schema erstellen
|
|
result = database(f"SELECT `Netzlaufwerk_1`,`Netzlaufwerk_2`,`Netzlaufwerk_3`,`Netzlaufwerk_4`,`Netzlaufwerk_5`,`Netzlaufwerk_6`,`Netzlaufwerk_7`,`Netzlaufwerk_8`,`Netzlaufwerk_9`,`Netzlaufwerk_10` FROM `Clients` where `Name` = '{get.name}'",f"{get.database}","")
|
|
volume = []
|
|
if not result:
|
|
return False
|
|
else:
|
|
for i in result:
|
|
volume.append(i)
|
|
return volume
|
|
|
|
@app.post("/info/getuser")
|
|
async def getuser(get: CL.CL_GET_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT `disabled` FROM `Active-Directory-RDS-User` where `SamAccountName` = '{get.user}'",f"{get.database}","")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/info/gethost")
|
|
async def gethostname(get: CL.CL_GET_HOST,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT id FROM `hardware` where `hostname` = '{get.hostname}'",f"{get.dbname}","")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/info/sethost")
|
|
async def sethostname(get: CL.CL_SET_HOST,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"INSERT INTO `{get.dbname}`.hardware (hostname,ipadresse,ram,hddused,hddfree,hddtotal) VALUES ('{get.hostname}','{get.ipadresse}','{get.ram}','{get.hddused}','{get.hddfree}','{get.hddtotal}')","","")
|
|
result = database(
|
|
f"SELECT id FROM `{get.dbname}`.hardware WHERE hostname = '{get.hostname}')",
|
|
"", "")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/info/sethost_user")
|
|
async def sethost_user(get: CL.CL_SET_HOST_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"INSERT INTO `{get.dbname}`.user (user) VALUES ('{get.user}')","","")
|
|
result = database(f"SELECT id FROM `{get.dbname}`.user WHERE user = '{get.user}'","","")
|
|
database(f"INSERT INTO `{get.dbname}`.hardware_to_user (userID,hardwareID) VALUES ('{result[0][0]}',{get.hardwareID})", "", "")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result
|
|
|
|
@app.post("/info/gethost_user")
|
|
async def gethost_user(get: CL.CL_GET_HOST_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT id FROM `{get.dbname}`.user WHERE user = '{get.user}'","","")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/info/setlastlogon_user")
|
|
async def setlastlogon_user(get: CL.CL_SET_LASTLOGON_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"INSERT INTO `{get.dbname}`.lastlogon (lastlogon) VALUES ('{get.lastlogon}')","","")
|
|
lastlogonid = database(f"SELECT id FROM `{get.dbname}`.lastlogon WHERE lastlogon = '{get.lastlogon}'","","")
|
|
result = database(f"INSERT INTO `{get.dbname}`.lastlogon_to_user (userID,lastlogonID) VALUES ('{get.userID}','{lastlogonid[0][0]}')", "", "")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
|
|
@app.post("/info/getclient")
|
|
async def getclient(get: CL.CL_GET_CLIENT,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT Firma FROM `Kunden-Clients` where `Client-Name` = '{get.name}'","Stines-GmbH","")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/info/addclient")
|
|
async def addclient(addclient: CL.CL_ADD_CLIENT,api_key: APIKey = Depends(auth.get_api_key)):
|
|
print(addclient.query)
|
|
database(addclient.query,addclient.database,"")
|
|
return f"Client wurde zur Datenbank hinzugefügt"
|
|
|
|
|
|
@app.post("/info/getdatabase")
|
|
async def getdatabase(get: CL.CL_GET_DATABASE,api_key: APIKey = Depends(auth.get_api_key)):
|
|
return database(f"SELECT name FROM `Kunden-Server` where `IP-Adresse` = '{get.ip}'","Stines-GmbH","")[0][0]
|
|
|
|
@app.post("/dbset")
|
|
async def dbset(get: CL.CL_Database_set,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(get.query,get.dbname,"")
|
|
return f"Created {get.dbname}"
|
|
|
|
@app.post("/cpu_info")
|
|
async def cpu_info(get: CL.CL_CPUinfo,api_key: APIKey = Depends(auth.get_api_key)):
|
|
return database(f"SELECT `Prozessor-Anzahl` FROM `CPU-Liste` WHERE `CPU-Name`='{get.name}'","Stines-GmbH","")
|
|
|
|
@app.post("/systeminfo_set")
|
|
async def systeminfo_set(get: CL.CL_Systeminfo_set,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"UPDATE `Kunden-Server` SET RAM={get.ram}, Prozessor={get.prozessor}, CPU='{get.cpu}',single_free_hdd='{get.single_free_hdd}',single_used_hdd='{get.single_used_hdd}',single_total_hdd='{get.single_total_hdd}',total_free_hdd='{get.total_free_hdd}',total_used_hdd='{get.total_used_hdd}',total_total_hdd='{get.total_total_hdd}' WHERE `IP-Adresse`='{get.ipaddress}'","Stines-GmbH","")
|
|
return f"Server was UPDATE RAM={get.ram}, Prozessor={get.prozessor}, CPU={get.cpu}, IP-Adresse={get.ipaddress}"
|
|
|
|
@app.post("/systeminfo_get")
|
|
async def systeminfo_set(get: CL.CL_Systeminfo_get,api_key: APIKey = Depends(auth.get_api_key)):
|
|
return database("SELECT * FROM `Stines-GmbH`.`Kunden-Server` WHERE `IP-Adresse` = ""'{}'""".format(get.ipaddress),"","")
|
|
|
|
@app.post("/ad/getuserid")
|
|
async def getuser(get: CL.CL_GET_AD_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(get.query,get.dbname,"")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/ad/getuser")
|
|
async def getuser(get: CL.CL_GET_AD_GROUP_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(get.query,get.dbname,"")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/ad/adduser")
|
|
async def add_ad_user(get: CL.CL_ADD_AD_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
print(database(get.query,get.dbname,get.entry))
|
|
return f"Create {get.query} - {get.dbname}"
|
|
|
|
@app.post("/ad/reportadduser")
|
|
async def add_report_user(get: CL.CL_ADD_REPORT_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
print(database(get.query,get.dbname,""))
|
|
return f"Create {get.query} - {get.dbname}"
|
|
|
|
@app.post("/report/getgroup")
|
|
async def get_report_group(get: CL.CL_GET_REPORT_GROUP,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT ID FROM `{get.dbname}`.`Gruppen` WHERE Name = '{get.group_name}'","","")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/report/getuserid")
|
|
async def get_user_id(get: CL.CL_GET_USER_ID,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT ID FROM `{get.dbname}`.`User` WHERE SamAccountName = '{get.user}'","","")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/report/addusertogroup")
|
|
async def get_addusertogroup(get: CL.CL_ADD_USER_TO_GROUP,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"INSERT INTO `{get.dbname}`.GruppenToUser (UserID,GruppenID) VALUES ({get.userID},{get.groupID})","","")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/report/addgroup")
|
|
async def get_addreport_group(get: CL.CL_ADD_REPORT_GROUP,api_key: APIKey = Depends(auth.get_api_key)):
|
|
print(database(get.query,get.dbname,""))
|
|
return f"Create {get.query} - {get.dbname}"
|
|
|
|
@app.post("/ad/addgroup")
|
|
async def add_ad_user(get: CL.CL_ADD_AD_GROUP,api_key: APIKey = Depends(auth.get_api_key)):
|
|
print(database(get.query,get.dbname,get.entry))
|
|
return f"Create {get.query} - {get.dbname}"
|
|
|
|
@app.post("/ex/adduser")
|
|
async def add_ad_user(get: CL.CL_ADD_EX_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
print(database(get.query,get.dbname,get.entry))
|
|
return f"Create {get.query} - {get.dbname}"
|
|
|
|
|
|
@app.post("/checkdisabledduser")
|
|
async def disabled_user(get: CL.CL_CHECK_DISABLED_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(
|
|
f"SELECT `{get.dbname}`.`Gruppen`.`Name` ,`{get.dbname}`.`GruppenToUser`.`Deaktiviert` from `{get.dbname}`.`User` INNER join `{get.dbname}`.`GruppenToUser` on `{get.dbname}`.`User`.`ID` = `{get.dbname}`.`GruppenToUser`.`UserID` INNER join`{get.dbname}`.`Gruppen` on `{get.dbname}`.`Gruppen`.`ID` = `{get.dbname}`.`GruppenToUser`.`GruppenID` WHERE `{get.dbname}`.`User`.`SamAccountName` = '{get.user}' AND `{get.dbname}`.`GruppenToUser`.`Deaktiviert` LIKE '%{datetime.now().strftime('%Y-%m-%d')}%'",f"{get.dbname}","")
|
|
print(f"SELECT `{get.dbname}`.`Gruppen`.`Name` ,`{get.dbname}`.`GruppenToUser`.`Deaktiviert` from `{get.dbname}`.`User` INNER join `{get.dbname}`.`GruppenToUser` on `{get.dbname}`.`User`.`ID` = `{get.dbname}`.`GruppenToUser`.`UserID` INNER join`{get.dbname}`.`Gruppen` on `{get.dbname}`.`Gruppen`.`ID` = `{get.dbname}`.`GruppenToUser`.`GruppenID` WHERE `{get.dbname}`.`User`.`SamAccountName` = '{get.user}' AND `{get.dbname}`.`GruppenToUser`.`Deaktiviert` LIKE '%{datetime.now().strftime('%Y-%m-%d')}%'")
|
|
print(result)
|
|
return result
|
|
|
|
@app.post("/volume_mount")
|
|
async def volume_mount(get: CL.CL_GET_VOLUME,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"INSERT INTO `Volume-Mount` (date,user) VALUES ('{get.date}','{get.user}')","Stines-GmbH","")
|
|
return f"User {get.user} ADD"
|
|
|
|
## Startet den API-Server
|
|
if __name__ == '__main__':
|
|
uvicorn.run("main:app", host='0.0.0.0', port=8001, reload=True, log_level="debug") |