diff options
| author | Christian Breunig <christian@breunig.cc> | 2026-03-06 13:10:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-06 13:10:53 +0100 |
| commit | 20e005fc88c2a5182b0b711245272724a0e9d853 (patch) | |
| tree | 394f4259dfe6e30d48820ee40f84644243ea2a72 /python | |
| parent | f1584a8a53f0f9df5957d5540aec433561c579c2 (diff) | |
| parent | 046a4284d839e8d89e62a0afe6f838f3e2bef764 (diff) | |
| download | vyos-1x-20e005fc88c2a5182b0b711245272724a0e9d853.tar.gz vyos-1x-20e005fc88c2a5182b0b711245272724a0e9d853.zip | |
Merge pull request #5030 from c-po/op-mode-cli-verify
opmode: T8343: implement common verify_cli_exists() helper
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/opmode.py | 28 |
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 |
