From 2e31655f5bdc2e0f052ba9d703a2816b24b20f0a Mon Sep 17 00:00:00 2001 From: Oliver Walsh Date: Wed, 26 Jul 2023 18:46:10 +0100 Subject: [PATCH] Handle nested inventory groups in export_storage_ips This is causing an exception to be raised in OSPdO during FFU as it will try to run ceph export on any stack with ceph-ansible enabled. Resolves: rhbz#2226845 Change-Id: I69e8ca7c18951a6fe7753b44636767d47efc7a6b --- tripleoclient/export.py | 10 +++-- tripleoclient/tests/test_export.py | 69 ++++++++++++++++++++++-------- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/tripleoclient/export.py b/tripleoclient/export.py index f1a005fd5..5780109d0 100644 --- a/tripleoclient/export.py +++ b/tripleoclient/export.py @@ -174,10 +174,14 @@ def export_storage_ips(stack, config_download_dir=constants.DEFAULT_WORK_DIR, _('Could not read file %s') % file) LOG.error(e) mon_ips = [] - for mon_role in inventory_data['mons']['children'].keys(): - for hostname in inventory_data[mon_role]['hosts']: - ip = inventory_data[mon_role]['hosts'][hostname][ceph_net_key] + + def get_mon_ips(group): + for hostname in inventory_data[group].get('hosts', {}): + ip = inventory_data[group]['hosts'][hostname][ceph_net_key] mon_ips.append(ip) + for child in inventory_data[group].get('children', {}): + get_mon_ips(child) + get_mon_ips('mons') return mon_ips diff --git a/tripleoclient/tests/test_export.py b/tripleoclient/tests/test_export.py index b892cff50..6c3febcdc 100644 --- a/tripleoclient/tests/test_export.py +++ b/tripleoclient/tests/test_export.py @@ -30,24 +30,9 @@ class TestExport(TestCase): self.mock_open = mock.mock_open(read_data='{"an_key":"an_value"}') - ceph_inv = { - 'DistributedComputeHCI': { - 'hosts': { - 'dcn0-distributedcomputehci-0': { - 'foo_ip': '192.168.24.42' - }, - 'dcn0-distributedcomputehci-1': { - 'foo_ip': '192.168.8.8' - } - } - }, - 'mons': { - 'children': { - 'DistributedComputeHCI': {} - } - } - } - self.mock_open_ceph_inv = mock.mock_open(read_data=str(ceph_inv)) + self.mock_open_ceph_inv = mock.mock_open(read_data=str( + self._get_test_inventory() + )) ceph_global = { 'service_net_map': { @@ -65,6 +50,25 @@ class TestExport(TestCase): } self.mock_open_ceph_all = mock.mock_open(read_data=str(ceph_all)) + def _get_test_inventory(self): + return { + 'DistributedComputeHCI': { + 'hosts': { + 'dcn0-distributedcomputehci-0': { + 'foo_ip': '192.168.24.42' + }, + 'dcn0-distributedcomputehci-1': { + 'foo_ip': '192.168.8.8' + } + } + }, + 'mons': { + 'children': { + 'DistributedComputeHCI': {} + } + } + } + def _get_stack_saved_output_item(self, output_key, working_dir): outputs = { 'EndpointMap': dict(em_key='em_value'), @@ -245,3 +249,32 @@ class TestExport(TestCase): self.assertEqual(data, expected) self.mock_open_ceph_all.assert_called_once_with( '/foo/dcn0/ceph-ansible/group_vars/all.yml', 'r') + + +class TestExportNestedInventory(TestExport): + def _get_test_inventory(self): + return { + 'DistributedComputeHCI': { + 'hosts': { + 'dcn0-distributedcomputehci-0': { + 'foo_ip': '192.168.24.42' + }, + 'dcn0-distributedcomputehci-1': { + 'foo_ip': '192.168.8.8' + } + } + }, + 'stackfoo_mons': { + 'children': { + 'DistributedComputeHCI': {} + } + }, + 'mons': { + 'children': { + 'stackfoo_mons': {} + } + } + } + + def test_export_storage_ips(self): + return super().test_export_storage_ips()