summaryrefslogtreecommitdiff
path: root/python/vyos/config.py
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2019-10-31 02:37:54 +0100
committerChristian Poessinger <christian@poessinger.com>2019-10-31 02:37:54 +0100
commitcd6fc1182e649a72e842567d9aaa6dd39f304e2a (patch)
tree02909b62bdc5945c90c402d3def901591c5b5314 /python/vyos/config.py
parent9805067e9d80ec1676cf2c2039be3136401c9fa3 (diff)
parentc5aefce07b1f505a1796c440801459112ed734d1 (diff)
downloadvyos-1x-cd6fc1182e649a72e842567d9aaa6dd39f304e2a.tar.gz
vyos-1x-cd6fc1182e649a72e842567d9aaa6dd39f304e2a.zip
Merge branch 'current' of github.com:vyos/vyos-1x into equuleus
* 'current' of github.com:vyos/vyos-1x: [conf completion]: T1779: Add tunnels to completion [XML templates] T1772: Changed old hacks to proper regex, according to the fix Add a function for retrieving config dicts. snmp: make script extension code more readable snmp: use proper stat literals on chmod() snmp: fix verify() indent on script extensions snmp: fix verify() bail out early order snmp: T1738: cleanup import statements T1759: Fixing dependency bug from previous commit T1773, T1774: add a show config operation with JSON and raw options. T1759: Merging interface.py into ifconfig.py Allow list arguments in the vyos.config show_config() function. Replace the try and wait for segfault approach with explicit inSession check. T1773: add a script for converting the config to JSON. It also exposes those functions in vyos.configtree [XML templates] T1772: Add escaping of `\` symbol in `<regex>`
Diffstat (limited to 'python/vyos/config.py')
-rw-r--r--python/vyos/config.py25
1 files changed, 18 insertions, 7 deletions
diff --git a/python/vyos/config.py b/python/vyos/config.py
index 7f65a9397..5bd8fb072 100644
--- a/python/vyos/config.py
+++ b/python/vyos/config.py
@@ -65,6 +65,7 @@ In operational mode, all functions return values from the running config.
import os
import re
+import json
import subprocess
import vyos.configtree
@@ -104,12 +105,10 @@ class Config(object):
running_config_text = f.read()
# Session config ("active") only exists in conf mode.
- # Trying to obtain it from op mode will cause a fatal cli-shell-api error.
- # If that happens, we assume that a script is running from op mode and use the running config
- # for the "session config" variable as well.
- try:
+ # In op mode, we'll just use the same running config for both active and session configs.
+ if self.in_session():
session_config_text = self._run([self._cli_shell_api, '--show-working-only', '--show-show-defaults', 'showConfig'])
- except VyOSError:
+ else:
session_config_text = running_config_text
self._session_config = vyos.configtree.ConfigTree(session_config_text)
@@ -224,21 +223,33 @@ class Config(object):
except VyOSError:
return False
- def show_config(self, path='', default=None):
+ def show_config(self, path=[], default=None):
"""
Args:
- path (str): Configuration tree path, or empty
+ path (str list): Configuration tree path, or empty
default (str): Default value to return
Returns:
str: working configuration
"""
+ if isinstance(path, list):
+ path = " ".join(path)
try:
out = self._run(self._make_command('showConfig', path))
return out
except VyOSError:
return(default)
+ def get_config_dict(self, path=[], effective=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))
+ config_tree = vyos.configtree.ConfigTree(res)
+ config_dict = json.loads(config_tree.to_json())
+ return config_dict
+
def is_multi(self, path):
"""
Args: