From 6b920f8a02b0e5bb54a1be6718d8d81ac91b7092 Mon Sep 17 00:00:00 2001 From: Thomas Mangin Date: Sat, 2 May 2020 21:17:09 +0100 Subject: interface: T2367: de-imbricate the ifs --- python/vyos/ifconfig/interface.py | 57 ++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'python') diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py index 6241c83b5..1f932c884 100644 --- a/python/vyos/ifconfig/interface.py +++ b/python/vyos/ifconfig/interface.py @@ -637,7 +637,6 @@ class Interface(Control): """ # XXX: normalize/compress with ipaddress if calling functions don't? # is subnet mask always passed, and in the same way? - ret = True # we can't have both DHCP and static IPv4 addresses assigned for a in self._addr: @@ -648,23 +647,26 @@ class Interface(Control): "on the same interface")) # do not add same address twice - if addr not in self._addr: - # add to interface - if addr == 'dhcp': - self.dhcp.v4.set() - elif addr == 'dhcpv6': - self.dhcp.v6.set() - else: - if not is_intf_addr_assigned(self.ifname, addr): - ret = self._cmd(f'ip addr add "{addr}" dev "{self.ifname}"') - else: - return False - # add to cache - self._addr.append(addr) - else: + if addr in self._addr: return False - return ret + # add to interface + if addr == 'dhcp': + self._addr.append(addr) + self.dhcp.v4.set() + return True + + if addr == 'dhcpv6': + self._addr.append(addr) + self.dhcp.v6.set() + return True + + if not is_intf_addr_assigned(self.ifname, addr): + self._addr.append(addr) + self._cmd(f'ip addr add "{addr}" dev "{self.ifname}"') + return True + + return False def del_addr(self, addr): """ @@ -690,24 +692,25 @@ class Interface(Control): >>> j.get_addr() ['2001:db8::ffff/64'] """ - ret = True + + # 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() - elif addr == 'dhcpv6': + return True + + if addr == 'dhcpv6': self.dhcp.v6.delete() - else: - if is_intf_addr_assigned(self.ifname, addr): - ret = self._cmd(f'ip addr del "{addr}" dev "{self.ifname}"') - else: - return False + return True - if addr in self._addr: - # remove from cache - self._addr.remove(addr) + if is_intf_addr_assigned(self.ifname, addr): + self._cmd(f'ip addr del "{addr}" dev "{self.ifname}"') + return True - return ret + return False def flush_addrs(self): """ -- cgit v1.2.3