Skip to content

Commit bb7b57e

Browse files
committed
add validators for custom-toolsets and config-file
1 parent fd8ee72 commit bb7b57e

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/aks-preview/azext_aks_preview/_params.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@
224224
validate_max_blocked_nodes,
225225
validate_resource_group_parameter,
226226
validate_location_resource_group_cluster_parameters,
227+
validate_agent_config_file,
228+
validate_agent_custom_toolsets,
227229
)
228230
from azext_aks_preview.azurecontainerstorage._consts import (
229231
CONST_ACSTOR_ALL,
@@ -2803,6 +2805,7 @@ def load_arguments(self, _):
28032805
c.argument(
28042806
"config_file",
28052807
default=os.path.join(get_config_dir(), "aksAgent.config"),
2808+
validator=validate_agent_config_file,
28062809
required=False,
28072810
help="Path to the config file.",
28082811
)
@@ -2841,6 +2844,7 @@ def load_arguments(self, _):
28412844
c.argument(
28422845
"custom-toolsets",
28432846
help="File path to a custom toolsets.",
2847+
validator=validate_agent_custom_toolsets,
28442848
required=False,
28452849
type=str,
28462850
)

src/aks-preview/azext_aks_preview/_validators.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import os.path
1010
import re
11+
import yaml
1112
from ipaddress import ip_network
1213
from math import isclose, isnan
1314

@@ -977,3 +978,42 @@ def validate_location_resource_group_cluster_parameters(namespace):
977978
raise MutuallyExclusiveArgumentError(
978979
"Cannot specify --location and --resource-group and --cluster at the same time."
979980
)
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

Comments
 (0)