summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2020-07-01 20:59:17 +0200
committerGitHub <noreply@github.com>2020-07-01 20:59:17 +0200
commit3c973c0852de9342a94ae3df7c3d927b0a600032 (patch)
treefe703729f5f50799fff70a5b9cc53fa16393521b
parent9afc69f83f5e6f44751c3d6c546ea7614baff41c (diff)
parentb4a60249f5c26da1d5d837de9122d7d87948507b (diff)
downloadvyos-1x-3c973c0852de9342a94ae3df7c3d927b0a600032.tar.gz
vyos-1x-3c973c0852de9342a94ae3df7c3d927b0a600032.zip
Merge pull request #486 from jestabro/sub_dict
-rw-r--r--python/vyos/config.py18
-rw-r--r--python/vyos/util.py30
-rwxr-xr-xsrc/conf_mode/interfaces-dummy.py2
-rwxr-xr-xsrc/conf_mode/interfaces-loopback.py2
-rwxr-xr-xsrc/conf_mode/interfaces-macsec.py2
-rwxr-xr-xsrc/conf_mode/interfaces-wirelessmodem.py2
-rwxr-xr-xsrc/conf_mode/ssh.py2
-rwxr-xr-xsrc/conf_mode/system_console.py2
-rwxr-xr-xsrc/conf_mode/vrf.py2
9 files changed, 49 insertions, 13 deletions
diff --git a/python/vyos/config.py b/python/vyos/config.py
index 56353c322..0bbfece6e 100644
--- a/python/vyos/config.py
+++ b/python/vyos/config.py
@@ -71,7 +71,6 @@ import subprocess
import vyos.util
import vyos.configtree
-
class VyOSError(Exception):
"""
Raised on config access errors, most commonly if the type of a config tree node
@@ -288,17 +287,24 @@ class Config(object):
self.__session_env = save_env
return(default)
- def get_config_dict(self, path=[], effective=False, key_mangling=None):
+ def get_config_dict(self, path=[], effective=False, key_mangling=None, get_first_key=False):
"""
Args: path (str list): Configuration tree path, can be empty
Returns: a dict representation of the config
"""
- res = self.show_config(self._make_path(path), effective=effective)
- if res:
+ res = self.show_config(effective=effective)
+ config_dict = {}
+ if not res:
+ return config_dict
+ else:
config_tree = vyos.configtree.ConfigTree(res)
config_dict = json.loads(config_tree.to_json())
- else:
- config_dict = {}
+ config_dict = vyos.util.get_sub_dict(config_dict, self._make_path(path))
+ if get_first_key:
+ tmp = next(iter(config_dict.values()))
+ if not isinstance(tmp, dict):
+ raise TypeError("Data under node is not of type dict")
+ config_dict = tmp
if key_mangling:
if not (isinstance(key_mangling, tuple) and \
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 0ddc14963..268eaa69a 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -364,6 +364,36 @@ def mangle_dict_keys(data, regex, replacement):
return new_dict
+def _get_sub_dict(d, lpath):
+ k = lpath[0]
+ if k not in d.keys():
+ return {}
+ c = {k: d[k]}
+ lpath = lpath[1:]
+ if not lpath:
+ return c
+ elif not isinstance(c[k], dict):
+ return {}
+ return _get_sub_dict(c[k], lpath)
+
+def get_sub_dict(source, lpath):
+ """ Returns the sub-dict of a nested dict, defined by path of keys.
+
+ Args:
+ source (dict): Source dict to extract from
+ lpath (list[str]): sequence of keys
+
+ Returns: {key : source[..]..[key]} for key the last element of lpath, if exists
+ {} otherwise
+ """
+ if not isinstance(source, dict):
+ raise TypeError("source must be of type dict")
+ if not isinstance(lpath, list):
+ raise TypeError("path must be of type list")
+ if not lpath:
+ return source
+ return _get_sub_dict(source, lpath)
+
def process_running(pid_file):
""" Checks if a process with PID in pid_file is running """
from psutil import pid_exists
diff --git a/src/conf_mode/interfaces-dummy.py b/src/conf_mode/interfaces-dummy.py
index 1877bc3f7..2d62420a6 100755
--- a/src/conf_mode/interfaces-dummy.py
+++ b/src/conf_mode/interfaces-dummy.py
@@ -40,7 +40,7 @@ def get_config():
ifname = os.environ['VYOS_TAGNODE_VALUE']
base = ['interfaces', 'dummy', ifname]
- dummy = conf.get_config_dict(base, key_mangling=('-', '_'))
+ dummy = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True)
# Check if interface has been removed
if dummy == {}:
dummy.update({'deleted' : ''})
diff --git a/src/conf_mode/interfaces-loopback.py b/src/conf_mode/interfaces-loopback.py
index 7c3d8663d..2368f88a9 100755
--- a/src/conf_mode/interfaces-loopback.py
+++ b/src/conf_mode/interfaces-loopback.py
@@ -35,7 +35,7 @@ def get_config():
ifname = os.environ['VYOS_TAGNODE_VALUE']
base = ['interfaces', 'loopback', ifname]
- loopback = conf.get_config_dict(base, key_mangling=('-', '_'))
+ loopback = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True)
# Check if interface has been removed
if loopback == {}:
loopback.update({'deleted' : ''})
diff --git a/src/conf_mode/interfaces-macsec.py b/src/conf_mode/interfaces-macsec.py
index 2f14b6a92..56273f71a 100755
--- a/src/conf_mode/interfaces-macsec.py
+++ b/src/conf_mode/interfaces-macsec.py
@@ -53,7 +53,7 @@ def get_config():
ifname = os.environ['VYOS_TAGNODE_VALUE']
base = base + [ifname]
- macsec = conf.get_config_dict(base, key_mangling=('-', '_'))
+ macsec = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True)
# Check if interface has been removed
if macsec == {}:
tmp = {
diff --git a/src/conf_mode/interfaces-wirelessmodem.py b/src/conf_mode/interfaces-wirelessmodem.py
index 49a036104..ec5a85e54 100755
--- a/src/conf_mode/interfaces-wirelessmodem.py
+++ b/src/conf_mode/interfaces-wirelessmodem.py
@@ -62,7 +62,7 @@ def get_config():
ifname = os.environ['VYOS_TAGNODE_VALUE']
base = base + [ifname]
- wwan = conf.get_config_dict(base, key_mangling=('-', '_'))
+ wwan = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True)
# Check if interface has been removed
if wwan == {}:
wwan.update({'deleted' : ''})
diff --git a/src/conf_mode/ssh.py b/src/conf_mode/ssh.py
index 1ca2c8b4c..ffb0b700d 100755
--- a/src/conf_mode/ssh.py
+++ b/src/conf_mode/ssh.py
@@ -37,7 +37,7 @@ def get_config():
if not conf.exists(base):
return None
- ssh = conf.get_config_dict(base, key_mangling=('-', '_'))
+ ssh = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True)
# We have gathered the dict representation of the CLI, but there are default
# options which we need to update into the dictionary retrived.
default_values = defaults(base)
diff --git a/src/conf_mode/system_console.py b/src/conf_mode/system_console.py
index 034cbee63..6f83335f3 100755
--- a/src/conf_mode/system_console.py
+++ b/src/conf_mode/system_console.py
@@ -31,7 +31,7 @@ def get_config():
base = ['system', 'console']
# retrieve configuration at once
- console = conf.get_config_dict(base)
+ console = conf.get_config_dict(base, get_first_key=True)
# bail out early if no serial console is configured
if 'device' not in console.keys():
diff --git a/src/conf_mode/vrf.py b/src/conf_mode/vrf.py
index e8f523e36..d3327b3c7 100755
--- a/src/conf_mode/vrf.py
+++ b/src/conf_mode/vrf.py
@@ -52,7 +52,7 @@ def vrf_interfaces(c, match):
matched = []
old_level = c.get_level()
c.set_level(['interfaces'])
- section = c.get_config_dict([])
+ section = c.get_config_dict([], get_first_key=True)
for type in section:
interfaces = section[type]
for name in interfaces: