summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2023-12-24 13:13:44 +0100
committerChristian Breunig <christian@breunig.cc>2023-12-24 13:13:44 +0100
commit4ee4064705ebd1e1a6a59be0c6df3b96755a067e (patch)
tree4cfde7b83abc2346329da415f2e6f16a0d7eb3f2 /python
parent5e7a8288d06a6d6beee5e1abd2e06698ab778650 (diff)
downloadvyos-1x-4ee4064705ebd1e1a6a59be0c6df3b96755a067e.tar.gz
vyos-1x-4ee4064705ebd1e1a6a59be0c6df3b96755a067e.zip
configdict: T5837: add support to return added nodes when calling node_changed()
In the past, node_changed() suggested it would also return nodes that got added (function comment) but in reality only deleted keys got accounted for. This commit changes the signature and adds an argument expand_nodes to specify the users interest of a node was deleted (default), added (expand_nodes=Diff.ADD) or even both (expand_nodes=Diff.ADD|Diff.DELETE).
Diffstat (limited to 'python')
-rw-r--r--python/vyos/configdict.py26
1 files changed, 18 insertions, 8 deletions
diff --git a/python/vyos/configdict.py b/python/vyos/configdict.py
index ed8137c0a..6a421485f 100644
--- a/python/vyos/configdict.py
+++ b/python/vyos/configdict.py
@@ -143,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):
"""