style: ruff format and the lint rules of the driver standard
Mechanical: ruff format, and ruff check --fix with the rule set every NAPALM driver uses (E, F, I, UP, B at line length 100, Python 3.10). What ruff cannot fix comes in the next commit.
This commit is contained in:
1 parent
1f6859de08
commit
3b873d91fe
27 files changed
+195
-225
No files matched your search
+53
-52
@@ -20,8 +20,6 @@ import logging
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from napalm_proxmox import utils
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_JsonDict = dict[str, Any]
|
||||
@@ -30,9 +28,7 @@ _JsonDict = dict[str, Any]
|
||||
class ProxmoxVMMixin:
|
||||
"""Mixin providing VM and container NAPALM methods."""
|
||||
|
||||
def get_vm_interfaces(
|
||||
self, vmid: int, vm_type: str
|
||||
) -> tuple[dict[str, _JsonDict], bool, bool]:
|
||||
def get_vm_interfaces(self, vmid: int, vm_type: str) -> tuple[dict[str, _JsonDict], bool, bool]:
|
||||
"""Return network interfaces for a single VM or LXC container.
|
||||
|
||||
Returns a 3-tuple ``(interfaces, agent_running, agent_enabled)``:
|
||||
@@ -105,7 +101,7 @@ class ProxmoxVMMixin:
|
||||
logger.debug("get_vm_interfaces: LXC %s config failed: %s", vmid, exc)
|
||||
|
||||
try:
|
||||
for iface in (self._node_api().lxc(vmid).interfaces.get() or []):
|
||||
for iface in self._node_api().lxc(vmid).interfaces.get() or []:
|
||||
name = iface.get("name", "")
|
||||
if not name or name == "lo":
|
||||
continue
|
||||
@@ -233,7 +229,7 @@ class ProxmoxVMMixin:
|
||||
|
||||
# QEMU VMs
|
||||
try:
|
||||
for vm in (self._node_api().qemu.get() or []):
|
||||
for vm in self._node_api().qemu.get() or []:
|
||||
vmid = int(vm.get("vmid", 0))
|
||||
name = vm.get("name", f"vm-{vmid}")
|
||||
status = vm.get("status", "unknown")
|
||||
@@ -256,30 +252,32 @@ class ProxmoxVMMixin:
|
||||
)
|
||||
|
||||
disks, onboot = self._get_vm_disk_and_boot(vmid, "qemu")
|
||||
result.append({
|
||||
"vmid": str(vmid),
|
||||
"name": name,
|
||||
"type": "vm",
|
||||
"status": status,
|
||||
"vcpus": vcpus,
|
||||
"memory": memory_mb,
|
||||
"cpu_usage": round(cpu_usage, 4),
|
||||
"memory_usage": memory_usage_mb,
|
||||
"uptime": uptime,
|
||||
"node": self._node_name,
|
||||
"interfaces": interfaces,
|
||||
"ipv4": ipv4,
|
||||
"agent_enabled": agent_enabled,
|
||||
"agent_running": agent_running,
|
||||
"disks": disks,
|
||||
"onboot": onboot,
|
||||
})
|
||||
result.append(
|
||||
{
|
||||
"vmid": str(vmid),
|
||||
"name": name,
|
||||
"type": "vm",
|
||||
"status": status,
|
||||
"vcpus": vcpus,
|
||||
"memory": memory_mb,
|
||||
"cpu_usage": round(cpu_usage, 4),
|
||||
"memory_usage": memory_usage_mb,
|
||||
"uptime": uptime,
|
||||
"node": self._node_name,
|
||||
"interfaces": interfaces,
|
||||
"ipv4": ipv4,
|
||||
"agent_enabled": agent_enabled,
|
||||
"agent_running": agent_running,
|
||||
"disks": disks,
|
||||
"onboot": onboot,
|
||||
}
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.warning("get_vms: failed to list QEMU VMs: %s", exc)
|
||||
|
||||
# LXC containers
|
||||
try:
|
||||
for ct in (self._node_api().lxc.get() or []):
|
||||
for ct in self._node_api().lxc.get() or []:
|
||||
vmid = int(ct.get("vmid", 0))
|
||||
name = ct.get("name", f"ct-{vmid}")
|
||||
status = ct.get("status", "unknown")
|
||||
@@ -300,24 +298,26 @@ class ProxmoxVMMixin:
|
||||
)
|
||||
|
||||
disks, onboot = self._get_vm_disk_and_boot(vmid, "lxc")
|
||||
result.append({
|
||||
"vmid": str(vmid),
|
||||
"name": name,
|
||||
"type": "container",
|
||||
"status": status,
|
||||
"vcpus": vcpus,
|
||||
"memory": memory_mb,
|
||||
"cpu_usage": round(cpu_usage, 4),
|
||||
"memory_usage": memory_usage_mb,
|
||||
"uptime": uptime,
|
||||
"node": self._node_name,
|
||||
"interfaces": interfaces,
|
||||
"ipv4": ipv4,
|
||||
"agent_enabled": agent_enabled,
|
||||
"agent_running": agent_running,
|
||||
"disks": disks,
|
||||
"onboot": onboot,
|
||||
})
|
||||
result.append(
|
||||
{
|
||||
"vmid": str(vmid),
|
||||
"name": name,
|
||||
"type": "container",
|
||||
"status": status,
|
||||
"vcpus": vcpus,
|
||||
"memory": memory_mb,
|
||||
"cpu_usage": round(cpu_usage, 4),
|
||||
"memory_usage": memory_usage_mb,
|
||||
"uptime": uptime,
|
||||
"node": self._node_name,
|
||||
"interfaces": interfaces,
|
||||
"ipv4": ipv4,
|
||||
"agent_enabled": agent_enabled,
|
||||
"agent_running": agent_running,
|
||||
"disks": disks,
|
||||
"onboot": onboot,
|
||||
}
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.warning("get_vms: failed to list LXC containers: %s", exc)
|
||||
|
||||
@@ -328,9 +328,7 @@ class ProxmoxVMMixin:
|
||||
# Disk-key prefix for LXC: rootfs, mp (mount points)
|
||||
_DISK_KEYS_CT = re.compile(r"^(rootfs|mp\d+)$")
|
||||
|
||||
def _get_vm_disk_and_boot(
|
||||
self, vmid: int, vm_type: str
|
||||
) -> tuple[list[dict], bool]:
|
||||
def _get_vm_disk_and_boot(self, vmid: int, vm_type: str) -> tuple[list[dict], bool]:
|
||||
"""Return (disks, onboot) for a VM or container.
|
||||
|
||||
Fetches config from /nodes/{node}/{qemu|lxc}/{vmid}/config.
|
||||
@@ -376,8 +374,8 @@ class ProxmoxVMMixin:
|
||||
logger.debug("Failed to fetch disk/boot config for vmid %s: %s", vmid, exc)
|
||||
return [], False
|
||||
|
||||
_POWER_ACTIONS_VM = {'start', 'stop', 'shutdown', 'reboot', 'reset'}
|
||||
_POWER_ACTIONS_CT = {'start', 'stop', 'shutdown', 'reboot'}
|
||||
_POWER_ACTIONS_VM = {"start", "stop", "shutdown", "reboot", "reset"}
|
||||
_POWER_ACTIONS_CT = {"start", "stop", "shutdown", "reboot"}
|
||||
|
||||
def power_vm(self, vmid: int, vm_type: str, action: str) -> dict:
|
||||
"""Send a power action to a VM or container on this node.
|
||||
@@ -385,11 +383,14 @@ class ProxmoxVMMixin:
|
||||
Supported actions for VMs: start, stop, shutdown, reboot, reset
|
||||
Supported actions for containers: start, stop, shutdown, reboot
|
||||
"""
|
||||
allowed = self._POWER_ACTIONS_VM if vm_type == 'vm' else self._POWER_ACTIONS_CT
|
||||
allowed = self._POWER_ACTIONS_VM if vm_type == "vm" else self._POWER_ACTIONS_CT
|
||||
if action not in allowed:
|
||||
return {"success": False, "error": f"Action '{action}' not supported for {vm_type} (allowed: {sorted(allowed)})"}
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Action '{action}' not supported for {vm_type} (allowed: {sorted(allowed)})",
|
||||
}
|
||||
try:
|
||||
vm_api = self._node_api().qemu(vmid) if vm_type == 'vm' else self._node_api().lxc(vmid)
|
||||
vm_api = self._node_api().qemu(vmid) if vm_type == "vm" else self._node_api().lxc(vmid)
|
||||
task_id = getattr(vm_api.status, action).post()
|
||||
return {"success": True, "task_id": task_id or ""}
|
||||
except Exception as exc:
|
||||
|
||||
Reference in new issue
Block a user