diff options
author | John Estabrook <jestabro@vyos.io> | 2021-08-12 11:09:58 -0500 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2021-08-12 11:09:58 -0500 |
commit | d19b35d668184fdac00a8ef56ff58c0748f22816 (patch) | |
tree | 1a5de49dcba9fe12911322809715e7db399e05a5 /scripts | |
parent | e75db1c0c23bb8615dea92316148871edd18749e (diff) | |
download | vyos-1x-d19b35d668184fdac00a8ef56ff58c0748f22816.tar.gz vyos-1x-d19b35d668184fdac00a8ef56ff58c0748f22816.zip |
T3574: add constraintGroup for combining validators with logical AND
(cherry picked from commit 591b8bcadd8b6bbd46c61484193d2bf7e16bd1ae
commit 31553283aaa929da63147082e85513e8d4dacf0e
commit 59a4aadfe419eca16e6288b37d6c51acd9789903)
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/build-command-templates | 76 |
1 files changed, 46 insertions, 30 deletions
diff --git a/scripts/build-command-templates b/scripts/build-command-templates index 452c420eb..d8abb0a13 100755 --- a/scripts/build-command-templates +++ b/scripts/build-command-templates @@ -86,6 +86,37 @@ def make_path(l): print(path) return path +def collect_validators(ve): + regexes = [] + regex_elements = ve.findall("regex") + if regex_elements is not None: + regexes = list(map(lambda e: e.text.strip().replace('\\','\\\\'), regex_elements)) + if "" in regexes: + print("Warning: empty regex, node will be accepting any value") + + validator_elements = ve.findall("validator") + validators = [] + if validator_elements is not None: + for v in validator_elements: + v_name = os.path.join(validator_dir, v.get("name")) + + # XXX: lxml returns None for empty arguments + v_argument = None + try: + v_argument = v.get("argument") + except: + pass + if v_argument is None: + v_argument = "" + + validators.append("{0} {1}".format(v_name, v_argument)) + + + regex_args = " ".join(map(lambda s: "--regex \\\'{0}\\\'".format(s), regexes)) + validator_args = " ".join(map(lambda s: "--exec \\\"{0}\\\"".format(s), validators)) + + return regex_args + " " + validator_args + def get_properties(p): props = {} @@ -108,7 +139,8 @@ def get_properties(p): except: props["val_help"] = [] - # Get the constraint statements + # Get the constraint and constraintGroup statements + error_msg = default_constraint_err_msg # Get the error message if it's there try: @@ -117,40 +149,24 @@ def get_properties(p): pass vce = p.find("constraint") - vc = [] + + distinct_validator_string = "" if vce is not None: # The old backend doesn't support multiple validators in OR mode # so we emulate it - regexes = [] - regex_elements = vce.findall("regex") - if regex_elements is not None: - regexes = list(map(lambda e: e.text.strip().replace('\\','\\\\'), regex_elements)) - if "" in regexes: - print("Warning: empty regex, node will be accepting any value") - - validator_elements = vce.findall("validator") - validators = [] - if validator_elements is not None: - for v in validator_elements: - v_name = os.path.join(validator_dir, v.get("name")) - - # XXX: lxml returns None for empty arguments - v_argument = None - try: - v_argument = v.get("argument") - except: - pass - if v_argument is None: - v_argument = "" - - validators.append("{0} {1}".format(v_name, v_argument)) - - - regex_args = " ".join(map(lambda s: "--regex \\\'{0}\\\'".format(s), regexes)) - validator_args = " ".join(map(lambda s: "--exec \\\"{0}\\\"".format(s), validators)) + distinct_validator_string = collect_validators(vce) + + vcge = p.findall("constraintGroup") + + group_validator_string = "" + if len(vcge): + for vcg in vcge: + group_validator_string = group_validator_string + " --grp " + collect_validators(vcg) + + if vce is not None or len(vcge): validator_script = '${vyos_libexec_dir}/validate-value' - validator_string = "exec \"{0} {1} {2} --value \\\'$VAR(@)\\\'\"; \"{3}\"".format(validator_script, regex_args, validator_args, error_msg) + validator_string = "exec \"{0} {1} {2} --value \\\'$VAR(@)\\\'\"; \"{3}\"".format(validator_script, distinct_validator_string, group_validator_string, error_msg) props["constraint"] = validator_string |