From bf9b37cbb01db5acab30ba9a33c915c40730d377 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 30 May 2017 16:15:15 +0200 Subject: [PATCH] library: add NMUtil.connection_ensure_setting() helper --- library/network_connections.py | 26 +++++++++---------- library/test_network_connections.py | 39 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/library/network_connections.py b/library/network_connections.py index 9d66ca4..f825694 100755 --- a/library/network_connections.py +++ b/library/network_connections.py @@ -1103,6 +1103,13 @@ class NMUtil: nmclient = Util.NM().Client.new(None) self.nmclient = nmclient + def connection_ensure_setting(self, connection, setting_type): + setting = connection.get_setting(setting_type) + if not setting: + setting = setting_type.new() + connection.add_setting(setting) + return setting + def device_is_master_type(self, dev): if dev: NM = Util.NM() @@ -1197,8 +1204,7 @@ class NMUtil: connection = connections[idx] con = NM.SimpleConnection.new() - s_con = NM.SettingConnection.new() - con.add_setting(s_con) + s_con = self.connection_ensure_setting(con, NM.SettingConnection) s_con.set_property(NM.SETTING_CONNECTION_ID, connection['name']) s_con.set_property(NM.SETTING_CONNECTION_UUID, connection['nm.uuid']) @@ -1207,21 +1213,18 @@ class NMUtil: if connection['type'] == 'ethernet': s_con.set_property(NM.SETTING_CONNECTION_TYPE, '802-3-ethernet') - s_wired = NM.SettingWired.new() - con.add_setting(s_wired) + s_wired = self.connection_ensure_setting(con, NM.SettingWired) s_wired.set_property(NM.SETTING_WIRED_MAC_ADDRESS, connection['mac']) elif connection['type'] == 'bridge': s_con.set_property(NM.SETTING_CONNECTION_TYPE, 'bridge') - s_bridge = NM.SettingBridge.new() - con.add_setting(s_bridge) + s_bridge = self.connection_ensure_setting(con, NM.SettingBridge) s_bridge.set_property(NM.SETTING_BRIDGE_STP, False) elif connection['type'] == 'bond': s_con.set_property(NM.SETTING_CONNECTION_TYPE, 'bond') elif connection['type'] == 'team': s_con.set_property(NM.SETTING_CONNECTION_TYPE, 'team') elif connection['type'] == 'vlan': - s_vlan = NM.SettingVlan.new() - con.add_setting(s_vlan) + s_vlan = self.connection_ensure_setting(con, NM.SettingVlan) s_vlan.set_property(NM.SETTING_VLAN_ID, int(connection['vlan_id'])) s_vlan.set_property(NM.SETTING_VLAN_PARENT, self._connection_find_master_uuid(connection['parent'], connections, idx)) else: @@ -1233,8 +1236,8 @@ class NMUtil: else: ip = connection['ip'] - s_ip4 = NM.SettingIP4Config.new() - s_ip6 = NM.SettingIP6Config.new() + s_ip4 = self.connection_ensure_setting(con, NM.SettingIP4Config) + s_ip6 = self.connection_ensure_setting(con, NM.SettingIP6Config) s_ip4.set_property(NM.SETTING_IP_CONFIG_METHOD, 'auto') s_ip6.set_property(NM.SETTING_IP_CONFIG_METHOD, 'auto') @@ -1277,9 +1280,6 @@ class NMUtil: if not d['is_v4']: s_ip6.add_dns(d['address']) - con.add_setting(s_ip4) - con.add_setting(s_ip6) - try: con.normalize() except Exception as e: diff --git a/library/test_network_connections.py b/library/test_network_connections.py index 19ef018..6c66d0b 100755 --- a/library/test_network_connections.py +++ b/library/test_network_connections.py @@ -8,6 +8,23 @@ sys.path.insert(1, os.path.dirname(os.path.abspath(__file__))) import network_connections as n +try: + my_test_skipIf = unittest.skipIf +except AttributeError: + # python 2.6 workaround + def my_test_skipIf(condition, reason): + if condition: + return lambda x: None + else: + return lambda x: x + +try: + nmutil = n.NMUtil() +except: + # NMUtil is not supported, for example on RHEL 6 or without + # pygobject. + nmutil = None + class TestValidator(unittest.TestCase): def assertValidationError(self, v, value): @@ -107,6 +124,8 @@ class TestValidator(unittest.TestCase): def test_1(self): + self.maxDiff = None + self.assertEqual( [], n.AnsibleUtil.ARGS_CONNECTIONS.validate([]), @@ -383,6 +402,26 @@ class TestValidator(unittest.TestCase): self.assertValidationError(n.AnsibleUtil.ARGS_CONNECTIONS, [ { 'name': 'b', 'type': 'ethernet', 'mac': 'aa:b' } ]) +@my_test_skipIf(nmutil is None, 'no support for NM (libnm via pygobject)') +class TestNM(unittest.TestCase): + + def test_connection_ensure_setting(self): + + NM = n.Util.NM() + GObject = n.Util.GObject() + + con = NM.SimpleConnection.new() + self.assertIsNotNone(con) + self.assertTrue(GObject.type_is_a(con, NM.Connection)) + + s = nmutil.connection_ensure_setting(con, NM.SettingWired) + self.assertIsNotNone(s) + self.assertTrue(GObject.type_is_a(s, NM.SettingWired)) + + s2 = nmutil.connection_ensure_setting(con, NM.SettingWired) + self.assertIsNotNone(s2) + self.assertIs(s, s2) + self.assertTrue(GObject.type_is_a(s, NM.SettingWired)) if __name__ == '__main__': unittest.main()