Remove deleted override vars from inventory

When re-running the dynamic_inventory.py script, either directly or via
a playbook run, deployers expect that their edits to
openstack_user_config.yml are reflected in the inventory. However, previous
behavior only added new entries or updated existing variables with new
values; variables that were deleted remained in the inventory JSON
file.

This patch only corrects behavior for the variables includes in the
global_overrides dictionary. Similar behavior may be present in other
aspects of the inventory, such as removing host vars, but those should
be addressed in later patches.

Closes-Bug: #1588902

Change-Id: I86d6627ef34d99c7ef3c77fcd42c918a13f0fe81
This commit is contained in:
Travis Truman 2016-06-03 11:56:26 -04:00 committed by Jesse Pretorius (odyssey4me)
parent ab8e272f7f
commit f426eb98d1
3 changed files with 56 additions and 3 deletions

View File

@ -871,6 +871,11 @@ def _parse_global_variables(user_cidr, inventory, user_defined_config):
user_defined_config['global_overrides']
)
# Remove global overrides that were deleted from inventory, too
for key in inventory['all']['vars'].keys():
if key not in user_defined_config['global_overrides']:
del inventory['all']['vars'][key]
def append_if(array, item):
"""Append an ``item`` to an ``array`` if its not already in it.

View File

@ -0,0 +1,5 @@
---
fixes:
- Deleting variable entries from the ``global_overrides`` dictionary in
``openstack_user_config.yml`` now properly removes those variables from
the ``openstack_inventory.json`` file. See Bug #1588902.

View File

@ -382,6 +382,12 @@ class TestConfigChecks(unittest.TestCase):
with open(USER_CONFIG_FILE, 'wb') as f:
f.write(yaml.dump(self.user_defined_config))
def restore_config(self):
# get back our initial user config file
os.remove(USER_CONFIG_FILE)
os.rename(USER_CONFIG_FILE + ".tmp", USER_CONFIG_FILE)
self.config_changed = False
def test_missing_container_cidr_network(self):
self.delete_provider_network('container')
with self.assertRaises(SystemExit) as context:
@ -519,9 +525,7 @@ class TestConfigChecks(unittest.TestCase):
def tearDown(self):
if self.config_changed:
# get back our initial user config file
os.remove(USER_CONFIG_FILE)
os.rename(USER_CONFIG_FILE + ".tmp", USER_CONFIG_FILE)
self.restore_config()
class TestStaticRouteConfig(TestConfigChecks):
@ -643,6 +647,45 @@ class TestNetAddressSearch(unittest.TestCase):
self.assertEqual(pns, new_pns)
class TestGlobalOverridesConfigDeletion(TestConfigChecks):
def setUp(self):
super(TestGlobalOverridesConfigDeletion, self).setUp()
self.inventory = get_inventory()
def add_global_override(self, var_name, var_value):
"""Adds an arbitrary name and value to the global_overrides dict."""
overrides = self.user_defined_config['global_overrides']
overrides[var_name] = var_value
def remove_global_override(self, var_name):
"""Removes target key from the global_overrides dict."""
overrides = self.user_defined_config['global_overrides']
del overrides[var_name]
def test_global_overrides_delete_when_merge(self):
"""Vars removed from global overrides are removed from inventory"""
self.add_global_override('foo', 'bar')
di._parse_global_variables({}, self.inventory,
self.user_defined_config)
self.remove_global_override('foo')
di._parse_global_variables({}, self.inventory,
self.user_defined_config)
self.assertNotIn('foo', self.inventory['all']['vars'],
"foo var not removed from group_vars_all")
def test_global_overrides_merge(self):
self.add_global_override('foo', 'bar')
di._parse_global_variables({}, self.inventory,
self.user_defined_config)
self.assertEqual('bar', self.inventory['all']['vars']['foo'])
class TestMultipleRuns(unittest.TestCase):
def test_creating_backup_file(self):
inventory_file_path = os.path.join(TARGET_DIR,