summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2019-08-30 12:16:09 +0200
committerChristian Poessinger <christian@poessinger.com>2019-08-30 12:16:09 +0200
commit758715df97dea209bffd1a935b70a765a80d2d81 (patch)
tree13e9498fed1a2075a67307a32059359bfceb6034
parent83ded2c50e750acb73294ddde5383d8ffdf746ed (diff)
downloadvyos-1x-758715df97dea209bffd1a935b70a765a80d2d81.tar.gz
vyos-1x-758715df97dea209bffd1a935b70a765a80d2d81.zip
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.
-rw-r--r--python/vyos/interfaceconfig.py32
1 files 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)