From 0ba7123d8d5edcf79038c582c84b93e44e186a84 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 31 May 2017 15:09:11 +0200 Subject: [PATCH] library: accept space-separated string for list This allows for ip: address: '192.168.1.5/24' instead of ip: address: - '192.168.1.5/24' --- library/network_connections.py | 7 ++ library/test_network_connections.py | 144 ++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) diff --git a/library/network_connections.py b/library/network_connections.py index 45964a0..1240204 100755 --- a/library/network_connections.py +++ b/library/network_connections.py @@ -579,6 +579,13 @@ class ArgValidatorList(ArgValidator): def construct_name(self, name): return ('' if n is None else n) + name def _validate(self, value, name): + + if isinstance(value, Util.STRING_TYPE): + # we expect a list. However, for convenience allow to + # specify a string, separated by space. Escaping is + # not supported. If you need that, define a proper list. + value = [s for s in value.split(' ') if s] + result = [] for (idx, v) in enumerate(value): try: diff --git a/library/test_network_connections.py b/library/test_network_connections.py index b2eca25..55fd799 100755 --- a/library/test_network_connections.py +++ b/library/test_network_connections.py @@ -234,6 +234,150 @@ class TestValidator(unittest.TestCase): ]), ) + self.assertEqual( + [ + { + 'autoconnect': True, + 'name': 'prod1', + 'parent': None, + 'ip': { + 'ip_is_present': True, + 'dhcp4': False, + 'route_metric6': None, + 'route_metric4': None, + 'dns_search': [], + 'dhcp4_send_hostname': None, + 'gateway6': None, + 'gateway4': None, + 'auto6': True, + 'dns': [], + 'address': [ + { + 'is_v4': True, + 'prefix': 24, + 'family': 2, + 'address': '192.168.174.5' + } + ] + }, + 'state': 'up', + 'mtu': 1450, + 'check_iface_exists': True, + 'mac': '52:54:00:44:9f:ba', + 'master': None, + 'vlan_id': None, + 'ignore_errors': None, + 'interface_name': None, + 'type': 'ethernet', + 'slave_type': None, + 'wait': None + }, + ], + n.AnsibleUtil.ARGS_CONNECTIONS.validate([ + { + 'name': 'prod1', + 'state': 'up', + 'type': 'ethernet', + 'autoconnect': 'yes', + 'mac': '52:54:00:44:9f:ba', + 'mtu': 1450, + 'ip': { + 'address': '192.168.174.5/24', + } + } + ]), + ) + + self.assertEqual( + [ + { + 'autoconnect': True, + 'name': 'prod1', + 'parent': None, + 'ip': { + 'ip_is_present': False, + 'dhcp4': True, + 'auto6': True, + 'address': [], + 'route_metric6': None, + 'route_metric4': None, + 'dns_search': [], + 'dhcp4_send_hostname': None, + 'gateway6': None, + 'gateway4': None, + 'dns': [] + }, + 'state': 'up', + 'mtu': 1450, + 'check_iface_exists': True, + 'mac': '52:54:00:44:9f:ba', + 'master': None, + 'vlan_id': None, + 'ignore_errors': None, + 'interface_name': None, + 'type': 'ethernet', + 'slave_type': None, + 'wait': None + }, + { + 'autoconnect': True, + 'name': 'prod.100', + 'parent': 'prod1', + 'ip': { + 'ip_is_present': True, + 'dhcp4': False, + 'route_metric6': None, + 'route_metric4': None, + 'dns_search': [], + 'dhcp4_send_hostname': None, + 'gateway6': None, + 'gateway4': None, + 'auto6': True, + 'dns': [], + 'address': [ + { + 'is_v4': True, + 'prefix': 24, + 'family': 2, + 'address': '192.168.174.5' + } + ] + }, + 'mac': None, + 'mtu': None, + 'check_iface_exists': True, + 'state': 'up', + 'master': None, + 'slave_type': None, + 'ignore_errors': None, + 'interface_name': 'prod.100', + 'type': 'vlan', + 'vlan_id': 100, + 'wait': None + } + ], + n.AnsibleUtil.ARGS_CONNECTIONS.validate([ + { + 'name': 'prod1', + 'state': 'up', + 'type': 'ethernet', + 'autoconnect': 'yes', + 'mac': '52:54:00:44:9f:ba', + 'mtu': 1450, + }, + { + 'name': 'prod.100', + 'state': 'up', + 'type': 'vlan', + 'parent': 'prod1', + 'vlan_id': '100', + 'ip': { + 'address': [ '192.168.174.5/24' ], + } + } + ]), + ) + self.assertValidationError(n.AnsibleUtil.ARGS_CONNECTIONS, [ { } ]) self.assertValidationError(n.AnsibleUtil.ARGS_CONNECTIONS,