Files
napalm-opnsense/napalm_opnsense/api_endpoint.py
T
Christian Manivong 4e377923db fix(connection): probe the API port from base_url in is_alive
is_alive always opened port 443 on the hostname, so a firewall whose API
is reached through base_url on another port (https://fw:8443) read as
dead. The probe now takes host and port from base_url, falling back to
the scheme's default port.

Closes #15
2026-11-01 21:44:51 +01:00

21 lines
780 B
Python

"""Where the OPNsense REST API listens, read from the driver's ``base_url``."""
from __future__ import annotations
from urllib.parse import urlsplit
#: The port the API listens on when ``base_url`` names none, per scheme.
DEFAULT_PORTS = {"https": 443, "http": 80}
#: Seconds the driver's reachability probe waits for the API port to accept.
ALIVE_TIMEOUT = 5
def api_endpoint(base_url: str, fallback_host: str) -> tuple[str, int]:
"""``https://fw:8443`` -> ``("fw", 8443)``: the host and port of the REST API.
Without a port the scheme's default applies; without a host, *fallback_host*.
"""
parts = urlsplit(base_url)
port = parts.port or DEFAULT_PORTS.get(parts.scheme.lower(), DEFAULT_PORTS["https"])
return parts.hostname or fallback_host, port