From e835aef3a0ebbc957abd38e54f8040f5b67157ce Mon Sep 17 00:00:00 2001 From: Thomas Mangin Date: Sat, 11 Apr 2020 14:27:57 +0100 Subject: ifconfig: T2223: rename Register to Section While the class does indeed all the registration, it work is really to map classes to interface section. ie: interface ethernet -> EthernetIf Therefore it can also list which interface are from which type, therefore the name change. Other function name will also be renamed as a consequence --- python/vyos/ifconfig/__init__.py | 1 + python/vyos/ifconfig/control.py | 4 +- python/vyos/ifconfig/register.py | 95 ---------------------------------- python/vyos/ifconfig/section.py | 95 ++++++++++++++++++++++++++++++++++ src/completion/list_openvpn_clients.py | 4 +- 5 files changed, 100 insertions(+), 99 deletions(-) delete mode 100644 python/vyos/ifconfig/register.py create mode 100644 python/vyos/ifconfig/section.py diff --git a/python/vyos/ifconfig/__init__.py b/python/vyos/ifconfig/__init__.py index 1f9956af0..cd1696ca1 100644 --- a/python/vyos/ifconfig/__init__.py +++ b/python/vyos/ifconfig/__init__.py @@ -14,6 +14,7 @@ # License along with this library. If not, see . +from vyos.ifconfig.section import Section from vyos.ifconfig.interface import Interface from vyos.ifconfig.bond import BondIf diff --git a/python/vyos/ifconfig/control.py b/python/vyos/ifconfig/control.py index c7a2fa2d6..464cd585e 100644 --- a/python/vyos/ifconfig/control.py +++ b/python/vyos/ifconfig/control.py @@ -18,10 +18,10 @@ import os from vyos.util import debug, debug_msg from vyos.util import popen, cmd -from vyos.ifconfig.register import Register +from vyos.ifconfig.section import Section -class Control(Register): +class Control(Section): _command_get = {} _command_set = {} diff --git a/python/vyos/ifconfig/register.py b/python/vyos/ifconfig/register.py deleted file mode 100644 index c90782b70..000000000 --- a/python/vyos/ifconfig/register.py +++ /dev/null @@ -1,95 +0,0 @@ -# Copyright 2020 VyOS maintainers and contributors -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see . - -import netifaces - - -class Register: - # the known interface prefixes - _prefixes = {} - - # class need to define: definition['prefixes'] - # the interface prefixes declared by a class used to name interface with - # prefix[0-9]*(\.[0-9]+)?(\.[0-9]+)?, such as lo, eth0 or eth0.1.2 - - @classmethod - def register(cls, klass): - if not klass.definition.get('prefixes',[]): - raise RuntimeError(f'valid interface prefixes not defined for {klass.__name__}') - - for ifprefix in klass.definition['prefixes']: - if ifprefix in cls._prefixes: - raise RuntimeError(f'only one class can be registered for prefix "{ifprefix}" type') - cls._prefixes[ifprefix] = klass - - return klass - - @classmethod - def _basename (cls, name, vlan): - # remove number from interface name - name = name.rstrip('0123456789') - name = name.rstrip('.') - if vlan: - name = name.rstrip('0123456789') - return name - - @classmethod - def section(cls, name, vlan=True): - # return the name of a section an interface should be under - name = cls._basename(name, vlan) - - # XXX: To leave as long as vti and input are not moved to vyos - if name == 'vti': - return 'vti' - if name == 'ifb': - return 'input' - - if name in cls._prefixes: - return cls._prefixes[name].definition['section'] - return '' - - @classmethod - def klass(cls, name, vlan=True): - name = cls._basename(name, vlan) - if name in cls._prefixes: - return cls._prefixes[name] - raise ValueError(f'No type found for interface name: {name}') - - @classmethod - def _listing (cls,section=''): - interfaces = netifaces.interfaces() - - for ifname in interfaces: - # XXX: Temporary hack as vti and input are not yet moved from vyatta to vyos - if ifname.startswith('vti') or ifname.startswith('input'): - yield ifname - continue - - ifsection = cls.section(ifname) - if not ifsection: - continue - - if section and ifsection != section: - continue - - yield ifname - - @classmethod - def listing(cls, section=''): - return list(cls._listing(section)) - - -# XXX: TODO - limit name for VRF interfaces - diff --git a/python/vyos/ifconfig/section.py b/python/vyos/ifconfig/section.py new file mode 100644 index 000000000..eeec1968a --- /dev/null +++ b/python/vyos/ifconfig/section.py @@ -0,0 +1,95 @@ +# Copyright 2020 VyOS maintainers and contributors +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see . + +import netifaces + + +class Section: + # the known interface prefixes + _prefixes = {} + + # class need to define: definition['prefixes'] + # the interface prefixes declared by a class used to name interface with + # prefix[0-9]*(\.[0-9]+)?(\.[0-9]+)?, such as lo, eth0 or eth0.1.2 + + @classmethod + def register(cls, klass): + if not klass.definition.get('prefixes',[]): + raise RuntimeError(f'valid interface prefixes not defined for {klass.__name__}') + + for ifprefix in klass.definition['prefixes']: + if ifprefix in cls._prefixes: + raise RuntimeError(f'only one class can be registered for prefix "{ifprefix}" type') + cls._prefixes[ifprefix] = klass + + return klass + + @classmethod + def _basename (cls, name, vlan): + # remove number from interface name + name = name.rstrip('0123456789') + name = name.rstrip('.') + if vlan: + name = name.rstrip('0123456789') + return name + + @classmethod + def section(cls, name, vlan=True): + # return the name of a section an interface should be under + name = cls._basename(name, vlan) + + # XXX: To leave as long as vti and input are not moved to vyos + if name == 'vti': + return 'vti' + if name == 'ifb': + return 'input' + + if name in cls._prefixes: + return cls._prefixes[name].definition['section'] + return '' + + @classmethod + def klass(cls, name, vlan=True): + name = cls._basename(name, vlan) + if name in cls._prefixes: + return cls._prefixes[name] + raise ValueError(f'No type found for interface name: {name}') + + @classmethod + def _listing (cls,section=''): + interfaces = netifaces.interfaces() + + for ifname in interfaces: + # XXX: Temporary hack as vti and input are not yet moved from vyatta to vyos + if ifname.startswith('vti') or ifname.startswith('input'): + yield ifname + continue + + ifsection = cls.section(ifname) + if not ifsection: + continue + + if section and ifsection != section: + continue + + yield ifname + + @classmethod + def listing(cls, section=''): + return list(cls._listing(section)) + + +# XXX: TODO - limit name for VRF interfaces + diff --git a/src/completion/list_openvpn_clients.py b/src/completion/list_openvpn_clients.py index 17b0c7008..177ac90c9 100755 --- a/src/completion/list_openvpn_clients.py +++ b/src/completion/list_openvpn_clients.py @@ -18,7 +18,7 @@ import os import sys import argparse -from vyos.ifconfig import Interface +from vyos.ifconfig import Section def get_client_from_interface(interface): clients = [] @@ -50,7 +50,7 @@ if __name__ == "__main__": if args.interface: clients = get_client_from_interface(args.interface) elif args.all: - for interface in Interface.listing("openvpn"): + for interface in Section.interfaces("openvpn"): clients += get_client_from_interface(interface) print(" ".join(clients)) -- cgit v1.2.3