Move network_entry function to top level

This change adds some minor testing around the build process for a
network's dictionary. This function may be further refactored in the
future, but this serves as a start for that work.

Change-Id: I63506e863386bd91a59d10530f3e81a444692cf7
This commit is contained in:
Nolan Brubaker 2016-08-11 16:27:33 -04:00 committed by Jesse Pretorius (odyssey4me)
parent bb3ddb31c7
commit 3b6895be67
2 changed files with 54 additions and 24 deletions

View File

@ -526,6 +526,31 @@ def _load_optional_q(config, cidr_name):
return ip_q
def network_entry(is_metal, interface,
bridge=None, net_type=None, net_mtu=None):
"""Return a network entry for a container."""
# TODO(cloudnull) After a few releases this conditional should be
# simplified. The container address checking that is ssh address
# is only being done to support old inventory.
if is_metal:
_network = dict()
else:
_network = {'interface': interface}
if bridge:
_network['bridge'] = bridge
if net_type:
_network['type'] = net_type
if net_mtu:
_network['mtu'] = net_mtu
return _network
def _add_additional_networks(key, inventory, ip_q, q_name, netmask, interface,
bridge, net_type, net_mtu, user_config,
is_ssh_address, is_container_address,
@ -547,28 +572,6 @@ def _add_additional_networks(key, inventory, ip_q, q_name, netmask, interface,
:param is_container_address: ``bol`` set this address to container_address.
:param static_routes: ``list`` List containing static route dicts.
"""
def network_entry():
"""Return a network entry for a container."""
# TODO(cloudnull) After a few releases this conditional should be
# simplified. The container address checking that is ssh address
# is only being done to support old inventory.
if is_metal:
_network = dict()
else:
_network = {'interface': interface}
if bridge:
_network['bridge'] = bridge
if net_type:
_network['type'] = net_type
if net_mtu:
_network['mtu'] = net_mtu
return _network
base_hosts = inventory['_meta']['hostvars']
lookup = inventory.get(key, list())
@ -625,7 +628,13 @@ def _add_additional_networks(key, inventory, ip_q, q_name, netmask, interface,
# This should convert found addresses based on q_name + "_address"
# and then build the network if its not found.
if not is_metal and old_address not in networks:
network = networks[old_address] = network_entry()
network = networks[old_address] = network_entry(
is_metal,
interface,
bridge,
net_type,
net_mtu
)
if old_address in container and container[old_address]:
network['address'] = container.pop(old_address)
elif not is_metal:
@ -635,7 +644,13 @@ def _add_additional_networks(key, inventory, ip_q, q_name, netmask, interface,
network['netmask'] = netmask
elif is_metal:
network = networks[old_address] = network_entry()
network = networks[old_address] = network_entry(
is_metal,
interface,
bridge,
net_type,
net_mtu
)
network['netmask'] = netmask
if is_ssh_address or is_container_address:
# Container physical host group

View File

@ -1029,5 +1029,20 @@ class TestConfigCheckFunctional(TestConfigCheckBase):
self.assertEqual(context.exception.ip, '172.29.236.100')
class TestNetworkEntry(unittest.TestCase):
def test_all_args_filled(self):
entry = di.network_entry(True, 'eth1', 'br-mgmt', 'my_type', '1700')
self.assertNotIn('interface', entry.keys())
self.assertEqual(entry['bridge'], 'br-mgmt')
self.assertEqual(entry['type'], 'my_type')
self.assertEqual(entry['mtu'], '1700')
def test_container_dict(self):
entry = di.network_entry(False, 'eth1', 'br-mgmt', 'my_type', '1700')
self.assertEqual(entry['interface'], 'eth1')
if __name__ == '__main__':
unittest.main()