From 32e9a0c25066099887dbd21679d1c83ac85c9f45 Mon Sep 17 00:00:00 2001 From: Jeffrey van Pelt Date: Thu, 22 Jul 2021 22:55:07 +0200 Subject: [PATCH] Proxmox inventory: Added snapshots fact (#3044) * Added snapshots fact * Added changelog * Made linter happy again * Processed feedback * Fix changelog type * Punctuation ;-) * Punctuation ;-), take 2 --- .../3044-proxmox-inventory-snapshots.yml | 2 ++ plugins/inventory/proxmox.py | 15 +++++++++++++-- tests/unit/plugins/inventory/test_proxmox.py | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/3044-proxmox-inventory-snapshots.yml diff --git a/changelogs/fragments/3044-proxmox-inventory-snapshots.yml b/changelogs/fragments/3044-proxmox-inventory-snapshots.yml new file mode 100644 index 0000000000..d6a324ea30 --- /dev/null +++ b/changelogs/fragments/3044-proxmox-inventory-snapshots.yml @@ -0,0 +1,2 @@ +minor_changes: + - proxmox inventory plugin - added snapshots to host facts (https://github.com/ansible-collections/community.general/pull/3044). diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index c99962bcdd..f0f5a4e418 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -325,6 +325,15 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): status_key = self.to_safe('%s%s' % (self.get_option('facts_prefix'), status_key.lower())) self.inventory.set_variable(name, status_key, status) + def _get_vm_snapshots(self, node, vmid, vmtype, name): + ret = self._get_json("%s/api2/json/nodes/%s/%s/%s/snapshot" % (self.proxmox_url, node, vmtype, vmid)) + + snapshots_key = 'snapshots' + snapshots_key = self.to_safe('%s%s' % (self.get_option('facts_prefix'), snapshots_key.lower())) + + snapshots = [snapshot['name'] for snapshot in ret if snapshot['name'] != 'current'] + self.inventory.set_variable(name, snapshots_key, snapshots) + def to_safe(self, word): '''Converts 'bad' characters in a string to underscores so they can be used as Ansible groups #> ProxmoxInventory.to_safe("foo-bar baz") @@ -393,9 +402,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): elif lxc['status'] == 'running': self.inventory.add_child(running_group, lxc['name']) - # get LXC config for facts + # get LXC config and snapshots for facts if self.get_option('want_facts'): self._get_vm_config(node['node'], lxc['vmid'], 'lxc', lxc['name']) + self._get_vm_snapshots(node['node'], lxc['vmid'], 'lxc', lxc['name']) self._apply_constructable(lxc["name"], self.inventory.get_host(lxc['name']).get_vars()) @@ -417,9 +427,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): elif qemu['status'] == 'running': self.inventory.add_child(running_group, qemu['name']) - # get QEMU config for facts + # get QEMU config and snapshots for facts if self.get_option('want_facts'): self._get_vm_config(node['node'], qemu['vmid'], 'qemu', qemu['name']) + self._get_vm_snapshots(node['node'], qemu['vmid'], 'qemu', qemu['name']) self._apply_constructable(qemu["name"], self.inventory.get_host(qemu['name']).get_vars()) diff --git a/tests/unit/plugins/inventory/test_proxmox.py b/tests/unit/plugins/inventory/test_proxmox.py index 87d47a3cff..12927551f8 100644 --- a/tests/unit/plugins/inventory/test_proxmox.py +++ b/tests/unit/plugins/inventory/test_proxmox.py @@ -522,6 +522,21 @@ def get_json(url): } +def get_vm_snapshots(node, vmtype, vmid, name): + return [ + {"description": "", + "name": "clean", + "snaptime": 1000, + "vmstate": 0 + }, + {"name": "current", + "digest": "1234689abcdf", + "running": 0, + "description": "You are here!", + "parent": "clean" + }] + + def get_vm_status(node, vmtype, vmid, name): return True @@ -549,6 +564,7 @@ def test_populate(inventory, mocker): 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) inventory._populate()