summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2026-03-05 21:51:26 +0100
committerChristian Breunig <christian@breunig.cc>2026-03-05 21:53:32 +0100
commit046a4284d839e8d89e62a0afe6f838f3e2bef764 (patch)
tree3bacb1f56ae3a69ee1ea29c5f971b86fc970d6fd /python
parent7c520f8ec786f6cff411188498822eab00e69afd (diff)
downloadvyos-1x-046a4284d839e8d89e62a0afe6f838f3e2bef764.tar.gz
vyos-1x-046a4284d839e8d89e62a0afe6f838f3e2bef764.zip
opmode: T8343: implement common verify_cli_exists() helper
Instead of re-defining a per module helper which verifies that a given CLI path exists during execution, rather create a generic representation which takes a CLI path (list) as argument with an optional error message to display on the CLI. This is the initial implementation of the generic helper with some migrations for VPP, WireGuard and Wireless interfaces. More opmode scripts should follow.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/opmode.py28
1 files changed, 27 insertions, 1 deletions
diff --git a/python/vyos/opmode.py b/python/vyos/opmode.py
index 7f1fc6b4f..421054f6e 100644
--- a/python/vyos/opmode.py
+++ b/python/vyos/opmode.py
@@ -16,8 +16,9 @@
import re
import sys
import typing
-from humps import decamelize
+from humps import decamelize
+from vyos.configquery import ConfigTreeQuery
class Error(Exception):
"""Any error that makes requested operation impossible to complete
@@ -327,3 +328,28 @@ def run(module):
# Other functions should not return anything,
# although they may print their own warnings or status messages
func(**args)
+
+def verify_cli_exists(cli_path: list, error_msg: str=''):
+ """Decorator checks if CLI config exists using a dynamic path and error message"""
+ def decorator(func):
+ from functools import wraps
+
+ @wraps(func)
+ def _wrapper(*args, **kwargs):
+ config = ConfigTreeQuery()
+ interface = kwargs.get('intf_name')
+
+ # Combine the base CLI path with the specific interface name
+ path = cli_path + [interface] if interface else cli_path
+
+ if not config.exists(path):
+ if not error_msg:
+ unconf_message = f'CLI path [{" ".join(cli_path)}] unconfigured!'
+ else:
+ # Format the error message dynamically to allow {interface} injection
+ unconf_message = error_msg.format(interface=interface)
+ raise UnconfiguredSubsystem(unconf_message)
+ return func(*args, **kwargs)
+
+ return _wrapper
+ return decorator