Fix error during 'inventory-manage.py -r' calls

Use load_inventory instead for load_from_json. Rename
load_from_json to _load_from_json.

Move most of the --clear-ip and --remove calls to their
corresponding functions. Modified other calls to account
for the change

Add a test to test for inventory item removal.

fixes bug: 1663328

Change-Id: I2dbecb085383b3d02e298cc09b02566d1e52a064
(cherry picked from commit 3a08bb7b78)
This commit is contained in:
Joel Griffiths 2017-02-13 23:06:14 +00:00 committed by Jesse Pretorius (odyssey4me)
parent 89881f15bd
commit 7df772c0fc
3 changed files with 49 additions and 13 deletions

View File

@ -197,7 +197,7 @@ def write_hostnames(save_path, hostnames_ips):
)
def load_from_json(filename, preferred_path=None, raise_if_missing=True):
def _load_from_json(filename, preferred_path=None, raise_if_missing=True):
"""Return a dictionary found in json format in a given file
:param filename: ``str`` Name of the file to read from
@ -217,7 +217,7 @@ def load_from_json(filename, preferred_path=None, raise_if_missing=True):
return dictionary, target_file
def load_inventory(preferred_path=None, default_inv=None):
def load_inventory(preferred_path=None, default_inv=None, filename=None):
"""Create an inventory dictionary from the given source file or a default
inventory. If an inventory is found then a backup tarball is created
as well.
@ -230,7 +230,12 @@ def load_inventory(preferred_path=None, default_inv=None):
or should have been loaded from.
"""
inventory, file_loaded = load_from_json(INVENTORY_FILENAME, preferred_path,
if filename:
inv_fn = filename
else:
inv_fn = INVENTORY_FILENAME
inventory, file_loaded = _load_from_json(inv_fn, preferred_path,
raise_if_missing=False)
if file_loaded is not False:
load_path = os.path.dirname(file_loaded)

View File

@ -40,6 +40,7 @@ def args():
required=False,
default='openstack_inventory.json'
)
parser.add_argument(
'-s',
'--sort',
@ -281,9 +282,11 @@ def export_host_info(inventory):
return export_info
def remove_ip_addresses(inventory):
def remove_ip_addresses(inventory, filepath=None):
"""Removes container IP address information from the inventory dictionary
Writes the changes into the inventory file in filepath if specified
All container_networks information for containers will be deleted.
"""
hostvars = inventory['_meta']['hostvars']
@ -299,6 +302,26 @@ def remove_ip_addresses(inventory):
for ip_var in ip_vars:
variables.pop(ip_var, None)
if filepath is not None:
inventory_json = json.dumps(inventory, indent=2,
separators=(',', ': '))
filesys.save_inventory(inventory_json, filepath)
def remove_inventory_item(remove_item, inventory, filepath=None):
"""Removes inventory item from the inventory dictionary
Writes the changes into the inventory file in filepath if available
All container_networks information for containers will be deleted.
"""
du.recursive_dict_removal(inventory, remove_item)
if filepath is not None:
inventory_json = json.dumps(inventory, indent=2,
separators=(',', ': '))
filesys.save_inventory(inventory_json, filepath)
def main():
"""Run the main application."""
@ -306,7 +329,7 @@ def main():
user_args = args()
# Get the contents of the system inventory
inventory, filename = filesys.load_from_json(user_args['file'])
inventory, filepath = filesys.load_inventory(filename=user_args['file'])
# Make a table with hosts in the left column and details about each in the
# columns to the right
@ -323,16 +346,10 @@ def main():
elif user_args['export'] is True:
print(json.dumps(export_host_info(inventory), indent=2))
elif user_args['clear_ips'] is True:
remove_ip_addresses(inventory)
inventory_json = json.dumps(inventory, indent=2,
separators=(',', ': '))
filesys.save_inventory(inventory_json, filename)
remove_ip_addresses(inventory, filepath)
print('Success. . .')
else:
du.recursive_dict_removal(inventory, user_args['remove_item'])
inventory_json = json.dumps(inventory, indent=2,
seprators=(',', ': '))
filesys.save_inventory(inventory_json, filename)
remove_inventory_item(user_args['remove_item'], inventory, filepath)
print('Success. . .')
if __name__ == "__main__":

View File

@ -19,6 +19,7 @@ import test_inventory
import unittest
MANAGE_DIR = path.join(os.getcwd(), 'lib')
TARGET_DIR = path.join(os.getcwd(), 'tests', 'inventory')
sys.path.append(MANAGE_DIR)
@ -78,6 +79,7 @@ class TestRemoveIpfunction(unittest.TestCase):
def test_ips_removed(self):
mi.remove_ip_addresses(self.inv)
mi.remove_ip_addresses(self.inv, TARGET_DIR)
hostvars = self.inv['_meta']['hostvars']
for host, variables in hostvars.items():
@ -86,6 +88,18 @@ class TestRemoveIpfunction(unittest.TestCase):
continue
self.assertFalse(has_networks)
def test_inventory_item_removed(self):
inventory = self.inv
# Make sure we have log_hosts in the original inventory
self.assertIn('log_hosts', inventory)
mi.remove_inventory_item("log_hosts", inventory)
mi.remove_inventory_item("log_hosts", inventory, TARGET_DIR)
# No make sure it's gone
self.assertIn('log_hosts', inventory)
def test_metal_ips_kept(self):
mi.remove_ip_addresses(self.inv)
hostvars = self.inv['_meta']['hostvars']