summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2020-11-18 13:59:38 -0600
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2020-11-18 13:59:38 -0600
commit557adae9056c81247e713ad4fb133c24db6e8b7f (patch)
treea59fd9a37b7f631f1a9ae65a149904c3811bf7f2
parentb6098ddf2e634b95f0255fbba78ae047199da4d4 (diff)
downloadnetconfapk-557adae9056c81247e713ad4fb133c24db6e8b7f.tar.gz
netconfapk-557adae9056c81247e713ad4fb133c24db6e8b7f.tar.bz2
netconfapk-557adae9056c81247e713ad4fb133c24db6e8b7f.tar.xz
netconfapk-557adae9056c81247e713ad4fb133c24db6e8b7f.zip
ifupdown_ng NMSA: Implement list_addresses
-rw-r--r--ncserver/module/nms_ifupdownng.py63
1 files changed, 59 insertions, 4 deletions
diff --git a/ncserver/module/nms_ifupdownng.py b/ncserver/module/nms_ifupdownng.py
index 9066a18..d5d5da8 100644
--- a/ncserver/module/nms_ifupdownng.py
+++ b/ncserver/module/nms_ifupdownng.py
@@ -10,6 +10,7 @@ with this source distribution for more information.
SPDX-License-Identifier: NCSA
"""
+import ipaddress
import logging
import subprocess
import yaml
@@ -55,12 +56,11 @@ def _load_config():
# Won't load during a transaction.
if _TRANSACTION:
- LOGGER.warning(_("attempted to load eni config during transaction"))
return
result = None
try:
- result = subprocess.run(['/sbin/ifupdown', 'ifparse', '-AF',
+ result = subprocess.run(['/sbin/ifparse', '-AF',
'yaml-raw'],
stdout=subprocess.PIPE, check=False)
except OSError:
@@ -98,9 +98,36 @@ def _save():
conf_file.write(eni[:-1])
+def _find_one(iface: str, key: str):
+ """Find a single instance of configuration +key+ for +iface+."""
+ if iface not in _CONFIG.keys():
+ return None
+
+ for item in _CONFIG[iface]:
+ if key in item.keys():
+ return item[key]
+
+ return None
+
+
+def _find_many(iface: str, key: str) -> list:
+ """Find n instances of configuration +key+ for +iface+."""
+ ret = list()
+
+ if iface not in _CONFIG.keys():
+ return None
+
+ for item in _CONFIG[iface]:
+ if key in item.keys():
+ ret.append(item[key])
+
+ return ret
+
+
def interface_list():
"""Return a list of configured interfaces."""
_load_config()
+
return tuple(_CONFIG.keys())
@@ -128,6 +155,8 @@ def commit():
def get_param(iface: str, parameter: str):
"""Retrieve the parameter for the specified interface."""
+ _load_config()
+
if iface not in _CONFIG.keys():
LOGGER.warning(
_("requested parameter %s for non-existant interface %s"),
@@ -141,6 +170,8 @@ def get_param(iface: str, parameter: str):
def set_param(iface: str, parameter: str, value):
"""Set the parameter for the specified interface."""
+ _load_config()
+
if iface not in _CONFIG.keys():
LOGGER.warning(
_("attempted to set parameter %s for non-existant interface %s"),
@@ -154,6 +185,8 @@ def set_param(iface: str, parameter: str, value):
def unset_param(iface: str, parameter: str):
"""Unset the parameter for the specified interface."""
+ _load_config()
+
if iface not in _CONFIG.keys():
LOGGER.warning(
_("attempted to unset parameter %s for non-existant interface %s"),
@@ -167,17 +200,32 @@ def unset_param(iface: str, parameter: str):
def list_addresses(iface: str) -> list:
"""Retrieve all configured addresses for the specified interface."""
+ _load_config()
+
if iface not in _CONFIG.keys():
LOGGER.warning(_("requested addresses for non-existant interface %s"),
iface)
return list()
- # implement this.
- raise NotImplementedError
+ # Per comment in lif_address_format_cidr, this is the right thing to do.
+ fallback_prefix = "24"
+ netmask = _find_one(iface, 'netmask')
+ if netmask:
+ net = ipaddress.IPv4Network('0.0.0.0/'+netmask)
+ fallback_prefix = str(net.prefixlen)
+
+ addrs = _find_many(iface, 'address')
+ def fixup(addr):
+ if '/' not in addr:
+ addr = addr + "/" + fallback_prefix
+ return addr
+ return list(map(fixup, addrs))
def add_address(iface: str, _type, addr: str, prefix):
"""Add an address of the specified ``type`` to the specified interface."""
+ _load_config()
+
if iface not in _CONFIG.keys():
LOGGER.warning(
_("attempted to add address to non-existant interface %s"), iface
@@ -190,6 +238,8 @@ def add_address(iface: str, _type, addr: str, prefix):
def remove_address(iface: str, addr: str):
"""Remove an address from the specified interface."""
+ _load_config()
+
if iface not in _CONFIG.keys():
LOGGER.warning(
_("attempted to remove address from non-existant interface %s"),
@@ -199,3 +249,8 @@ def remove_address(iface: str, addr: str):
# implement this.
raise NotImplementedError
+
+
+# Load immediately when we're loaded so we can go straight to a transaction
+# if desired.
+_load_config()