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 e12837353d)
This commit is contained in:
Dan Sneddon 2018-07-27 03:46:34 -07:00 committed by Tim Rozet
parent 9084bb5e57
commit e771e088c5
2 changed files with 20 additions and 3 deletions

View File

@ -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 <nic>.<vlan_id> 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

View File

@ -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",