|
|
@@ -1,10 +1,10 @@
|
|
|
from fastapi import FastAPI
|
|
|
-# import libvirt
|
|
|
+import libvirt
|
|
|
|
|
|
app = FastAPI(title="VirtManager API", description="API for managing virtual machines")
|
|
|
|
|
|
# Connect to libvirt
|
|
|
-# conn = libvirt.open('qemu:///system')
|
|
|
+conn = libvirt.open('qemu:///system')
|
|
|
|
|
|
@app.get("/")
|
|
|
async def root():
|
|
|
@@ -12,18 +12,17 @@ async def root():
|
|
|
|
|
|
@app.get("/vms")
|
|
|
async def list_vms():
|
|
|
- # domains = conn.listAllDomains()
|
|
|
- # vms = []
|
|
|
- # for domain in domains:
|
|
|
- # vms.append({
|
|
|
- # "id": domain.ID(),
|
|
|
- # "name": domain.name(),
|
|
|
- # "state": domain.state()[0]
|
|
|
- # })
|
|
|
- # return {"vms": vms}
|
|
|
- return {"vms": []} # Mock for now
|
|
|
+ domains = conn.listAllDomains()
|
|
|
+ vms = []
|
|
|
+ for domain in domains:
|
|
|
+ vms.append({
|
|
|
+ "id": domain.ID(),
|
|
|
+ "name": domain.name(),
|
|
|
+ "state": domain.state()[0]
|
|
|
+ })
|
|
|
+ return {"vms": vms}
|
|
|
|
|
|
-@app.post("/vms/{vm_id}/start")
|
|
|
+@app.get("/vms/{vm_id}/start")
|
|
|
async def start_vm(vm_id: int):
|
|
|
# try:
|
|
|
# domain = conn.lookupByID(vm_id)
|
|
|
@@ -33,7 +32,7 @@ async def start_vm(vm_id: int):
|
|
|
# return {"error": str(e)}
|
|
|
return {"message": f"VM {vm_id} started (mock)"}
|
|
|
|
|
|
-@app.post("/vms/{vm_id}/stop")
|
|
|
+@app.get("/vms/{vm_id}/stop")
|
|
|
async def stop_vm(vm_id: int):
|
|
|
# try:
|
|
|
# domain = conn.lookupByID(vm_id)
|