diff options
author | Christian Breunig <christian@breunig.cc> | 2023-06-12 21:15:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-12 21:15:39 +0200 |
commit | dd1cdc4ec18ef000f39228f773e1ceafc9fce766 (patch) | |
tree | f391f80db247106bc29504f7afe008f9af46433e /python | |
parent | 4816264e3a6b824da2d0b96aea60f958c46947a2 (diff) | |
parent | f8670aadaa2de60972b55a9784a5dfb6c75193d1 (diff) | |
download | vyos-1x-dd1cdc4ec18ef000f39228f773e1ceafc9fce766.tar.gz vyos-1x-dd1cdc4ec18ef000f39228f773e1ceafc9fce766.zip |
Merge pull request #2037 from jestabro/api-config-section
http-api: T5248: set/load config sections as JSON via API
Diffstat (limited to 'python')
-rw-r--r-- | python/vyos/configsession.py | 18 | ||||
-rw-r--r-- | python/vyos/utils/dict.py | 21 |
2 files changed, 38 insertions, 1 deletions
diff --git a/python/vyos/configsession.py b/python/vyos/configsession.py index df44fd8d6..88eedb944 100644 --- a/python/vyos/configsession.py +++ b/python/vyos/configsession.py @@ -1,5 +1,5 @@ # configsession -- the write API for the VyOS running config -# Copyright (C) 2019 VyOS maintainers and contributors +# Copyright (C) 2019-2023 VyOS maintainers and contributors # # This library is free software; you can redistribute it and/or modify it under the terms of # the GNU Lesser General Public License as published by the Free Software Foundation; @@ -18,6 +18,7 @@ import sys import subprocess from vyos.util import is_systemd_service_running +from vyos.utils.dict import dict_to_paths CLI_SHELL_API = '/bin/cli-shell-api' SET = '/opt/vyatta/sbin/my_set' @@ -148,6 +149,13 @@ class ConfigSession(object): value = [value] self.__run_command([SET] + path + value) + def set_section(self, path: list, d: dict): + try: + for p in dict_to_paths(d): + self.set(path + p) + except (ValueError, ConfigSessionError) as e: + raise ConfigSessionError(e) + def delete(self, path, value=None): if not value: value = [] @@ -155,6 +163,14 @@ class ConfigSession(object): value = [value] self.__run_command([DELETE] + path + value) + def load_section(self, path: list, d: dict): + try: + self.delete(path) + for p in dict_to_paths(d): + self.set(path + p) + except (ValueError, ConfigSessionError) as e: + raise ConfigSessionError(e) + def comment(self, path, value=None): if not value: value = [""] diff --git a/python/vyos/utils/dict.py b/python/vyos/utils/dict.py index 7c93deef6..3faf5c596 100644 --- a/python/vyos/utils/dict.py +++ b/python/vyos/utils/dict.py @@ -234,6 +234,27 @@ def dict_to_list(d, save_key_to=None): return collect +def dict_to_paths(d: dict) -> list: + """ Generator to return list of paths from dict of list[str]|str + """ + def func(d, path): + if isinstance(d, dict): + if not d: + yield path + for k, v in d.items(): + for r in func(v, path + [k]): + yield r + elif isinstance(d, list): + for i in d: + for r in func(i, path): + yield r + elif isinstance(d, str): + yield path + [d] + else: + raise ValueError('object is not a dict of strings/list of strings') + for r in func(d, []): + yield r + def check_mutually_exclusive_options(d, keys, required=False): """ Checks if a dict has at most one or only one of mutually exclusive keys. |