Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base handler treats command inputs separately #143

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions src/aosm/azext_aosm/cli_handlers/onboarding_base_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from dataclasses import fields, is_dataclass
from pathlib import Path
from typing import Optional, Union

from azure.cli.core.azclierror import UnclassifiedUserFault, UserFault, InvalidArgumentValueError
from json.decoder import JSONDecodeError
from azure.cli.core.azclierror import InvalidArgumentValueError, UnclassifiedUserFault, UserFault
from jinja2 import StrictUndefined, Template
from knack.log import get_logger

Expand Down Expand Up @@ -47,12 +47,13 @@ def __init__(
self.skip = skip
# If input.jsonc file provided (therefore if build command run)
if config_file_path:
self._validate_input_file(config_file_path)
config_dict = self._read_input_config_from_file(config_file_path)
try:
self.config = self._get_input_config(config_dict)
except Exception as e:
raise UnclassifiedUserFault(f"The input file provided contains an incorrect input.\nPlease fix the problem parameter:\n{e}") from e
except TypeError as e:
raise InvalidArgumentValueError(
"The input file provided contains an incorrect input.\n"
f"Please fix the problem parameter:\n{e}") from e
# Validate config before getting processor list,
# in case error with input artifacts i.e helm package
self.config.validate()
Expand All @@ -61,8 +62,11 @@ def __init__(
elif all_deploy_params_file_path:
try:
self.config = self._get_params_config(all_deploy_params_file_path)
except Exception as e:
raise UnclassifiedUserFault(f"The all_deploy.parameters.json in the folder provided contains an incorrect input.\nPlease check if you have provided the correct folder for the definition/design type:\n{e}") from e
except TypeError as e:
raise InvalidArgumentValueError(
"The all_deploy.parameters.json in the folder "
"provided contains an incorrect input.\nPlease check if you have provided "
f"the correct folder for the definition/design type:\n{e}") from e
# If no config file provided (for generate-config)
else:
self.config = self._get_input_config()
Expand Down Expand Up @@ -175,11 +179,16 @@ def _read_input_config_from_file(input_json_path: Path) -> dict:

Returns config as dictionary.
"""
lines = input_json_path.read_text().splitlines()
lines = [line for line in lines if not line.strip().startswith("//")]
config_dict = json.loads("".join(lines))

return config_dict
try:
lines = input_json_path.read_text().splitlines()
lines = [line for line in lines if not line.strip().startswith("//")]
config_dict = json.loads("".join(lines))
return config_dict
except (UserFault, FileNotFoundError) as e:
raise UnclassifiedUserFault(f"Invalid config file provided.\nError: {e} ") from e
except JSONDecodeError as e:
raise UnclassifiedUserFault("Invalid JSON found in the config file provided.\n"
f"Error: {e} ") from e

@staticmethod
def _render_base_bicep_contents(template_path):
Expand Down Expand Up @@ -333,13 +342,4 @@ def _build_deploy_params_schema(schema_properties):
"properties": {},
}
schema_contents["properties"] = schema_properties
return schema_contents

def _validate_input_file(self, input_file: Path):
"""Validate the input file."""
if not input_file.suffix == ".jsonc":
raise UnclassifiedUserFault(
f"The input file {input_file} does not have a .jsonc extension."
)
if not input_file.exists():
raise UnclassifiedUserFault(f"The input file {input_file} does not exist.")
return schema_contents