diff --git a/CHANGELOG.md b/CHANGELOG.md index b6fb95a..f7c7b68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- `resume_vm(name)` on both drivers (napalm-device-types' `HypervisorDriver`, + NetOrk/netork#2658): a VM `suspend_vm` suspended is powered on, which vSphere resumes + from its saved state. A VM that is not suspended is refused. - `get_clusters()` on both drivers. The vCenter reports each cluster and each host added to a datacenter directly (as a cluster of one with no name), with the id `/`; an ESXi host reports itself, `local`, with its system diff --git a/README.md b/README.md index b70a096..56068fc 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ hardware**: see [Harvesting fixtures](#harvesting-fixtures). | `get_environment` | ✅ | ✅ (per host) | quick stats + hardware sensors | | `get_vms` | ✅ | ✅ | VMs, templates excluded | | `get_vm_config` | ✅ | ✅ | virtual hardware | -| `start_vm`, `stop_vm`, `reboot_vm`, `suspend_vm` | ✅ | ✅ | power tasks / VMware Tools | +| `start_vm`, `stop_vm`, `reboot_vm`, `suspend_vm`, `resume_vm` | ✅ | ✅ | power tasks / VMware Tools | | `get_vm_snapshots`, `create/delete/rollback_vm_snapshot` | ✅ | ✅ | snapshot tree | | `get_vm_storage_pools` | ✅ | ✅ | datastores | | `get_virtual_networks` | ✅ | ✅ (+ dvPortgroups) | port groups | diff --git a/napalm_vmware/actions.py b/napalm_vmware/actions.py index 9640bd8..10b9cb2 100644 --- a/napalm_vmware/actions.py +++ b/napalm_vmware/actions.py @@ -12,6 +12,7 @@ from napalm_vmware.parse.snapshots import find_snapshot, snapshot_list _TASK_TIMEOUT = 300 _GUEST_SHUTDOWN_TIMEOUT = 300 _RUNNING = "poweredOn" +_SUSPENDED = "suspended" _TOOLS_RUNNING = "guestToolsRunning" @@ -81,6 +82,13 @@ class VmwareActionsMixin: self._require_running(vm, "suspend") self._run(self._vm_mo(vm).SuspendVM_Task) + def resume_vm(self, name: str) -> None: + """Power a suspended VM on: vSphere resumes it from its saved state.""" + vm = self._find_vm(name) + if vm.get("runtime.powerState") != _SUSPENDED: + raise RuntimeError(f"Cannot resume VM {vm.get('name')!r}: it is not suspended") + self._run(self._vm_mo(vm).PowerOnVM_Task) + # -- snapshots ------------------------------------------------------------- def create_vm_snapshot( diff --git a/tests/test_actions.py b/tests/test_actions.py index 48dd61a..811ddf3 100644 --- a/tests/test_actions.py +++ b/tests/test_actions.py @@ -80,6 +80,20 @@ class TestPower: with pytest.raises(RuntimeError, match="not running"): driver.suspend_vm(vm0["name"]) + def test_resume_powers_a_suspended_vm_on(self, driver, vm0, waited): + """vSphere resumes a suspended VM by powering it on (NetOrk/netork#2658).""" + vm0["runtime.powerState"] = "suspended" + driver.resume_vm(vm0["name"]) + driver.mo.PowerOnVM_Task.assert_called_once_with() + assert waited.call_args.args[1] is driver.mo.PowerOnVM_Task.return_value + + @pytest.mark.parametrize("state", ["poweredOn", "poweredOff"]) + def test_resume_requires_a_suspended_vm(self, driver, vm0, state): + vm0["runtime.powerState"] = state + with pytest.raises(RuntimeError, match="not suspended"): + driver.resume_vm(vm0["name"]) + driver.mo.PowerOnVM_Task.assert_not_called() + def test_free_license_refusal_is_explained(self, driver, vm0): from pyVmomi import vim