diff options
author | Daniil Baturin <daniil@vyos.io> | 2023-12-24 17:21:47 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-24 17:21:47 +0000 |
commit | b1a35b8ae02c7a72ee29bf3e1595fedf254479ee (patch) | |
tree | 5ae96e50f01d84d78a2bf5af4e9bc72f5051de82 /python/vyos | |
parent | 50ec7b3cd28d1dec1592a141f4eaf2e5c7082ba3 (diff) | |
parent | bfdc261db153ba2813b93ffe1564afd37d0a0d7c (diff) | |
download | vyos-1x-b1a35b8ae02c7a72ee29bf3e1595fedf254479ee.tar.gz vyos-1x-b1a35b8ae02c7a72ee29bf3e1595fedf254479ee.zip |
Merge pull request #2685 from vyos/mergify/bp/sagitta/pr-2682
configdict: T5837: add support to return added nodes when calling node_changed() (backport #2682)
Diffstat (limited to 'python/vyos')
-rw-r--r-- | python/vyos/configdict.py | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/python/vyos/configdict.py b/python/vyos/configdict.py index 075ffe466..6a421485f 100644 --- a/python/vyos/configdict.py +++ b/python/vyos/configdict.py @@ -104,6 +104,10 @@ def list_diff(first, second): return [item for item in first if item not in second] def is_node_changed(conf, path): + """ + Check if any key under path has been changed and return True. + If nothing changed, return false + """ from vyos.configdiff import get_config_diff D = get_config_diff(conf, key_mangling=('-', '_')) return D.is_node_changed(path) @@ -139,17 +143,27 @@ def leaf_node_changed(conf, path): return None -def node_changed(conf, path, key_mangling=None, recursive=False): +def node_changed(conf, path, key_mangling=None, recursive=False, expand_nodes=None) -> list: """ - Check if a leaf node was altered. If it has been altered - values has been - changed, or it was added/removed, we will return the old value. If nothing - has been changed, None is returned + Check if node under path (or anything under path if recursive=True) was changed. By default + we only check if a node or subnode (recursive) was deleted from path. If expand_nodes + is set to Diff.ADD we can also check if something was added to the path. + + If nothing changed, an empty list is returned. """ - from vyos.configdiff import get_config_diff, Diff + from vyos.configdiff import get_config_diff + from vyos.configdiff import Diff + # to prevent circular dependencies we assign the default here + if not expand_nodes: expand_nodes = Diff.DELETE D = get_config_diff(conf, key_mangling) - # get_child_nodes() will return dict_keys(), mangle this into a list with PEP448 - keys = D.get_child_nodes_diff(path, expand_nodes=Diff.DELETE, recursive=recursive)['delete'].keys() - return list(keys) + # get_child_nodes_diff() will return dict_keys() + tmp = D.get_child_nodes_diff(path, expand_nodes=expand_nodes, recursive=recursive) + output = [] + if expand_nodes & Diff.DELETE: + output.extend(list(tmp['delete'].keys())) + if expand_nodes & Diff.ADD: + output.extend(list(tmp['add'].keys())) + return output def get_removed_vlans(conf, path, dict): """ |