From b3e74611befb157dc4bf992f23aaba346e655a86 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Wed, 20 Aug 2014 13:10:40 -0400 Subject: Add nic1, nic2 naming abstraction Implements a new active NIC abstraction and naming convention that allows nic1, nic2, etc. to be translated to actual (active) network device names like em1, em2 (or eth0, eth1). This includes some logic to map ordered active nics to the nic1, nic2 naming scheme. Embedded nics are always listed first (in sort order) followed by any other active Nics on the system. With the new code: {"type": "interface", "name": "nic1" } is automatically translated (internally) to: {"type": "interface", "name": "em1" } This works for all top level "interface" devices, vlans, bonds, and bridges alike. For vlans the 'device' name is translated instead of the device name per vlan object conventions. --- os_net_config/objects.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'os_net_config/objects.py') diff --git a/os_net_config/objects.py b/os_net_config/objects.py index 14ecfc6..b73122c 100644 --- a/os_net_config/objects.py +++ b/os_net_config/objects.py @@ -14,9 +14,17 @@ # License for the specific language governing permissions and limitations # under the License. +import logging import netaddr from openstack.common import strutils +from os_net_config import utils + + +logger = logging.getLogger(__name__) + +_NUMBERED_NICS = None + class InvalidConfigException(ValueError): pass @@ -43,6 +51,19 @@ def _get_required_field(json, name, object_name): return field +def _numbered_nics(): + global _NUMBERED_NICS + if _NUMBERED_NICS: + return _NUMBERED_NICS + _NUMBERED_NICS = {} + count = 0 + for nic in utils.ordered_active_nics(): + count += 1 + _NUMBERED_NICS["nic%i" % count] = nic + logger.info("nic%i mapped to: %s" % (count, nic)) + return _NUMBERED_NICS + + class Route(object): """Base class for network routes.""" @@ -80,7 +101,11 @@ class _BaseOpts(object): def __init__(self, name, use_dhcp=False, use_dhcpv6=False, addresses=[], routes=[], mtu=1500, primary=False): - self.name = name + numbered_nic_names = _numbered_nics() + if name in numbered_nic_names: + self.name = numbered_nic_names[name] + else: + self.name = name self.mtu = mtu self.use_dhcp = use_dhcp self.use_dhcpv6 = use_dhcpv6 @@ -171,7 +196,12 @@ class Vlan(_BaseOpts): super(Vlan, self).__init__(name, use_dhcp, use_dhcpv6, addresses, routes, mtu, primary) self.vlan_id = int(vlan_id) - self.device = device + + numbered_nic_names = _numbered_nics() + if device in numbered_nic_names: + self.device = numbered_nic_names[device] + else: + self.device = device @staticmethod def from_json(json): -- cgit 1.2.3-korg