From 71745b802478668a42d9e5fe02dc98512ae6bb2f Mon Sep 17 00:00:00 2001 From: Jeffrey van Pelt Date: Sat, 11 Jun 2022 13:46:17 +0200 Subject: [PATCH] Added conditional to only collect qmpstatus on qemu VMs (#4816) * Added conditional to only collect qmpstatus on qemu VMs * Processed feedback, added changelog * Initial change to unit tests * Made Sanity tests happy again * Missed a function call, removed superfluous function * Derp, no need to mock get_vm_status anymore * Added detail checks whether hosts are mapped to the paused/prelaunch groups * Fix sanity check * Processed feedback * Processed feedback - noqa --- .../4816-proxmox-fix-extended-status.yaml | 2 + plugins/inventory/proxmox.py | 3 +- tests/unit/plugins/inventory/test_proxmox.py | 107 +++++++++++++++++- 3 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/4816-proxmox-fix-extended-status.yaml diff --git a/changelogs/fragments/4816-proxmox-fix-extended-status.yaml b/changelogs/fragments/4816-proxmox-fix-extended-status.yaml new file mode 100644 index 0000000000..496de40ab0 --- /dev/null +++ b/changelogs/fragments/4816-proxmox-fix-extended-status.yaml @@ -0,0 +1,2 @@ +bugfixes: + - proxmox inventory plugin - fixed extended status detection for qemu (https://github.com/ansible-collections/community.general/pull/4816). diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index 1664e6ba03..1669b23325 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -443,7 +443,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): def _get_vm_status(self, properties, node, vmid, vmtype, name): ret = self._get_json("%s/api2/json/nodes/%s/%s/%s/status/current" % (self.proxmox_url, node, vmtype, vmid)) properties[self._fact('status')] = ret['status'] - properties[self._fact('qmpstatus')] = ret['qmpstatus'] + if vmtype == 'qemu': + properties[self._fact('qmpstatus')] = ret['qmpstatus'] def _get_vm_snapshots(self, properties, node, vmid, vmtype, name): ret = self._get_json("%s/api2/json/nodes/%s/%s/%s/snapshot" % (self.proxmox_url, node, vmtype, vmid)) diff --git a/tests/unit/plugins/inventory/test_proxmox.py b/tests/unit/plugins/inventory/test_proxmox.py index 91411d1d55..bf553024ab 100644 --- a/tests/unit/plugins/inventory/test_proxmox.py +++ b/tests/unit/plugins/inventory/test_proxmox.py @@ -520,6 +520,99 @@ def get_json(url): } ] } + elif url == "https://localhost:8006/api2/json/nodes/testnode/lxc/100/status/current": + # _get_vm_status (lxc) + return { + "swap": 0, + "name": "test-lxc", + "diskread": 0, + "vmid": 100, + "diskwrite": 0, + "pid": 9000, + "mem": 89980928, + "netin": 1950776396424, + "disk": 4998168576, + "cpu": 0.00163430613110039, + "type": "lxc", + "uptime": 6793736, + "maxmem": 1073741824, + "status": "running", + "cpus": "1", + "ha": { + "group": 'null', + "state": "started", + "managed": 1 + }, + "maxdisk": 3348329267200, + "netout": 1947793356037, + "maxswap": 1073741824 + } + elif url == "https://localhost:8006/api2/json/nodes/testnode/qemu/101/status/current": + # _get_vm_status (qemu) + return { + "status": "stopped", + "uptime": 0, + "maxmem": 5364514816, + "maxdisk": 34359738368, + "netout": 0, + "cpus": 2, + "ha": { + "managed": 0 + }, + "diskread": 0, + "vmid": 101, + "diskwrite": 0, + "name": "test-qemu", + "cpu": 0, + "disk": 0, + "netin": 0, + "mem": 0, + "qmpstatus": "stopped" + } + elif url == "https://localhost:8006/api2/json/nodes/testnode/qemu/102/status/current": + # _get_vm_status (qemu) + return { + "status": "stopped", + "uptime": 0, + "maxmem": 5364514816, + "maxdisk": 34359738368, + "netout": 0, + "cpus": 2, + "ha": { + "managed": 0 + }, + "diskread": 0, + "vmid": 102, + "diskwrite": 0, + "name": "test-qemu-windows", + "cpu": 0, + "disk": 0, + "netin": 0, + "mem": 0, + "qmpstatus": "prelaunch" + } + elif url == "https://localhost:8006/api2/json/nodes/testnode/qemu/103/status/current": + # _get_vm_status (qemu) + return { + "status": "stopped", + "uptime": 0, + "maxmem": 5364514816, + "maxdisk": 34359738368, + "netout": 0, + "cpus": 2, + "ha": { + "managed": 0 + }, + "diskread": 0, + "vmid": 103, + "diskwrite": 0, + "name": "test-qemu-multi-nic", + "cpu": 0, + "disk": 0, + "netin": 0, + "mem": 0, + "qmpstatus": "paused" + } def get_vm_snapshots(node, properties, vmtype, vmid, name): @@ -537,10 +630,6 @@ def get_vm_snapshots(node, properties, vmtype, vmid, name): }] -def get_vm_status(properties, node, vmtype, vmid, name): - return True - - def get_option(opts): def fn(option): default = opts.get('default', False) @@ -568,7 +657,6 @@ def test_populate(inventory, mocker): # bypass authentication and API fetch calls inventory._get_auth = mocker.MagicMock(side_effect=get_auth) inventory._get_json = mocker.MagicMock(side_effect=get_json) - inventory._get_vm_status = mocker.MagicMock(side_effect=get_vm_status) inventory._get_vm_snapshots = mocker.MagicMock(side_effect=get_vm_snapshots) inventory.get_option = mocker.MagicMock(side_effect=get_option(opts)) inventory._can_add_host = mocker.MagicMock(return_value=True) @@ -617,6 +705,14 @@ def test_populate(inventory, mocker): for group in ['paused', 'prelaunch']: assert ('%sall_%s' % (inventory.group_prefix, group)) in inventory.inventory.groups + # check if qemu-windows is in the prelaunch group + group_prelaunch = inventory.inventory.groups['proxmox_all_prelaunch'] + assert group_prelaunch.hosts == [host_qemu_windows] + + # check if qemu-multi-nic is in the paused group + group_paused = inventory.inventory.groups['proxmox_all_paused'] + assert group_paused.hosts == [host_qemu_multi_nic] + def test_populate_missing_qemu_extended_groups(inventory, mocker): # module settings @@ -638,7 +734,6 @@ def test_populate_missing_qemu_extended_groups(inventory, mocker): # bypass authentication and API fetch calls inventory._get_auth = mocker.MagicMock(side_effect=get_auth) inventory._get_json = mocker.MagicMock(side_effect=get_json) - inventory._get_vm_status = mocker.MagicMock(side_effect=get_vm_status) inventory._get_vm_snapshots = mocker.MagicMock(side_effect=get_vm_snapshots) inventory.get_option = mocker.MagicMock(side_effect=get_option(opts)) inventory._can_add_host = mocker.MagicMock(return_value=True)