Fix vm_destroy: Handle Proxmox API response formats (list vs dict)
This commit is contained in:
parent
e79f7256b5
commit
3b37c639a2
1 changed files with 12 additions and 3 deletions
15
app.py
15
app.py
|
|
@ -381,15 +381,24 @@ async def vm_destroy_full(vmid: int, _=Depends(_verify), dry_run: bool = Query(F
|
|||
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:
|
||||
vm_info = r.json().get("data", {})
|
||||
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"]
|
||||
break
|
||||
|
||||
if not vm_info:
|
||||
return JSONResponse({"error": f"VM {vmid} not found"}, status_code=404)
|
||||
|
||||
hostname = vm_info.get("name", "")
|
||||
ip = vm_info.get("net0", "").split("=")[-1].split(",")[0] if "net0" in vm_info else ""
|
||||
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}
|
||||
|
||||
# Step 2: Stop VM (if running)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue