refactor(dhcp): move the Kea subnet lookup into kea_reservations
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 17s
driver / types (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m17s
driver / test (3.13) (pull_request) Successful in 1m12s
driver / lowest (pull_request) Successful in 36s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Successful in 1m7s
CI / driver (pull_request) Successful in 2m56s
driver / publish (pull_request) Skipped

create_dhcp_reservation and _kea_subnet_uuid_for each walked the
searchSubnet rows for the one holding an address. Both now call the
pure subnet_containing(), which brings create_dhcp_reservation under
the complexity limit and back below its recorded length after #40;
the quality baseline records the shrunk entries.
This commit is contained in:
Christian Manivong committed 2026-11-02 08:51:21 +01:00
1 parent 96a4a139d0
commit 6ac7a6f6a2
4 files changed
+55 -26

No files matched your search

+16
View File
@@ -7,6 +7,7 @@ edited by hand (#26), so every lookup here checks both.
from __future__ import annotations
from ipaddress import ip_address, ip_network
from typing import Any
from napalm_device_types.dhcp import normalize_mac
@@ -19,6 +20,21 @@ def _mac_of(row: dict[str, Any], *fields: str) -> str:
return normalize_mac(next((str(row[f]) for f in fields if row.get(f)), ""))
def subnet_containing(rows: list[dict[str, Any]], ip: str) -> dict[str, Any] | None:
"""The first ``searchSubnet`` row whose subnet holds *ip*; None when none does.
A row without a readable ``subnet`` is skipped.
"""
address = ip_address(ip)
for row in rows:
try:
if address in ip_network(str(row["subnet"]).strip(), strict=False):
return row
except (ValueError, KeyError):
continue
return None
def split_by_mac(rows: list[dict[str, Any]], mac: str) -> tuple[dict[str, Any] | None, str]:
"""``(the row *mac* holds, the MAC of a row another host holds)`` among *rows*.
+13 -22
View File
@@ -41,7 +41,6 @@ import logging
import re
import socket
import time
from ipaddress import ip_address, ip_network
from typing import Any
import requests
@@ -64,7 +63,12 @@ from requests.exceptions import RequestException
from napalm_opnsense.api_endpoint import ALIVE_TIMEOUT, api_endpoint
from napalm_opnsense.ha import config_sync, dhcp_ha
from napalm_opnsense.kea_reservations import lease_held, reservation_unchanged, split_by_mac
from napalm_opnsense.kea_reservations import (
lease_held,
reservation_unchanged,
split_by_mac,
subnet_containing,
)
from napalm_opnsense.listening import parse_socket_statistics, wireguard_listen_ports
from napalm_opnsense.ospf_mixin import OPNsenseOspfMixin
from napalm_opnsense.ping_mixin import OPNsensePingMixin
@@ -1462,19 +1466,10 @@ class OPNsenseDriver(
except Exception as exc:
raise RuntimeError(f"Kea DHCPv4 plugin unavailable: {exc}") from exc
ip_obj = ip_address(ip)
subnet_uuid = None
subnet_cidr = ""
for row in subnets:
try:
if ip_obj in ip_network(row["subnet"], strict=False):
subnet_uuid = row["uuid"]
subnet_cidr = str(row["subnet"]).strip()
break
except ValueError:
continue
if subnet_uuid is None:
subnet_row = subnet_containing(subnets, ip)
if subnet_row is None:
raise ValueError(f"No Kea-managed subnet contains {ip}")
subnet_uuid, subnet_cidr = subnet_row["uuid"], str(subnet_row["subnet"]).strip()
existing_row = self._kea_own_reservation(ip, mac)
@@ -1632,14 +1627,10 @@ class OPNsenseDriver(
return str(row["uuid"])
raise ValueError(f"No Kea-managed subnet matches {subnet}")
ip_obj = ip_address(ip)
for row in rows:
try:
if ip_obj in ip_network(row["subnet"], strict=False):
return str(row["uuid"])
except (ValueError, KeyError):
continue
raise ValueError(f"No Kea-managed subnet contains {ip}")
row = subnet_containing(rows, ip)
if row is None:
raise ValueError(f"No Kea-managed subnet contains {ip}")
return str(row["uuid"])
def get_dhcp_reservations(self) -> list[dict[str, Any]]:
"""Return all Kea DHCPv4 static reservations, vendor-neutral.