diff options
author | Christian Breunig <christian@breunig.cc> | 2023-05-10 21:13:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-10 21:13:08 +0200 |
commit | 6eaa327c2a3bca3924e72cf7c5736489bb76f784 (patch) | |
tree | 3dcd4adf7692682026b3290b36c96de91d46c580 /python | |
parent | 96d9bf0a88fc822b8edd78db293b7f17ae8766cf (diff) | |
parent | 8dc6b0aa8d7bbecfc5087a7406725812779eaaf6 (diff) | |
download | vyos-1x-6eaa327c2a3bca3924e72cf7c5736489bb76f784.tar.gz vyos-1x-6eaa327c2a3bca3924e72cf7c5736489bb76f784.zip |
Merge pull request #1987 from dmbaturin/T5251-vrrp-group-ping
T5215: add a built-in ping check for VRRP groups
Diffstat (limited to 'python')
-rw-r--r-- | python/vyos/utils/dict.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/python/vyos/utils/dict.py b/python/vyos/utils/dict.py index 66b40d92b..4afc9f54e 100644 --- a/python/vyos/utils/dict.py +++ b/python/vyos/utils/dict.py @@ -233,3 +233,24 @@ def dict_to_list(d, save_key_to=None): collect.append(item) return collect + +def check_mutually_exclusive_options(d, keys, required=False): + """ Checks if a dict has at most one or only one of + mutually exclusive keys. + """ + present_keys = [] + + for k in d: + if k in keys: + present_keys.append(k) + + # Un-mangle the keys to make them match CLI option syntax + from re import sub + orig_keys = list(map(lambda s: sub(r'_', '-', s), keys)) + orig_present_keys = list(map(lambda s: sub(r'_', '-', s), present_keys)) + + if len(present_keys) > 1: + raise ValueError(f"Options {orig_keys} are mutually-exclusive but more than one of them is present: {orig_present_keys}") + + if required and (len(present_keys) < 1): + raise ValueError(f"At least one of the following options is required: {orig_present_keys}") |