diff --git a/validator/bondethernet.py b/validator/bondethernet.py index 814a34a..0a03505 100644 --- a/validator/bondethernet.py +++ b/validator/bondethernet.py @@ -6,14 +6,15 @@ class NullHandler(logging.Handler): pass -def exists(yaml, ifname): - """ Return True if the BondEthernet exists """ +def get_by_name(yaml, ifname): + """ Return the BondEthernet by name, if it exists. Return None otherwise. """ try: if ifname in yaml['bondethernets']: - return True + return yaml['bondethernets'][ifname] except: pass - return False + return None + def bondethernet(args, yaml): result = True @@ -27,7 +28,7 @@ def bondethernet(args, yaml): for ifname, iface in yaml['bondethernets'].items(): logger.debug("bondethernet %s: %s" % (ifname, iface)) for member in iface['interfaces']: - if not interface.exists(yaml, member): + if not interface.get_by_name(yaml, member): msgs.append("bondethernet %s member %s doesn't exist" % (ifname, member)) result = False diff --git a/validator/interface.py b/validator/interface.py index 992c6fb..62df7b3 100644 --- a/validator/interface.py +++ b/validator/interface.py @@ -5,6 +5,25 @@ class NullHandler(logging.Handler): def emit(self, record): pass +def get_by_name(yaml, ifname): + """ Returns the interface or sub-interface by a given name, or None if it does not exist """ + if '.' in ifname: + ifname, subid = ifname.split('.') + subid = int(subid) + try: + iface = yaml['interfaces'][ifname]['sub-interfaces'][subid] + return iface + except: + return None + + try: + iface = yaml['interfaces'][ifname] + return iface + except: + pass + return None + + def has_sub(yaml, ifname): """ Returns True if this interface has sub-interfaces """ if not 'interfaces' in yaml: @@ -16,6 +35,7 @@ def has_sub(yaml, ifname): return True return False + def has_address(yaml, ifname): """ Returns True if this interface or sub-interface has one or more addresses""" if not 'interfaces' in yaml: @@ -38,6 +58,7 @@ def has_address(yaml, ifname): pass return False + def is_bond_member(yaml, ifname): """ Returns True if this interface is a member of a BondEthernet """ if not 'bondethernets' in yaml: @@ -50,6 +71,7 @@ def is_bond_member(yaml, ifname): return True return False + def has_lcp(yaml, ifname): """ Returns True if this interface or sub-interface has an LCP""" if not 'interfaces' in yaml: @@ -72,19 +94,6 @@ def has_lcp(yaml, ifname): pass return False -def exists(yaml, ifname): - """ Returns true if ifname exists as a phy or sub-int """ - try: - if ifname in yaml['interfaces']: - return True - if '.' in ifname: - ifname, subid = ifname.split('.') - subid = int(subid) - if subid in yaml['interfaces'][ifname]['sub-interfaces']: - return True - except: - pass - return False def valid_encapsulation(yaml, sub_ifname): try: @@ -117,7 +126,7 @@ def interface(args, yaml): for ifname, iface in yaml['interfaces'].items(): logger.debug("interface %s" % iface) - if ifname.startswith("BondEthernet") and not bondethernet.exists(yaml, ifname): + if ifname.startswith("BondEthernet") and not bondethernet.get_by_name(yaml, ifname): msgs.append("interface %s does not exist in bondethernets" % ifname) result = False diff --git a/validator/loopback.py b/validator/loopback.py index 6cc9011..1073f96 100644 --- a/validator/loopback.py +++ b/validator/loopback.py @@ -4,14 +4,14 @@ class NullHandler(logging.Handler): def emit(self, record): pass -def exists(yaml, ifname): - """ Returns true if ifname exists as a loopback """ +def get_by_name(yaml, ifname): + """ Return the loopback by name, if it exists. Return None otherwise. """ try: if ifname in yaml['loopbacks']: - return True + return yaml['loopbacks'][ifname] except: pass - return False + return None def loopback(args, yaml):