summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Mangin <thomas.mangin@exa.net.uk>2020-04-04 16:11:07 +0100
committerThomas Mangin <thomas.mangin@exa.net.uk>2020-04-04 16:25:46 +0100
commitae72ab6de76dbc86cb881da7eafa64413819a9bc (patch)
tree1d4cc663cd627ed484ccfecb804575e606ecbcd8
parentf91a8869cb1ab3acc605a93789e9310f33dbd979 (diff)
downloadvyos-1x-ae72ab6de76dbc86cb881da7eafa64413819a9bc.tar.gz
vyos-1x-ae72ab6de76dbc86cb881da7eafa64413819a9bc.zip
ifconfig: T2190: option to prevent Interface creation
a new option was added to the Interface class "create". By default the value is set to True, and when an instance of the class is created and the underlying interface does not exists, the class will create it. If the option "create" is set to False, the interface will not be created and instead the class will raise an error when it is instantiated.
-rw-r--r--python/vyos/ifconfig/interface.py25
-rwxr-xr-xsrc/op_mode/wireguard.py2
2 files changed, 20 insertions, 7 deletions
diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py
index 4a34f96d6..96057a943 100644
--- a/python/vyos/ifconfig/interface.py
+++ b/python/vyos/ifconfig/interface.py
@@ -39,6 +39,8 @@ class Interface(DHCP):
required = []
default = {
'type': '',
+ 'debug': True,
+ 'create': True,
}
definition = {
'section': '',
@@ -174,15 +176,26 @@ class Interface(DHCP):
super().__init__(ifname, **kargs)
if not os.path.exists('/sys/class/net/{}'.format(self.config['ifname'])):
+ # Any instance of Interface, such as Interface('eth0')
+ # can be used safely to access the generic function in this class
+ # as 'type' is unset, the class can not be created
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()
+ # Should an Instance of a child class (EthernetIf, DummyIf, ..)
+ # be required, then create should be set to False to not accidentally create it.
+ # In case a subclass does not define it, we use get to set the default to True
+ if self.config.get('create',True):
+ 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()
+ # If we can not connect to the interface then let the caller know
+ # as the class could not be correctly initialised
+ else:
+ raise Exception('interface "{}" not found'.format(self.config['ifname']))
# list of assigned IP addresses
self._addr = []
diff --git a/src/op_mode/wireguard.py b/src/op_mode/wireguard.py
index 512c80dda..c684f8a47 100755
--- a/src/op_mode/wireguard.py
+++ b/src/op_mode/wireguard.py
@@ -150,7 +150,7 @@ if __name__ == '__main__':
if args.listkdir:
list_key_dirs()
if args.showinterface:
- intf = WireGuardIf(args.showinterface, debug=False)
+ intf = WireGuardIf(args.showinterface, create=False, debug=False)
intf.op_show_interface()
if args.delkdir:
if args.location: