diff --git a/backend/app/proxmox_actions.py b/backend/app/proxmox_actions.py index 04ed96b..62ce267 100644 --- a/backend/app/proxmox_actions.py +++ b/backend/app/proxmox_actions.py @@ -1,14 +1,38 @@ -from .proxmox_client import _client, _wait_task from .config import settings +from .proxmox_client import _client, _wait_task + + +def _is_not_found(error: Exception) -> bool: + """Определяет ответ Proxmox об отсутствующем госте.""" + if getattr(error, "status_code", None) == 404: + return True + response = getattr(error, "response", None) + return getattr(response, "status_code", None) == 404 def delete_guest(guest_type: str, vmid: int) -> None: - """Удаляет VM или LXC и ожидает завершения задачи Proxmox.""" + """Удаляет VM или LXC и ожидает завершения задачи Proxmox. + + Если объект уже отсутствует в Proxmox, операция считается успешной: + это позволяет очищать осиротевшие записи панели после неудачного создания. + """ client = _client() node = settings.pve_node - if guest_type == "vm": - upid = client.nodes(node).qemu(vmid).delete() - else: - upid = client.nodes(node).lxc(vmid).delete() + endpoint = client.nodes(node).qemu(vmid) if guest_type == "vm" else client.nodes(node).lxc(vmid) + + try: + endpoint.status.current.get() + except Exception as error: + if _is_not_found(error): + return + raise + + try: + upid = endpoint.delete() + except Exception as error: + if _is_not_found(error): + return + raise + if upid: _wait_task(client, node, upid)