From 336f9465cb1b60741eb1c56d9083b0376bd37d0c Mon Sep 17 00:00:00 2001 From: Samori Gorse Date: Mon, 22 Nov 2021 13:53:25 +0100 Subject: [PATCH] Xen orchestra inventory: Added groups, keyed_groups and compose support (#3766) * Xen orchestra inventory: Added groups, keyed_groups and compose support * Update plugins/inventory/xen_orchestra.py Remove extra params declaration Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein --- plugins/inventory/xen_orchestra.py | 13 +++++++++++++ .../unit/plugins/inventory/test_xen_orchestra.py | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/plugins/inventory/xen_orchestra.py b/plugins/inventory/xen_orchestra.py index af4b327c53..1e19051fe8 100644 --- a/plugins/inventory/xen_orchestra.py +++ b/plugins/inventory/xen_orchestra.py @@ -69,6 +69,11 @@ simple_config_file: password: xo_pwd validate_certs: true use_ssl: true + groups: + kube_nodes: "'kube_node' in tags" + compose: + ansible_port: 2222 + ''' import json @@ -165,6 +170,12 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): 'hosts': self.get_object('host'), } + def _apply_constructable(self, name, variables): + strict = self.get_option('strict') + self._add_host_to_composed_groups(self.get_option('groups'), variables, name, strict=strict) + self._add_host_to_keyed_groups(self.get_option('keyed_groups'), variables, name, strict=strict) + self._set_composite_vars(self.get_option('compose'), variables, name, strict=strict) + def _add_vms(self, vms, hosts, pools): for uuid, vm in vms.items(): group = 'with_ip' @@ -213,6 +224,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): self.inventory.set_variable( entry_name, 'os_version', vm['os_version']) + self._apply_constructable(entry_name, self.inventory.get_host(entry_name).get_vars()) + def _add_hosts(self, hosts, pools): for host in hosts.values(): entry_name = host['uuid'] diff --git a/tests/unit/plugins/inventory/test_xen_orchestra.py b/tests/unit/plugins/inventory/test_xen_orchestra.py index 39ff60a602..4916446c13 100644 --- a/tests/unit/plugins/inventory/test_xen_orchestra.py +++ b/tests/unit/plugins/inventory/test_xen_orchestra.py @@ -129,6 +129,19 @@ objects = { } +def get_option(option): + if option == 'groups': + return {} + elif option == 'keyed_groups': + return [] + elif option == 'compose': + return {} + elif option == 'strict': + return False + else: + return None + + def serialize_groups(groups): return list(map(str, groups)) @@ -144,7 +157,8 @@ def test_verify_file_bad_config(inventory): assert inventory.verify_file('foobar.xen_orchestra.yml') is False -def test_populate(inventory): +def test_populate(inventory, mocker): + inventory.get_option = mocker.MagicMock(side_effect=get_option) inventory._populate(objects) actual = sorted(inventory.inventory.hosts.keys()) expected = sorted(['c96ec4dd-28ac-4df4-b73c-4371bd202728', '222d8594-9426-468a-ad69-7a6f02330fa3',