summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2020-05-02 23:13:14 +0200
committerGitHub <noreply@github.com>2020-05-02 23:13:14 +0200
commitf03dd18529b8c20c2183ab27e81a8d38699cf3f1 (patch)
tree121458d6777d8fccec5c98e79f1dc546d2b18429 /python
parent3501313affa62b88fa530c4518a08269ea6f61c0 (diff)
parent87fc81773dea87338291ffd87f51c343a33ce0c8 (diff)
downloadvyos-1x-f03dd18529b8c20c2183ab27e81a8d38699cf3f1.tar.gz
vyos-1x-f03dd18529b8c20c2183ab27e81a8d38699cf3f1.zip
Merge pull request #389 from thomas-mangin/T2367
interface: T2367: code clarity
Diffstat (limited to 'python')
-rw-r--r--python/vyos/ifconfig/interface.py59
1 files changed, 31 insertions, 28 deletions
diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py
index 6241c83b5..de5ca369f 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):
"""
@@ -720,4 +723,4 @@ class Interface(Control):
self.dhcp.v6.delete()
# flush all addresses
- self._cmd(f'ip addr flush dev "{self.config["ifname"]}"')
+ self._cmd(f'ip addr flush dev "{self.ifname}"')