From 046a4284d839e8d89e62a0afe6f838f3e2bef764 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Thu, 5 Mar 2026 21:51:26 +0100 Subject: 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. --- python/vyos/opmode.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'python') 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 -- cgit v1.2.3