|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
|
|
|
from datetime import datetime, UTC
|
|
|
|
from typing import List
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
|
|
|
|
from fastapi import FastAPI, Depends, HTTPException
|
|
|
|
from fastapi import FastAPI, Depends, HTTPException
|
|
|
|
@ -20,7 +21,6 @@ def get_db():
|
|
|
|
@app.get("/hosts/", response_model=List[schemas.Host])
|
|
|
|
@app.get("/hosts/", response_model=List[schemas.Host])
|
|
|
|
def get_hosts(db: Session = Depends(get_db)):
|
|
|
|
def get_hosts(db: Session = Depends(get_db)):
|
|
|
|
hosts = crud.get_hosts(db)
|
|
|
|
hosts = crud.get_hosts(db)
|
|
|
|
|
|
|
|
|
|
|
|
return hosts
|
|
|
|
return hosts
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/get_host_by_hostname/{hostname}")
|
|
|
|
@app.get("/get_host_by_hostname/{hostname}")
|
|
|
|
@ -36,7 +36,6 @@ def create_host(host: schemas.HostCreate, db: Session = Depends(get_db)):
|
|
|
|
db_host = crud.get_host_by_hostname(db, hostname=host.hostname)
|
|
|
|
db_host = crud.get_host_by_hostname(db, hostname=host.hostname)
|
|
|
|
if db_host:
|
|
|
|
if db_host:
|
|
|
|
raise HTTPException(status_code=400, detail="Host already exists")
|
|
|
|
raise HTTPException(status_code=400, detail="Host already exists")
|
|
|
|
|
|
|
|
|
|
|
|
return crud.create_host(db=db, host=host)
|
|
|
|
return crud.create_host(db=db, host=host)
|
|
|
|
|
|
|
|
|
|
|
|
@app.delete("/delete_host/{host_id}")
|
|
|
|
@app.delete("/delete_host/{host_id}")
|
|
|
|
@ -47,15 +46,13 @@ def delete_host(host_id: int, db: Session = Depends(get_db)):
|
|
|
|
def delete_host_by_hostname(hostname: str, db: Session = Depends(get_db)):
|
|
|
|
def delete_host_by_hostname(hostname: str, db: Session = Depends(get_db)):
|
|
|
|
return crud.delete_host_by_hostname(db, hostname=hostname)
|
|
|
|
return crud.delete_host_by_hostname(db, hostname=hostname)
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/get_cpus_by_host_id/{host_id}")
|
|
|
|
@app.get("/get_cpus_by_host_id/{host_id}", response_model=List[schemas.CPU])
|
|
|
|
def get_cpus_by_host_id(host_id: int, db: Session = Depends(get_db)) -> List[schemas.CPU]:
|
|
|
|
def get_cpus_by_host_id(host_id: int, db: Session = Depends(get_db)) -> List[schemas.CPU]:
|
|
|
|
return crud.get_cpus_by_host_id(db, host_id=host_id)
|
|
|
|
return crud.get_cpus_by_host_id(db, host_id=host_id)
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/add_cpu/", response_model=schemas.CPUCreate, status_code=201)
|
|
|
|
@app.post("/add_cpu_to_host/", response_model=schemas.CPUCreate, status_code=201)
|
|
|
|
def create_cpu(cpu: schemas.CPUCreate, db: Session = Depends(get_db)):
|
|
|
|
def create_cpu(cpu: schemas.CPUCreate, db: Session = Depends(get_db)):
|
|
|
|
db_cpus = crud.get_cpus_by_host_id(db, host_id=cpu.host_id)
|
|
|
|
if cpu.time == datetime.fromtimestamp(0, UTC):
|
|
|
|
for db_cpu in db_cpus:
|
|
|
|
cpu.time = datetime.now()
|
|
|
|
if cpu.socket == db_cpu.socket:
|
|
|
|
|
|
|
|
raise HTTPException(status_code=400, detail="CPU already exists")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return crud.create_cpu(db, cpu=cpu)
|
|
|
|
return crud.create_cpu(db, cpu=cpu)
|
|
|
|
|