summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJernej Jakob <jernej.jakob@gmail.com>2020-05-03 11:22:11 +0200
committerJernej Jakob <jernej.jakob@gmail.com>2020-05-03 11:28:02 +0200
commita1afabff0fa8be39c917cd2fd879c4b68db49f42 (patch)
treef9c798a43da9c471d651a54a404944d3cb96f8a6
parentf03dd18529b8c20c2183ab27e81a8d38699cf3f1 (diff)
downloadvyos-1x-a1afabff0fa8be39c917cd2fd879c4b68db49f42.tar.gz
vyos-1x-a1afabff0fa8be39c917cd2fd879c4b68db49f42.zip
interface: T2367: optimize flow and detriplicate add/del_addr functions
- detriplicate list appending - detriplicate returns - use if-elif-else - move check if address is already added to beginning - move caching in variable to after address assignment so a failed assignment won't cache the address
-rw-r--r--python/vyos/ifconfig/interface.py48
1 files changed, 21 insertions, 27 deletions
diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py
index de5ca369f..5b3da228e 100644
--- a/python/vyos/ifconfig/interface.py
+++ b/python/vyos/ifconfig/interface.py
@@ -638,6 +638,10 @@ class Interface(Control):
# XXX: normalize/compress with ipaddress if calling functions don't?
# is subnet mask always passed, and in the same way?
+ # do not add same address twice
+ if addr in self._addr:
+ return False
+
# we can't have both DHCP and static IPv4 addresses assigned
for a in self._addr:
if ( ( addr == 'dhcp' and a != 'dhcpv6' and is_ipv4(a) ) or
@@ -646,27 +650,20 @@ class Interface(Control):
"Can't configure both static IPv4 and DHCP address "
"on the same interface"))
- # do not add same address twice
- if addr in self._addr:
- return False
-
# add to interface
if addr == 'dhcp':
- self._addr.append(addr)
self.dhcp.v4.set()
- return True
-
- if addr == 'dhcpv6':
- self._addr.append(addr)
+ elif addr == 'dhcpv6':
self.dhcp.v6.set()
- return True
-
- if not is_intf_addr_assigned(self.ifname, addr):
- self._addr.append(addr)
+ elif not is_intf_addr_assigned(self.ifname, addr):
self._cmd(f'ip addr add "{addr}" dev "{self.ifname}"')
- return True
+ else:
+ return False
- return False
+ # add to cache
+ self._addr.append(addr)
+
+ return True
def del_addr(self, addr):
"""
@@ -693,24 +690,21 @@ class Interface(Control):
['2001:db8::ffff/64']
"""
- # remove from cache (dhcp, and dhcpv6 can not be in it)
- if addr in self._addr:
- self._addr.remove(addr)
-
# remove from interface
if addr == 'dhcp':
self.dhcp.v4.delete()
- return True
-
- if addr == 'dhcpv6':
+ elif addr == 'dhcpv6':
self.dhcp.v6.delete()
- return True
-
- if is_intf_addr_assigned(self.ifname, addr):
+ elif is_intf_addr_assigned(self.ifname, addr):
self._cmd(f'ip addr del "{addr}" dev "{self.ifname}"')
- return True
+ else:
+ return False
+
+ # remove from cache
+ if addr in self._addr:
+ self._addr.remove(addr)
- return False
+ return True
def flush_addrs(self):
"""