From 8bc12847568582d3b34f3859946470af1344f9ea Mon Sep 17 00:00:00 2001 From: Thomas Mangin Date: Sat, 11 Apr 2020 16:42:40 +0100 Subject: completion: T2238: use interface data Use the data in the default dict of the interface to generate the output of list interface. --- python/vyos/ifconfig/section.py | 23 ++++++++++++++++++++-- src/completion/list_interfaces.py | 40 ++++++++++++--------------------------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/python/vyos/ifconfig/section.py b/python/vyos/ifconfig/section.py index 4f76258a7..234c9a6cc 100644 --- a/python/vyos/ifconfig/section.py +++ b/python/vyos/ifconfig/section.py @@ -19,6 +19,7 @@ import netifaces class Section: # the known interface prefixes _prefixes = {} + _classes = [] # class need to define: definition['prefixes'] # the interface prefixes declared by a class used to name interface with @@ -34,6 +35,8 @@ class Section: if not klass.definition.get('prefixes',[]): raise RuntimeError(f'valid interface prefixes not defined for {klass.__name__}') + cls._classes.append(klass) + for ifprefix in klass.definition['prefixes']: if ifprefix in cls._prefixes: raise RuntimeError(f'only one class can be registered for prefix "{ifprefix}" type') @@ -110,6 +113,22 @@ class Section: """ return list(cls._intf_under_section(section)) + @classmethod + def _intf_with_feature(cls, feature=''): + """ + return a generator with the name of the interface which have + a particular feature set in their definition such as: + bondable, broadcast, bridgeable, ... + """ + for klass in cls._classes: + if klass.definition[feature]: + yield klass.definition['section'] -# XXX: TODO - limit name for VRF interfaces - + @classmethod + def feature(cls, feature=''): + """ + return list with the name of the interface which have + a particular feature set in their definition such as: + bondable, broadcast, bridgeable, ... + """ + return list(cls._intf_with_feature(feature)) diff --git a/src/completion/list_interfaces.py b/src/completion/list_interfaces.py index 64aecef60..e27281433 100755 --- a/src/completion/list_interfaces.py +++ b/src/completion/list_interfaces.py @@ -4,6 +4,13 @@ import sys import argparse from vyos.ifconfig import Section + +def matching(feature): + for section in Section.feature(feature): + for intf in Section.interfaces(section): + yield intf + + parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group() group.add_argument("-t", "--type", type=str, help="List interfaces of specific type") @@ -13,46 +20,23 @@ group.add_argument("-bo", "--bondable", action="store_true", help="List all bond args = parser.parse_args() -# XXX: Need to be rewritten using the data in the class definition -# XXX: It can be done once vti and input are moved into vyos -# XXX: We store for each class what type they are (broadcast, bridgeabe, ...) - if args.type: try: interfaces = Section.interfaces(args.type) - + print(" ".join(interfaces)) except ValueError as e: print(e, file=sys.stderr) print("") elif args.broadcast: - eth = Section.interfaces("ethernet") - bridge = Section.interfaces("bridge") - bond = Section.interfaces("bonding") - interfaces = eth + bridge + bond + print(" ".join(matching("broadcast"))) elif args.bridgeable: - eth = Section.interfaces("ethernet") - bond = Section.interfaces("bonding") - l2tpv3 = Section.interfaces("l2tpv3") - openvpn = Section.interfaces("openvpn") - wireless = Section.interfaces("wireless") - tunnel = Section.interfaces("tunnel") - vxlan = Section.interfaces("vxlan") - geneve = Section.interfaces("geneve") - - interfaces = eth + bond + l2tpv3 + openvpn + vxlan + tunnel + wireless + geneve + print(" ".join(matching("bridgeable"))) elif args.bondable: - interfaces = [] - eth = Section.interfaces("ethernet") - # we need to filter out VLAN interfaces identified by a dot (.) in their name - for intf in eth: - if not '.' in intf: - interfaces.append(intf) + print(" ".join([intf for intf in matching("bondable") if '.' not in intf])) else: - interfaces = Section.interfaces() - -print(" ".join(interfaces)) + print(" ".join(Section.interfaces())) -- cgit v1.2.3