fix(rest): raise on a failed REST read instead of returning {} #24

Merged
christianmanivong merged 1 commits from fix/bugs-2026-11-02 into main 2026-11-02 08:12:16 +00:00
Owner

What

A failed read over the REST API raises instead of reading as an empty switch.

  • ProcurveApiClient.get() raises ConnectionException when the switch does not answer or refuses the session (HTTP 401/403), and RuntimeError with the status for any other non-2xx answer or a body that is not JSON. It returned {} for all of them.
  • A getter that expects a resource to be missing on some firmware or configuration says so with get(..., missing_ok=True), and only a 404 reads as empty there: system/status (stacked switches fall back to system/status/global_info), dns, system/status/switch, port-statistics, vlans/<id>/ipv4, poe/ports. Every other read (ports, vlans, vlans-ports, arp, mac-table, lldp/remote-device, ntp/server, system/status/global_info) raises.
  • get_interfaces no longer swallows every error from system/status/switch (only its 404).
  • get_config: a failed REST read still falls back to SSH, but when the SSH read fails too it raises instead of returning an empty running configuration. A changed SSH host key keeps its own exception type.
  • cli() over REST goes through the new ProcurveApiClient.run_cli() and raises on a failed request. It returned "Error: HTTP <status>" as the command's output.

Why

An expired session cookie, a timeout or an HTTP 500 produced a switch with an empty hostname, no ports, no VLANs and no neighbours, and the application stored that as the switch's state.

Tests

Fixtures only (no device). tests/unit/test_rest_failed_reads.py drives the real ProcurveApiClient against a fake session that answers per endpoint: 500/401/403/timeout/non-JSON on get(), every REST getter with its primary read answering 500, each missing_ok resource answering 404, get_config with REST and SSH failing, and cli() with 500/401/connection refused. The first of them (test_get_interfaces_raises_when_ports_answers_500) failed before the change. The cli() test that pinned "Error: HTTP 401" as output is replaced by these.

pytest 149 passed, ruff format/check clean, mypy clean, napalm-standard ratchet within the baseline (tightened: get_interfaces 65→62 lines / complexity 15→14, procurve.py 1387→1385 lines), napalm-standard check passes.

Not changed: there is no re-login after a 401 within one session; the next open() starts a new one.

Closes #22

## What A failed read over the REST API raises instead of reading as an empty switch. - `ProcurveApiClient.get()` raises `ConnectionException` when the switch does not answer or refuses the session (HTTP 401/403), and `RuntimeError` with the status for any other non-2xx answer or a body that is not JSON. It returned `{}` for all of them. - A getter that expects a resource to be missing on some firmware or configuration says so with `get(..., missing_ok=True)`, and only a 404 reads as empty there: `system/status` (stacked switches fall back to `system/status/global_info`), `dns`, `system/status/switch`, `port-statistics`, `vlans/<id>/ipv4`, `poe/ports`. Every other read (`ports`, `vlans`, `vlans-ports`, `arp`, `mac-table`, `lldp/remote-device`, `ntp/server`, `system/status/global_info`) raises. - `get_interfaces` no longer swallows every error from `system/status/switch` (only its 404). - `get_config`: a failed REST read still falls back to SSH, but when the SSH read fails too it raises instead of returning an empty running configuration. A changed SSH host key keeps its own exception type. - `cli()` over REST goes through the new `ProcurveApiClient.run_cli()` and raises on a failed request. It returned `"Error: HTTP <status>"` as the command's output. ## Why An expired session cookie, a timeout or an HTTP 500 produced a switch with an empty hostname, no ports, no VLANs and no neighbours, and the application stored that as the switch's state. ## Tests Fixtures only (no device). `tests/unit/test_rest_failed_reads.py` drives the real `ProcurveApiClient` against a fake session that answers per endpoint: 500/401/403/timeout/non-JSON on `get()`, every REST getter with its primary read answering 500, each `missing_ok` resource answering 404, `get_config` with REST and SSH failing, and `cli()` with 500/401/connection refused. The first of them (`test_get_interfaces_raises_when_ports_answers_500`) failed before the change. The cli() test that pinned `"Error: HTTP 401"` as output is replaced by these. pytest 149 passed, ruff format/check clean, mypy clean, `napalm-standard ratchet` within the baseline (tightened: `get_interfaces` 65→62 lines / complexity 15→14, `procurve.py` 1387→1385 lines), `napalm-standard check` passes. Not changed: there is no re-login after a 401 within one session; the next `open()` starts a new one. Closes #22
christianmanivong added 1 commit 2026-11-02 07:39:24 +00:00
fix(rest): raise on a failed REST read instead of returning {}
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 1m14s
driver / test (3.12) (pull_request) Successful in 1m9s
driver / test (3.13) (pull_request) Successful in 1m10s
driver / lowest (pull_request) Successful in 30s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Successful in 1m10s
CI / driver (pull_request) Successful in 2m49s
driver / publish (pull_request) Skipped
628a827cbe
ProcurveApiClient.get() answered {} for a timeout, a refused session or
any HTTP error, so every REST getter reported a failed read as a switch
with no hostname, ports, VLANs or neighbours. get() now raises
ConnectionException (no answer, 401/403) or RuntimeError (other
non-2xx, a body that is not JSON); the getters that expect a resource
to be missing on some firmware pass missing_ok for its 404.

get_config no longer returns an empty configuration when both the REST
read and the SSH fallback failed, and cli() over REST raises instead of
returning "Error: HTTP <status>" as the command's output.

Closes #22
christianmanivong merged commit f3da8944a3 into main 2026-11-02 08:12:16 +00:00
christianmanivong deleted branch fix/bugs-2026-11-02 2026-11-02 08:12:16 +00:00
Sign in to join this conversation.