summaryrefslogtreecommitdiff
path: root/python/vyos/ifconfig/control.py
diff options
context:
space:
mode:
authorThomas Mangin <thomas.mangin@exa.net.uk>2020-06-23 18:24:29 +0100
committerThomas Mangin <thomas.mangin@exa.net.uk>2020-06-23 18:24:29 +0100
commitfba3e4b624529576cbfe59f10dfc1bb3df80634f (patch)
treebad69c9e8914e932df7f628478ffdc124d95669c /python/vyos/ifconfig/control.py
parent70d45cdec4263a0eff1146656ae8e8012ab125b6 (diff)
downloadvyos-1x-fba3e4b624529576cbfe59f10dfc1bb3df80634f.tar.gz
vyos-1x-fba3e4b624529576cbfe59f10dfc1bb3df80634f.zip
validation: T2630: bound to interface mtu if available
Diffstat (limited to 'python/vyos/ifconfig/control.py')
-rw-r--r--python/vyos/ifconfig/control.py34
1 files changed, 32 insertions, 2 deletions
diff --git a/python/vyos/ifconfig/control.py b/python/vyos/ifconfig/control.py
index 0958be642..a6fc8ac6c 100644
--- a/python/vyos/ifconfig/control.py
+++ b/python/vyos/ifconfig/control.py
@@ -15,6 +15,8 @@
import os
+from inspect import signature
+from inspect import _empty
from vyos import debug
from vyos.util import popen
@@ -25,6 +27,7 @@ from vyos.ifconfig.section import Section
class Control(Section):
_command_get = {}
_command_set = {}
+ _signature = {}
def __init__(self, **kargs):
# some commands (such as operation comands - show interfaces, etc.)
@@ -54,6 +57,30 @@ class Control(Section):
cmd = self._command_get[name]['shellcmd'].format(**config)
return self._command_get[name].get('format', lambda _: _)(self._cmd(cmd))
+ def _values(self, name, validate, value):
+ """
+ looks at the validation function "validate"
+ for the interface sysfs or command and
+ returns a dict with the right options to call it
+ """
+ if name not in self._signature:
+ self._signature[name] = signature(validate)
+
+ values = {}
+
+ for k in self._signature[name].parameters:
+ default = self._signature[name].parameters[k].default
+ if default is not _empty:
+ continue
+ if k == 'self':
+ values[k] = self
+ elif k == 'ifname':
+ values[k] = self.ifname
+ else:
+ values[k] = value
+
+ return values
+
def _set_command(self, config, name, value):
"""
Using the defined names, set data write to sysfs.
@@ -64,7 +91,7 @@ class Control(Section):
validate = self._command_set[name].get('validate', None)
if validate:
try:
- validate(value)
+ validate(**self._values(name, validate, value))
except Exception as e:
raise e.__class__(f'Could not set {name}. {e}')
@@ -124,7 +151,10 @@ class Control(Section):
validate = self._sysfs_set[name].get('validate', None)
if validate:
- validate(value)
+ try:
+ validate(**self._values(name, validate, value))
+ except Exception as e:
+ raise e.__class__(f'Could not set {name}. {e}')
config = {**config, **{'value': value}}