|
8 | 8 | import os |
9 | 9 | import os.path |
10 | 10 | import re |
| 11 | +import yaml |
11 | 12 | from ipaddress import ip_network |
12 | 13 | from math import isclose, isnan |
13 | 14 |
|
@@ -977,3 +978,42 @@ def validate_location_resource_group_cluster_parameters(namespace): |
977 | 978 | raise MutuallyExclusiveArgumentError( |
978 | 979 | "Cannot specify --location and --resource-group and --cluster at the same time." |
979 | 980 | ) |
| 981 | + |
| 982 | +def _validate_param_yaml_file(yaml_path, param_name): |
| 983 | + if not yaml_path: |
| 984 | + return |
| 985 | + if not os.path.exists(yaml_path): |
| 986 | + raise InvalidArgumentValueError( |
| 987 | + f"--{param_name}={yaml_path}: file is not found." |
| 988 | + ) |
| 989 | + if not os.access(yaml_path, os.R_OK): |
| 990 | + raise InvalidArgumentValueError( |
| 991 | + f"--{param_name}={yaml_path}: file is not readable." |
| 992 | + ) |
| 993 | + try: |
| 994 | + with open(yaml_path, "r") as file: |
| 995 | + yaml.safe_load(file) |
| 996 | + except yaml.YAMLError as e: |
| 997 | + raise InvalidArgumentValueError( |
| 998 | + f"--{param_name}={yaml_path}: file is not a valid YAML file: {e}" |
| 999 | + ) |
| 1000 | + except Exception as e: |
| 1001 | + raise InvalidArgumentValueError( |
| 1002 | + f"--{param_name}={yaml_path}: An error occurred while reading the config file: {e}" |
| 1003 | + ) |
| 1004 | + |
| 1005 | + |
| 1006 | +def validate_agent_config_file(namespace): |
| 1007 | + config_file = namespace.config_file |
| 1008 | + if not config_file: |
| 1009 | + return |
| 1010 | + |
| 1011 | + _validate_param_yaml_file(config_file, "config-file") |
| 1012 | + |
| 1013 | + |
| 1014 | +def validate_agent_custom_toolsets(namespace): |
| 1015 | + custom_toolsets = namespace.custom_toolsets |
| 1016 | + if not custom_toolsets: |
| 1017 | + return |
| 1018 | + _validate_param_yaml_file(custom_toolsets, "custom-toolsets") |
| 1019 | + |
0 commit comments