Просмотр исходного кода

Api funcionando con listar y start y stop solo mock

Celestino Rey 7 месяцев назад
Родитель
Сommit
45033affaf
2 измененных файлов с 16 добавлено и 14 удалено
  1. 13 14
      app/main.py
  2. 3 0
      requirements.txt

+ 13 - 14
app/main.py

@@ -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)

+ 3 - 0
requirements.txt

@@ -0,0 +1,3 @@
+fastapi
+uvicorn[standard]
+libvirt-python