diff options
author | Thomas Mangin <thomas.mangin@exa.net.uk> | 2020-05-02 21:17:09 +0100 |
---|---|---|
committer | Thomas Mangin <thomas.mangin@exa.net.uk> | 2020-05-02 21:24:32 +0100 |
commit | 6b920f8a02b0e5bb54a1be6718d8d81ac91b7092 (patch) | |
tree | a9cff53a45aa28b1c5df943fe942c1f8930456ea | |
parent | 3501313affa62b88fa530c4518a08269ea6f61c0 (diff) | |
download | vyos-1x-6b920f8a02b0e5bb54a1be6718d8d81ac91b7092.tar.gz vyos-1x-6b920f8a02b0e5bb54a1be6718d8d81ac91b7092.zip |
interface: T2367: de-imbricate the ifs
-rw-r--r-- | python/vyos/ifconfig/interface.py | 57 |
1 files changed, 30 insertions, 27 deletions
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): """ |