From 084720544fb176fb116e32f780ad15934ea1720e Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Tue, 15 Dec 2020 13:53:13 -0600 Subject: ifupdown-ng NMSA: Implement removal and updating of IPs --- ncserver/module/ip.py | 8 +++--- ncserver/module/nms_ifupdownng.py | 58 +++++++++++++++++++++++++++++++++++++-- 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(_): -- cgit v1.2.3-60-g2f50