feat: managed devices from the Integration and the classic API
Integration fixtures are assembled from the shapes of Ubiquiti's published OpenAPI spec (10.6.106), which carries no whole-response examples. Refs NetOrk/netork#2787
This commit is contained in:
1 parent
596698970c
commit
4b171d154b
6 files changed
+343
No files matched your search
@@ -0,0 +1,100 @@
|
||||
"""The devices a UniFi controller manages, as :class:`ManagedDeviceDict`.
|
||||
|
||||
Two sources say the same thing in different words: the Integration API
|
||||
(``/v1/sites/{id}/devices``, ``/v1/pending-devices``) and the classic
|
||||
``stat/device``. ``site`` is always the classic site name (``default``) -- the
|
||||
Integration API's ``internalReference`` -- because that is the name a device's
|
||||
own driver addresses the controller by.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from napalm_device_types.network_controller_models import ManagedDeviceDict
|
||||
|
||||
Record = dict[str, Any]
|
||||
|
||||
_INTEGRATION_STATES = {
|
||||
"ONLINE": "online",
|
||||
"PENDING_ADOPTION": "pending",
|
||||
"UPDATING": "busy",
|
||||
"GETTING_READY": "busy",
|
||||
"ADOPTING": "busy",
|
||||
"DELETING": "busy",
|
||||
}
|
||||
#: The classic API's numeric states: 1 connected, 2 pending adoption, 4 upgrading,
|
||||
#: 5 provisioning, 7 adopting; the rest (0 disconnected, 6 heartbeat missed,
|
||||
#: 9/10 adoption failed, 11 isolated) cannot be reached.
|
||||
_CLASSIC_STATES = {1: "online", 2: "pending", 4: "busy", 5: "busy", 7: "busy"}
|
||||
_CLASSIC_ROLES = {
|
||||
"usw": "switch",
|
||||
"uap": "access_point",
|
||||
"ugw": "gateway",
|
||||
"udm": "gateway",
|
||||
"uxg": "gateway",
|
||||
}
|
||||
_OFFLINE = "offline"
|
||||
|
||||
|
||||
def _role_from_features(features: list[Any]) -> str:
|
||||
"""Gateway first: a gateway with switch ports is a gateway."""
|
||||
for feature, role in (("gateway", "gateway"), ("accessPoint", "access_point")):
|
||||
if feature in features:
|
||||
return role
|
||||
return "switch" if "switching" in features else "other"
|
||||
|
||||
|
||||
def _base(device: Record, site: str, state: str, role: str) -> ManagedDeviceDict:
|
||||
return {
|
||||
"mac": str(device.get("macAddress") or "").lower(),
|
||||
"name": str(device.get("name") or ""),
|
||||
"model": str(device.get("model") or ""),
|
||||
"role": role,
|
||||
"state": state,
|
||||
"site": site,
|
||||
"ip": device.get("ipAddress") or None,
|
||||
}
|
||||
|
||||
|
||||
def managed_from_integration(device: Record, site: str) -> ManagedDeviceDict:
|
||||
"""An adopted device of the Integration API (``/v1/sites/{id}/devices``)."""
|
||||
state = _INTEGRATION_STATES.get(str(device.get("state")), _OFFLINE)
|
||||
managed = _base(device, site, state, _role_from_features(device.get("features") or []))
|
||||
if device.get("id"):
|
||||
managed["id"] = str(device["id"])
|
||||
if device.get("firmwareVersion"):
|
||||
managed["firmware"] = str(device["firmwareVersion"])
|
||||
return managed
|
||||
|
||||
|
||||
def managed_from_pending(device: Record, site: str) -> ManagedDeviceDict:
|
||||
"""A device that waits to be adopted (``/v1/pending-devices``): it has no id yet."""
|
||||
managed = _base(device, site, "pending", _role_from_features(device.get("features") or []))
|
||||
if device.get("firmwareVersion"):
|
||||
managed["firmware"] = str(device["firmwareVersion"])
|
||||
return managed
|
||||
|
||||
|
||||
def managed_from_classic(record: Record, site: str) -> ManagedDeviceDict:
|
||||
"""A device of the classic ``stat/device``, adopted or pending."""
|
||||
managed: ManagedDeviceDict = {
|
||||
"mac": str(record.get("mac") or "").lower(),
|
||||
"name": str(record.get("name") or ""),
|
||||
"model": str(record.get("model") or ""),
|
||||
"role": _CLASSIC_ROLES.get(str(record.get("type")), "other"),
|
||||
"state": _CLASSIC_STATES.get(record.get("state"), _OFFLINE), # type: ignore[arg-type]
|
||||
"site": site,
|
||||
"ip": record.get("ip") or None,
|
||||
}
|
||||
optional = {
|
||||
"id": record.get("_id"),
|
||||
"serial": str(record.get("serial") or "").upper(),
|
||||
"model_name": record.get("model_display"),
|
||||
"firmware": record.get("version"),
|
||||
"firmware_available": record.get("upgrade_to_firmware") if record.get("upgradable") else None,
|
||||
}
|
||||
for key, value in optional.items():
|
||||
if value:
|
||||
managed[key] = str(value) # type: ignore[literal-required]
|
||||
return managed
|
||||
@@ -0,0 +1,78 @@
|
||||
{
|
||||
"offset": 0,
|
||||
"limit": 25,
|
||||
"count": 4,
|
||||
"totalCount": 4,
|
||||
"data": [
|
||||
{
|
||||
"id": "5b5e2c17-0d3c-3c47-a0f1-6c1d2f1e0001",
|
||||
"macAddress": "fc:ec:da:11:22:33",
|
||||
"ipAddress": "192.168.0.57",
|
||||
"name": "Switch 16",
|
||||
"model": "US16P150",
|
||||
"state": "ONLINE",
|
||||
"supported": true,
|
||||
"firmwareVersion": "4.0.66.10832",
|
||||
"firmwareUpdatable": true,
|
||||
"features": [
|
||||
"switching"
|
||||
],
|
||||
"interfaces": [
|
||||
"ports"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "5b5e2c17-0d3c-3c47-a0f1-6c1d2f1e0002",
|
||||
"macAddress": "80:2a:a8:00:01:02",
|
||||
"ipAddress": "192.168.0.4",
|
||||
"name": "ACCESS POINT AC PRO",
|
||||
"model": "U7PG2",
|
||||
"state": "ONLINE",
|
||||
"supported": true,
|
||||
"firmwareVersion": "4.0.69.10871",
|
||||
"firmwareUpdatable": false,
|
||||
"features": [
|
||||
"accessPoint"
|
||||
],
|
||||
"interfaces": [
|
||||
"ports",
|
||||
"radios"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "5b5e2c17-0d3c-3c47-a0f1-6c1d2f1e0003",
|
||||
"macAddress": "74:ac:b9:a1:01:01",
|
||||
"ipAddress": "198.168.0.9",
|
||||
"name": "UniFi Switch Flex Mini",
|
||||
"model": "USMINI",
|
||||
"state": "CONNECTION_INTERRUPTED",
|
||||
"supported": true,
|
||||
"firmwareVersion": "1.8.4.674",
|
||||
"firmwareUpdatable": false,
|
||||
"features": [
|
||||
"switching"
|
||||
],
|
||||
"interfaces": [
|
||||
"ports"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "5b5e2c17-0d3c-3c47-a0f1-6c1d2f1e0004",
|
||||
"macAddress": "78:8a:20:33:44:55",
|
||||
"ipAddress": "1.2.3.4",
|
||||
"name": "USG",
|
||||
"model": "UGW3",
|
||||
"state": "UPDATING",
|
||||
"supported": true,
|
||||
"firmwareVersion": "4.4.44.5213844",
|
||||
"firmwareUpdatable": false,
|
||||
"features": [
|
||||
"gateway",
|
||||
"switching"
|
||||
],
|
||||
"interfaces": [
|
||||
"ports"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"applicationVersion": "10.6.106"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"offset": 0,
|
||||
"limit": 25,
|
||||
"count": 1,
|
||||
"totalCount": 1,
|
||||
"data": [
|
||||
{
|
||||
"macAddress": "f4:92:bf:00:00:aa",
|
||||
"ipAddress": "192.168.0.77",
|
||||
"model": "U6LR",
|
||||
"state": "PENDING_ADOPTION",
|
||||
"supported": true,
|
||||
"firmwareVersion": "6.6.55",
|
||||
"firmwareUpdatable": false,
|
||||
"features": [
|
||||
"accessPoint"
|
||||
],
|
||||
"adoptionTargetSiteIds": [
|
||||
"88f7af54-98f8-306a-a1c7-c9349722b1f6"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"offset": 0,
|
||||
"limit": 25,
|
||||
"count": 1,
|
||||
"totalCount": 1,
|
||||
"data": [
|
||||
{
|
||||
"id": "88f7af54-98f8-306a-a1c7-c9349722b1f6",
|
||||
"internalReference": "default",
|
||||
"name": "Default"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
"""The devices a UniFi controller manages, from either API, in the contract's words."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from conftest import load
|
||||
from napalm_ubiquiti.parsers.managed import (
|
||||
managed_from_classic,
|
||||
managed_from_integration,
|
||||
managed_from_pending,
|
||||
)
|
||||
|
||||
|
||||
class TestIntegration:
|
||||
def test_an_adopted_switch(self) -> None:
|
||||
device = load("integration/v10.6.106-devices.json")["data"][0]
|
||||
|
||||
assert managed_from_integration(device, "default") == {
|
||||
"mac": "fc:ec:da:11:22:33",
|
||||
"name": "Switch 16",
|
||||
"model": "US16P150",
|
||||
"role": "switch",
|
||||
"state": "online",
|
||||
"site": "default",
|
||||
"ip": "192.168.0.57",
|
||||
"id": "5b5e2c17-0d3c-3c47-a0f1-6c1d2f1e0001",
|
||||
"firmware": "4.0.66.10832",
|
||||
}
|
||||
|
||||
def test_an_ap_and_a_gateway_that_also_switches(self) -> None:
|
||||
data = load("integration/v10.6.106-devices.json")["data"]
|
||||
|
||||
assert managed_from_integration(data[1], "default")["role"] == "access_point"
|
||||
assert managed_from_integration(data[3], "default")["role"] == "gateway"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("state", "word"),
|
||||
[
|
||||
("ONLINE", "online"),
|
||||
("OFFLINE", "offline"),
|
||||
("CONNECTION_INTERRUPTED", "offline"),
|
||||
("ISOLATED", "offline"),
|
||||
("PENDING_ADOPTION", "pending"),
|
||||
("UPDATING", "busy"),
|
||||
("GETTING_READY", "busy"),
|
||||
("ADOPTING", "busy"),
|
||||
("DELETING", "busy"),
|
||||
("U5G_INCORRECT_TOPOLOGY", "offline"),
|
||||
("SOMETHING_NEW", "offline"),
|
||||
],
|
||||
)
|
||||
def test_states(self, state: str, word: str) -> None:
|
||||
device = {"macAddress": "f4:92:bf:00:00:01", "model": "U6LR", "state": state, "features": []}
|
||||
|
||||
assert managed_from_integration(device, "default")["state"] == word
|
||||
|
||||
def test_a_device_without_features_or_address(self) -> None:
|
||||
device = {"macAddress": "F4:92:BF:00:00:01", "model": "UP1", "state": "ONLINE", "features": [], "name": ""}
|
||||
|
||||
managed = managed_from_integration(device, "default")
|
||||
|
||||
assert managed["role"] == "other"
|
||||
assert managed["ip"] is None
|
||||
assert managed["mac"] == "f4:92:bf:00:00:01"
|
||||
assert "firmware" not in managed and "id" not in managed
|
||||
|
||||
def test_a_pending_device(self) -> None:
|
||||
device = load("integration/v10.6.106-pending-devices.json")["data"][0]
|
||||
|
||||
assert managed_from_pending(device, "default") == {
|
||||
"mac": "f4:92:bf:00:00:aa",
|
||||
"name": "",
|
||||
"model": "U6LR",
|
||||
"role": "access_point",
|
||||
"state": "pending",
|
||||
"site": "default",
|
||||
"ip": "192.168.0.77",
|
||||
"firmware": "6.6.55",
|
||||
}
|
||||
|
||||
|
||||
class TestClassic:
|
||||
def test_a_switch(self, us16: dict[str, Any]) -> None:
|
||||
managed = managed_from_classic(us16, "default")
|
||||
|
||||
assert managed == {
|
||||
"mac": "fc:ec:da:11:22:33",
|
||||
"name": "Switch 16",
|
||||
"model": "US16P150",
|
||||
"role": "switch",
|
||||
"state": "online",
|
||||
"site": "default",
|
||||
"ip": "192.168.0.57",
|
||||
"id": us16["_id"],
|
||||
"serial": "FCECDA112233",
|
||||
"firmware": "4.0.66.10832",
|
||||
"firmware_available": "4.0.80.10875",
|
||||
}
|
||||
|
||||
def test_no_offer_when_it_is_not_upgradable(self, us16: dict[str, Any]) -> None:
|
||||
assert "firmware_available" not in managed_from_classic(dict(us16, upgradable=False), "default")
|
||||
|
||||
def test_the_offered_firmware_and_the_product_name(self, us16: dict[str, Any]) -> None:
|
||||
record = dict(us16, upgradable=True, upgrade_to_firmware="7.1.26.15869", model_display="USW 16 PoE")
|
||||
|
||||
managed = managed_from_classic(record, "default")
|
||||
|
||||
assert managed["firmware_available"] == "7.1.26.15869"
|
||||
assert managed["model_name"] == "USW 16 PoE"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("kind", "role"),
|
||||
[("usw", "switch"), ("uap", "access_point"), ("ugw", "gateway"), ("udm", "gateway"), ("uxg", "gateway"), ("uph", "other")],
|
||||
)
|
||||
def test_roles(self, kind: str, role: str) -> None:
|
||||
assert managed_from_classic({"mac": "x", "type": kind, "state": 1}, "s")["role"] == role
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("state", "word"),
|
||||
[(1, "online"), (0, "offline"), (6, "offline"), (11, "offline"), (2, "pending"), (4, "busy"), (5, "busy"), (7, "busy"), (99, "offline")],
|
||||
)
|
||||
def test_states(self, state: int, word: str) -> None:
|
||||
assert managed_from_classic({"mac": "x", "type": "usw", "state": state}, "s")["state"] == word
|
||||
Reference in new issue
Block a user