From 40f016c214d3a2209c1d1635a9b6eade1d71d716 Mon Sep 17 00:00:00 2001 From: Julia Kreger Date: Sun, 17 Jan 2016 15:43:32 -0500 Subject: [PATCH] Correct inventory script key usage A user reported that they were unable to utilize an inventory that did not have the ipv4_address key present. The user, in the bug report indicated a patch, however the end for bifrost appears to be slightly different since the key could exist but the value could still be None. Added unit test to catch the minimal variable case moving forward. Closes-Bug: #1535117 Change-Id: I0e685342887eaba054a8f80eb529835e48ec6744 --- bifrost/inventory.py | 14 +++++++++----- .../tests/functional/test_inventory_functional.py | 13 +++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/bifrost/inventory.py b/bifrost/inventory.py index 0e57b5e42..d96cf0371 100755 --- a/bifrost/inventory.py +++ b/bifrost/inventory.py @@ -184,12 +184,14 @@ def _process_baremetal_data(data_source, groups, hostvars): for name in file_data: host = file_data[name] # Perform basic validation - if not host['ipv4_address']: + if ('ipv4_address' not in host or + not host['ipv4_address']): host['addressing_mode'] = "dhcp" else: host['ansible_ssh_host'] = host['ipv4_address'] - if 'provisioning_ipv4_address' not in host.keys(): + if ('provisioning_ipv4_address' not in host and + 'addressing_mode' not in host): host['provisioning_ipv4_address'] = host['ipv4_address'] # Add each host to the values to be returned. groups['baremetal']['hosts'].append(host['name']) @@ -229,13 +231,15 @@ def _process_baremetal_csv(data_source, groups, hostvars): host['uuid'] = _val_or_none(row, 9) host['name'] = _val_or_none(row, 10) host['ipv4_address'] = _val_or_none(row, 11) - if not host['ipv4_address']: + if ('ipv4_address' not in host or + not host['ipv4_address']): host['addressing_mode'] = "dhcp" host['provisioning_ipv4_address'] = None else: host['ansible_ssh_host'] = host['ipv4_address'] - - if len(row) > 17: + # Note(TheJulia): We can't assign ipv4_address if we are + # using DHCP. + if (len(row) > 17 and 'addressing_mode' not in host): host['provisioning_ipv4_address'] = row[18] else: host['provisioning_ipv4_address'] = host['ipv4_address'] diff --git a/bifrost/tests/functional/test_inventory_functional.py b/bifrost/tests/functional/test_inventory_functional.py index 8d53154f7..fa4fd268c 100644 --- a/bifrost/tests/functional/test_inventory_functional.py +++ b/bifrost/tests/functional/test_inventory_functional.py @@ -210,3 +210,16 @@ unused,,00000000-0000-0000-0000-000000000002,hostname1, (groups, hostvars) = utils.bifrost_data_conversion( yaml.dump(json.loads(str(expected_hostvars)))) self.assertDictEqual(json.loads(str(expected_hostvars)), hostvars) + + def test_minimal_json(self): + input_json = """{"h0000-01":{"uuid": +"00000000-0000-0000-0001-bad00000010","name":"h0000-01","driver_info" +:{"power":{"ipmi_address":"10.0.0.78","ipmi_username":"ADMIN"," +ipmi_password":"ADMIN"}},"driver":"agent_ipmitool"}}""".replace('\n', '') + expected_json = """{"h0000-01":{"uuid": +"00000000-0000-0000-0001-bad00000010","name":"h0000-01","driver_info" +:{"power":{"ipmi_address":"10.0.0.78","ipmi_username":"ADMIN"," +ipmi_password":"ADMIN"}},"driver":"agent_ipmitool","addressing_mode": +"dhcp"}}""".replace('\n', '') + (groups, hostvars) = utils.bifrost_data_conversion(input_json) + self.assertDictEqual(json.loads(str(expected_json)), hostvars)