diff options
-rw-r--r-- | python/vyos/config.py | 12 | ||||
-rw-r--r-- | python/vyos/configtree.py | 14 | ||||
-rwxr-xr-x | src/services/vyos-http-api-server | 14 | ||||
-rwxr-xr-x | src/utils/vyos-config-to-json | 40 |
4 files changed, 72 insertions, 8 deletions
diff --git a/python/vyos/config.py b/python/vyos/config.py index 3a340b2da..5c2fc7285 100644 --- a/python/vyos/config.py +++ b/python/vyos/config.py @@ -104,12 +104,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,7 +222,7 @@ 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 @@ -233,6 +231,8 @@ class Config(object): Returns: str: working configuration """ + if isinstance(path, list): + path = " ".join(path) try: out = self._run(self._make_command('showConfig', path)) return out diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py index 8832a5a63..77cffe90b 100644 --- a/python/vyos/configtree.py +++ b/python/vyos/configtree.py @@ -105,6 +105,14 @@ class ConfigTree(object): self.__to_commands.argtypes = [c_void_p] self.__to_commands.restype = c_char_p + self.__to_json = self.__lib.to_json + self.__to_json.argtypes = [c_void_p] + self.__to_json.restype = c_char_p + + self.__to_json_ast = self.__lib.to_json_ast + self.__to_json_ast.argtypes = [c_void_p] + self.__to_json_ast.restype = c_char_p + self.__set_add_value = self.__lib.set_add_value self.__set_add_value.argtypes = [c_void_p, c_char_p, c_char_p] self.__set_add_value.restype = c_int @@ -184,6 +192,12 @@ class ConfigTree(object): def to_commands(self): return self.__to_commands(self.__config).decode() + def to_json(self): + return self.__to_json(self.__config).decode() + + def to_json_ast(self): + return self.__to_json_ast(self.__config).decode() + def set(self, path, value=None, replace=True): """Set new entry in VyOS configuration. path: configuration path e.g. 'system dns forwarding listen-address' diff --git a/src/services/vyos-http-api-server b/src/services/vyos-http-api-server index 04c44c2be..571ec1258 100755 --- a/src/services/vyos-http-api-server +++ b/src/services/vyos-http-api-server @@ -207,11 +207,21 @@ def get_value(): elif op == 'exists': res = config.exists(path) elif op == 'showConfig': - config_format = 'raw' + config_format = 'json' if 'configFormat' in command: config_format = command['configFormat'] - res = session.show_config(command['path'], format=config_format) + res = session.show_config(path=command['path']) + if config_format == 'json': + config_tree = vyos.configtree.ConfigTree(res) + res = json.loads(config_tree.to_json()) + elif config_format == 'json_ast': + config_tree = vyos.configtree.ConfigTree(res) + res = json.loads(config_tree.to_json_ast()) + elif config_format == 'raw': + pass + else: + return error(400, "\"{0}\" is not a valid config format") else: return error(400, "\"{0}\" is not a valid operation".format(op)) except VyOSError as e: diff --git a/src/utils/vyos-config-to-json b/src/utils/vyos-config-to-json new file mode 100755 index 000000000..e03fd6a59 --- /dev/null +++ b/src/utils/vyos-config-to-json @@ -0,0 +1,40 @@ +#!/usr/bin/python3 + +import sys +import json + +from signal import signal, SIGPIPE, SIG_DFL +from vyos.configtree import ConfigTree + +signal(SIGPIPE,SIG_DFL) + +config_string = None +if (len(sys.argv) == 1): + # If no argument given, act as a pipe + config_string = sys.stdin.read() +else: + file_name = sys.argv[1] + try: + with open(file_name, 'r') as f: + config_string = f.read() + except OSError as e: + print("Could not read config file {0}: {1}".format(file_name, e), file=sys.stderr) + +# This script is usually called with the output of "cli-shell-api showCfg", which does not +# escape backslashes. "ConfigTree()" expects escaped backslashes when parsing a config +# string (and also prints them itself). Therefore this script would fail. +# Manually escape backslashes here to handle backslashes in any configuration strings +# properly. The alternative would be to modify the output of "cli-shell-api showCfg", +# but that may be break other things who rely on that specific output. +config_string = config_string.replace("\\", "\\\\") + +try: + config = ConfigTree(config_string) + json_str = config.to_json() + # Pretty print + json_str = json.dumps(json.loads(json_str), indent=4, sort_keys=True) +except ValueError as e: + print("Could not parse the config file: {0}".format(e), file=sys.stderr) + sys.exit(1) + +print(json_str) |