From 758715df97dea209bffd1a935b70a765a80d2d81 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Fri, 30 Aug 2019 12:16:09 +0200 Subject: Python/ifconfig: replace up()/down() with 'state' property Commit cb1b72c5c ("Python/ifconfig: replace linkstate() with up()/down() methods") replaced the linkstate property in favour of up()/down() functions. Instead it really makes more sense to have a propery to also query the current linkstate from sysfs. --- python/vyos/interfaceconfig.py | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/python/vyos/interfaceconfig.py b/python/vyos/interfaceconfig.py index 9fd0d83f9..5bed90494 100644 --- a/python/vyos/interfaceconfig.py +++ b/python/vyos/interfaceconfig.py @@ -205,38 +205,32 @@ class Interface: with open('/sys/class/net/{0}/ifalias'.format(self._ifname), 'w') as f: f.write(str(ifalias)) - - def up(self): + @property + def state(self): """ - Bring interface up + Enable (up) / Disable (down) an interface Example: from vyos.interfaceconfig import Interface - i = Interface('br100', type='bridge') - i.up() + i = Interface('ens192').link """ - # Assemble command executed on system. Unfortunately there is no way - # to up/down an interface via sysfs - cmd = 'ip link set dev "{}" up'.format(self._ifname) - self._cmd(cmd) - + state = '' + with open('/sys/class/net/{0}/operstate'.format(self._ifname), 'r') as f: + state = f.read().rstrip('\n') + return state - def down(self): - """ - Bring interface down - Example: + @state.setter + def state(self, state=None): - from vyos.interfaceconfig import Interface - i = Interface('br100', type='bridge') - i.down() - """ + if state not in ['up', 'down']: + raise ValueError('state must be "up" or "down"') # Assemble command executed on system. Unfortunately there is no way # to up/down an interface via sysfs - cmd = 'ip link set dev "{}" down'.format(self._ifname) + cmd = 'ip link set dev "{}" "{}"'.format(self._ifname, state) self._cmd(cmd) -- cgit v1.2.3