summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2020-12-15 13:53:13 -0600
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2020-12-15 13:56:23 -0600
commit084720544fb176fb116e32f780ad15934ea1720e (patch)
tree87941f29705e3e4df38f868c584b20c1dc722a90
parentb4ec63029b8917e1b41b4b95252703c13197f53c (diff)
downloadnetconfapk-084720544fb176fb116e32f780ad15934ea1720e.tar.gz
netconfapk-084720544fb176fb116e32f780ad15934ea1720e.tar.bz2
netconfapk-084720544fb176fb116e32f780ad15934ea1720e.tar.xz
netconfapk-084720544fb176fb116e32f780ad15934ea1720e.zip
ifupdown-ng NMSA: Implement removal and updating of IPs
-rw-r--r--ncserver/module/ip.py8
-rw-r--r--ncserver/module/nms_ifupdownng.py58
2 files changed, 60 insertions, 6 deletions
diff --git a/ncserver/module/ip.py b/ncserver/module/ip.py
index 0f3eff1..84eab13 100644
--- a/ncserver/module/ip.py
+++ b/ncserver/module/ip.py
@@ -289,8 +289,8 @@ def _edit_ipv4(session, rpc, node, def_op, iface: str):
if qparam.localname in _params.keys():
param = _params[qparam.localname]
log_config_change(session, "[ietf-ip %s]" % iface,
- "IPv4 %s: -> %s" % (param, node.text))
- _edit_param(iface, param, operation, rpc, node)
+ "IPv4 %s: -> %s" % (param, xparam.text))
+ _edit_param(iface, param, operation, rpc, xparam)
elif qparam.localname == 'address':
# Oh no.
raise NotImplementedError
@@ -369,8 +369,8 @@ def _edit_ipv6(session, rpc, node, def_op, iface: str):
if qparam.localname in _params.keys():
param = _params[qparam.localname]
log_config_change(session, "[ietf-ip %s]" % iface,
- "IPv6 %s: -> %s" % (param, node.text))
- _edit_param(iface, param, p_op, rpc, node)
+ "IPv6 %s: -> %s" % (param, xparam.text))
+ _edit_param(iface, param, p_op, rpc, xparam)
elif qparam.localname == 'address':
# Oh no.
raise NotImplementedError
diff --git a/ncserver/module/nms_ifupdownng.py b/ncserver/module/nms_ifupdownng.py
index 16183ea..a607215 100644
--- a/ncserver/module/nms_ifupdownng.py
+++ b/ncserver/module/nms_ifupdownng.py
@@ -187,6 +187,19 @@ def _replace_one(iface: str, key: str, value: str):
return
+def _replace_one_in_list(iface: str, key: str, old: str, value: str):
+ """Replace +old+ with +value+ in +iface+'s list of +key+."""
+ iface_cfg = _CONFIG[iface]
+ for item in iface_cfg:
+ if key in item.keys():
+ if item[key] == old:
+ item[key] = value
+ _save()
+ return
+
+ raise ValueError(old)
+
+
def _remove_one(iface: str, key: str):
"""Remove a single instance of +key+ from +iface+."""
iface_cfg = _CONFIG[iface]
@@ -761,6 +774,35 @@ def add_address(iface: str, _type, addr: str, prefix):
_add_one_to_list(iface, 'address', s_addr)
+def modify_prefix(iface: str, addr: str, prefix: str):
+ """Change the prefix of existing IP address +addr+ to +prefix+."""
+ _load_config()
+
+ if iface not in _CONFIG.keys():
+ LOGGER.warning(
+ _("attempted to change addr prefix on non-existent interface %s"),
+ iface
+ )
+ return
+
+ if '/' in addr:
+ # Wow, the entire address! Much simpler.
+ the_ip = addr.split('/')[0]
+ if addr not in list_addresses(iface):
+ raise KeyError("Non-existent address cannot be modified")
+ _replace_one_in_list(iface, 'address', addr, the_ip + '/' + prefix)
+ return
+
+ for candidate in list_addresses(iface):
+ cand_addr = candidate.split('/')[0]
+ if addr == cand_addr:
+ _replace_one_in_list(iface, 'address', candidate,
+ addr + '/' + prefix)
+ return
+
+ raise KeyError("Non-existent address cannot be modified")
+
+
def remove_address(iface: str, addr: str):
"""Remove an address from the specified interface."""
_load_config()
@@ -772,8 +814,20 @@ def remove_address(iface: str, addr: str):
)
return
- # implement this.
- raise NotImplementedError
+ if '/' in addr:
+ # We have the entire address, including the prefix length.
+ if addr not in list_addresses(iface):
+ raise KeyError("Non-existent address cannot be removed")
+ _remove_one_from_list(iface, 'address', addr)
+ return
+
+ for candidate in list_addresses(iface):
+ cand_addr = candidate.split('/')[0]
+ if addr == cand_addr:
+ _remove_one_from_list(iface, 'address', candidate)
+ return
+
+ raise KeyError("Non-existent address cannot be removed")
def running(_):