summaryrefslogtreecommitdiff
path: root/python/vyos
diff options
context:
space:
mode:
Diffstat (limited to 'python/vyos')
-rw-r--r--python/vyos/ifconfig/control.py22
-rw-r--r--python/vyos/ifconfig/interface.py22
2 files changed, 24 insertions, 20 deletions
diff --git a/python/vyos/ifconfig/control.py b/python/vyos/ifconfig/control.py
index 508b4e279..635b626e8 100644
--- a/python/vyos/ifconfig/control.py
+++ b/python/vyos/ifconfig/control.py
@@ -29,12 +29,13 @@ class Control:
def _cmd(self, command):
p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True)
tmp = p.communicate()[0].strip()
- self._debug_msg("cmd '{}'".format(command))
- if tmp.decode():
- self._debug_msg("returned:\n{}".format(tmp.decode()))
-
- # do we need some error checking code here?
- return tmp.decode()
+ self._debug_msg(f"cmd '{command}'")
+ decoded = tmp.decode()
+ if decoded:
+ self._debug_msg(f"returned:\n{decoded}")
+ if p.returncode != 0:
+ raise RuntimeError(f'{command}\nreturned: {decoded}')
+ return decoded
def _get_command(self, config, name):
"""
@@ -55,14 +56,17 @@ class Control:
validate = self._command_set[name].get('validate', None)
if validate:
- validate(value)
-
- config = {**config, **{'value': value}}
+ try:
+ validate(value)
+ except Exception as e:
+ raise e.__class__(f'Could not set {name}. {e}')
convert = self._command_set[name].get('convert', None)
if convert:
value = convert(value)
+ config = {**config, **{'value': value}}
+
cmd = self._command_set[name]['shellcmd'].format(**config)
return self._cmd(cmd)
diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py
index b002e0171..21ffa88f6 100644
--- a/python/vyos/ifconfig/interface.py
+++ b/python/vyos/ifconfig/interface.py
@@ -77,6 +77,10 @@ class Interface(Control):
}
_command_set = {
+ 'state': {
+ 'validate': lambda v: assert_list(v, ['up', 'down']),
+ 'shellcmd': 'ip link set dev {ifname} {value}',
+ },
'mac': {
'validate': assert_mac,
'shellcmd': 'ip link set dev {ifname} address {value}',
@@ -166,13 +170,15 @@ class Interface(Control):
if k in kargs:
self.config[k] = kargs[k]
- for k in self.required:
- if k not in kargs:
- raise ConfigError('missing required option {} for {}'.format(k,self.__class__))
-
if not os.path.exists('/sys/class/net/{}'.format(self.config['ifname'])):
if not self.config['type']:
raise Exception('interface "{}" not found'.format(self.config['ifname']))
+
+ for k in self.required:
+ if k not in kargs:
+ name = self.default['type']
+ raise ConfigError(f'missing required option {k} for {name} {ifname} creation')
+
self._create()
# per interface DHCP config files
@@ -452,13 +458,7 @@ class Interface(Control):
>>> Interface('eth0').get_state()
'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 {} {}'.format(self.config['ifname'], state)
- return self._cmd(cmd)
+ return self.set_interface('state', state)
def set_proxy_arp(self, enable):
"""