From c7e7b6c9e38a27acd799d7d6ebdfb66533f10ed2 Mon Sep 17 00:00:00 2001 From: Tom Paine Date: Wed, 21 Aug 2024 14:32:41 +0100 Subject: [PATCH] Add check for empty records and add test --- plugins/inventory/opennebula.py | 3 ++- tests/unit/plugins/inventory/test_opennebula.py | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/plugins/inventory/opennebula.py b/plugins/inventory/opennebula.py index 75b40e10bc..11b5bfc297 100644 --- a/plugins/inventory/opennebula.py +++ b/plugins/inventory/opennebula.py @@ -200,7 +200,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable): server['name'] = vm.NAME server['id'] = vm.ID - server['host'] = vm.HISTORY_RECORDS.HISTORY[-1].HOSTNAME + if vm.HISTORY_RECORDS.HISTORY[-1]: + server['host'] = vm.HISTORY_RECORDS.HISTORY[-1].HOSTNAME server['LABELS'] = labels server['v4_first_ip'] = self._get_vm_ipv4(vm) server['v6_first_ip'] = self._get_vm_ipv6(vm) diff --git a/tests/unit/plugins/inventory/test_opennebula.py b/tests/unit/plugins/inventory/test_opennebula.py index bbc2fe699a..e62724a9e6 100644 --- a/tests/unit/plugins/inventory/test_opennebula.py +++ b/tests/unit/plugins/inventory/test_opennebula.py @@ -58,7 +58,19 @@ def get_vm_pool(): 'ETIME': 0, 'GID': 132, 'GNAME': 'CSApparelVDC', - 'HISTORY_RECORDS': {}, + 'HISTORY_RECORDS': OrderedDict({ + 'HISTORY': OrderedDict({ + 'OID': '42', + 'SEQ': '384', + 'HOSTNAME': 'sam-691-sam', + 'HID': '10', + 'CID': '0', + 'DS_ID': '100', + 'VM_MAD': 'kvm', + 'TM_MAD': '3par', + 'ACTION': '0' + }) + }), 'ID': 7157, 'LAST_POLL': 1632762935, 'LCM_STATE': 3,