From e771e088c5eada6b9ab0ec5c32fec6fe98d25131 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 (cherry picked from commit e12837353dc3668da7cfc575777ef197df4b0497) --- 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 e8f3c62c..73081b98 100644 --- a/os_net_config/objects.py +++ b/os_net_config/objects.py @@ -262,14 +262,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 edcabf25..d587f954 100644 --- a/os_net_config/tests/test_objects.py +++ b/os_net_config/tests/test_objects.py @@ -98,6 +98,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",