From 6f165ccb2ca8b8cc2a51e69a7b54d96ebb199e1c Mon Sep 17 00:00:00 2001 From: sascha Date: Wed, 22 Apr 2026 21:32:47 +0200 Subject: [PATCH] Fix vm_destroy: Use vm/list endpoint for reliable VM info + IP extraction --- app.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/app.py b/app.py index 7391bf3..456fbec 100644 --- a/app.py +++ b/app.py @@ -374,32 +374,33 @@ async def vm_destroy_full(vmid: int, _=Depends(_verify), dry_run: bool = Query(F results = {"vmid": vmid, "dry_run": dry_run, "steps": {}} async with httpx.AsyncClient(verify=False, timeout=30) as c: - # Step 1: Find VM and get details + # Step 1: Find VM and get details from vm/list (more reliable than config endpoint) vm_info = None node_name = None - nodes = await c.get("https://10.5.85.11:8006/api2/json/nodes", headers={"Authorization": auth}) - for n in nodes.json().get("data", []): - r = await c.get(f"https://10.5.85.11:8006/api2/json/nodes/{n['node']}/qemu/{vmid}", headers={"Authorization": auth}) - if r.status_code == 200: - data = r.json().get("data", {}) - # Proxmox returns config as dict, but status endpoint returns list - if isinstance(data, list) and len(data) > 0: - vm_info = data[0] - elif isinstance(data, dict): - vm_info = data - node_name = n["node"] + vms = await c.get("https://10.5.85.11:8006/api2/json/nodes", headers={"Authorization": auth}) + for n in vms.json().get("data", []): + r = await c.get(f"https://10.5.85.11:8006/api2/json/nodes/{n['node']}/qemu", headers={"Authorization": auth}) + for vm in r.json().get("data", []): + if vm.get("vmid") == vmid: + vm_info = vm + node_name = n["node"] + break + if vm_info: break if not vm_info: return JSONResponse({"error": f"VM {vmid} not found"}, status_code=404) - hostname = vm_info.get("name", "") or vm_info.get("vmid", "") - # Extract IP from net0 config (format: "virtio=AA:BB:CC:DD:EE:FF,bridge=vmbr0,ip=10.X.Y.Z") - net0 = vm_info.get("net0", "") - ip = "" - if "ip=" in net0: - ip = net0.split("ip=")[-1].split(",")[0] - results["vm_info"] = {"hostname": hostname, "ip": ip, "node": node_name} + hostname = vm_info.get("name", "") or str(vmid) + # Extract IP from vm list or config + ip = vm_info.get("ip", "") + if not ip: + # Try to get from net0 config + cfg = await c.get(f"https://10.5.85.11:8006/api2/json/nodes/{node_name}/qemu/{vmid}/config", headers={"Authorization": auth}) + net0 = cfg.json().get("data", {}).get("net0", "") + if "ip=" in net0: + ip = net0.split("ip=")[-1].split(",")[0] + results["vm_info"] = {"hostname": hostname, "ip": ip, "node": node_name, "status": vm_info.get("status", "unknown")} # Step 2: Stop VM (if running) if vm_info.get("status") == "running":