diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b5d5cd..c68f033 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ All notable changes to this driver. The format follows ## [Unreleased] ### Added +- Shutting the firewall down (NetOrk/netork#2841): `shutdown_host()` from + napalm-device-types' `HostShutdownMixin` halts the firewall and powers it off through + `POST /api/core/firmware/poweroff` (OPNsense 16.1.15 and later), the clean shutdown of + *Power → Power Off*, as `reboot_host()` restarts it through `firmware/reboot`. A + refused request raises `RuntimeError` with the firewall's answer. - How people log in (NetOrk/netork#662), in napalm-device-types 4.50.0's shapes, read from config.xml over `/api/core/backup/download/this`. `get_web_login_auth()` returns the auth servers `` names, in their order, and the Local Database when it diff --git a/napalm_opnsense/opnsense.py b/napalm_opnsense/opnsense.py index 3dba009..3bb65ab 100644 --- a/napalm_opnsense/opnsense.py +++ b/napalm_opnsense/opnsense.py @@ -2192,6 +2192,22 @@ class OPNsenseDriver( except Exception as exc: raise RuntimeError(f"The firewall refused to reboot: {exc}") from exc + def shutdown_host(self) -> None: + """Halt the firewall and power it off through its firmware API + (``HostShutdownMixin``). + + Calls ``POST /api/core/firmware/poweroff``, which OPNsense has had since + 16.1.15, next to ``firmware/reboot``. It runs configd's ``firmware poweroff`` + detached, the clean shutdown of *Power → Power Off*, and answers before the + firewall goes down. + + :raises RuntimeError: when the API refuses the request. + """ + try: + self._post("/api/core/firmware/poweroff") + except Exception as exc: + raise RuntimeError(f"The firewall refused to shut down: {exc}") from exc + def get_host_status(self) -> dict[str, Any]: """Whether the firewall needs a reboot to finish an update; it does not patch itself as far as netOrk can tell.""" diff --git a/tests/unit/test_updates.py b/tests/unit/test_updates.py index b8c8b4f..3a005e9 100644 --- a/tests/unit/test_updates.py +++ b/tests/unit/test_updates.py @@ -140,6 +140,22 @@ class TestRebootHost: driver.reboot_host() +class TestShutdownHost: + """``shutdown_host`` halts and powers the firewall off through its firmware API + (netOrk #2841).""" + + def test_the_firmware_api_is_asked(self, driver): + with patch.object(driver, "_post", return_value={"status": "ok"}) as post: + driver.shutdown_host() + + post.assert_called_once_with("/api/core/firmware/poweroff") + + def test_a_refusal_is_raised_with_the_answer(self, driver): + with patch.object(driver, "_post", side_effect=ConnectionError("403 Forbidden")): + with pytest.raises(RuntimeError, match="refused to shut down: 403 Forbidden"): + driver.shutdown_host() + + def test_no_warnings_of_its_own(): """The pending updates are get_available_updates; the caller names updates_available from them (netOrk #1111)."""