diff options
author | Christian Poessinger <christian@poessinger.com> | 2020-06-23 19:56:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-23 19:56:53 +0200 |
commit | 78d63b91572f5442b0b221951c9bc7b6f24066a7 (patch) | |
tree | 5e7e2d471f00ba4fc96ada91ab95921780971e63 /python/vyos/ifconfig | |
parent | 1d7f88b459da6224086ce1386964a238e08179ca (diff) | |
parent | fba3e4b624529576cbfe59f10dfc1bb3df80634f (diff) | |
download | vyos-1x-78d63b91572f5442b0b221951c9bc7b6f24066a7.tar.gz vyos-1x-78d63b91572f5442b0b221951c9bc7b6f24066a7.zip |
Merge pull request #473 from thomas-mangin/T2630
validation: T2630: bound to interface mtu if available
Diffstat (limited to 'python/vyos/ifconfig')
-rw-r--r-- | python/vyos/ifconfig/control.py | 34 |
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}} |