From e12837353dc3668da7cfc575777ef197df4b0497 Mon Sep 17 00:00:00 2001 From: Dan Sneddon Date: Fri, 27 Jul 2018 03:46:34 -0700 Subject: [PATCH] Fix numbered NIC mapping when using dotted VLAN notation When declaring VLANs using dotted notation (such as em1.10 for VLAN 10 on interface em1), numbered NICs are not properly mapped to real interface names. This patch fixes the handling of the mapped NIC names inside the BaseOpts object that is the base class for all interface types. This allows an interface name of "nic1.10" to be mapped to, e.g. "em1.10" Closes-bug: #1783206 Change-Id: Ibd9b28128900d9e959b82eb8673b8380bf20606a --- os_net_config/objects.py | 14 +++++++++++--- os_net_config/tests/test_objects.py | 9 +++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/os_net_config/objects.py b/os_net_config/objects.py index 24c531d8..65c02d2c 100644 --- a/os_net_config/objects.py +++ b/os_net_config/objects.py @@ -283,14 +283,22 @@ class _BaseOpts(object): self.hwaddr = None self.hwname = None self.renamed = False - if name in mapped_nic_names: + # Split name to support . format, e.g. em1.10 or nic1.10 + if len(name.split('.')) > 1 and name.split('.')[1].isdigit(): + base_name = name.split('.')[0] + vlan_suffix = '.%s' % name.split('.')[1] + else: + base_name = name + vlan_suffix = '' + if base_name in mapped_nic_names: if persist_mapping: self.name = name - self.hwname = mapped_nic_names[name] + self.hwname = '%s%s' % (mapped_nic_names[base_name], + vlan_suffix) self.hwaddr = utils.interface_mac(self.hwname) self.renamed = True else: - self.name = mapped_nic_names[name] + self.name = '%s%s' % (mapped_nic_names[base_name], vlan_suffix) else: self.name = name diff --git a/os_net_config/tests/test_objects.py b/os_net_config/tests/test_objects.py index b857027e..501d7564 100644 --- a/os_net_config/tests/test_objects.py +++ b/os_net_config/tests/test_objects.py @@ -120,6 +120,15 @@ class TestInterface(base.TestCase): self.assertEqual("em1", interface.name) self.assertTrue(interface.use_dhcp) + def test_from_json_dotted_vlan(self): + def dummy_mapped_nics(nic_mapping=None): + return {"nic1": "em3"} + self.stub_out('os_net_config.objects.mapped_nics', dummy_mapped_nics) + + data = '{"type": "interface", "name": "nic1.10", "use_dhcp": true}' + interface = objects.object_from_json(json.loads(data)) + self.assertEqual("em3.10", interface.name) + def test_from_json_hotplug(self): data = """{ "type": "interface",