Skip to content

Commit

Permalink
quick_setup: isolate stage formspec validation from custom validators
Browse files Browse the repository at this point in the history
Change-Id: Ibc4581a51eab649150eb33dbf1f5010b91c5dc8f
  • Loading branch information
anthonyh209 committed Jan 10, 2025
1 parent 1ca1d3f commit 6c571a7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
27 changes: 24 additions & 3 deletions cmk/gui/openapi/endpoints/quick_setup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@
NextStageStructure,
StageActionResult,
start_quick_setup_stage_job,
validate_and_recap_stage,
validate_stage_formspecs,
verify_custom_validators_and_recap_stage,
)
from cmk.gui.quick_setup.v0_unstable._registry import quick_setup_registry
from cmk.gui.quick_setup.v0_unstable.definitions import QuickSetupSaveRedirect
from cmk.gui.quick_setup.v0_unstable.predefined import build_formspec_map_from_stages
from cmk.gui.quick_setup.v0_unstable.setups import QuickSetupActionMode
from cmk.gui.quick_setup.v0_unstable.type_defs import ParsedFormData, StageIndex
from cmk.gui.quick_setup.v0_unstable.type_defs import ParsedFormData, RawFormData, StageIndex

from cmk import fields

Expand Down Expand Up @@ -239,6 +241,23 @@ def quicksetup_run_stage_action(params: Mapping[str, Any]) -> Response:

stage_index = StageIndex(len(body["stages"]) - 1)
stage_action = matching_stage_action(quick_setup.stages[stage_index](), stage_action_id)

built_stages = [stage() for stage in quick_setup.stages[: stage_index + 1]]
form_spec_map = build_formspec_map_from_stages(built_stages)
# Validate the stage formspec data; this is separate from the custom validators of the stage
# as the custom validators can potentially take a long time (and therefore be run in a
# background job)
errors = validate_stage_formspecs(
stage_index=stage_index,
stages_raw_formspecs=[RawFormData(stage["form_data"]) for stage in body["stages"]],
quick_setup_formspec_map=form_spec_map,
)
if errors.exist():
return _serve_action_result(
StageActionResult(validation_errors=errors),
status_code=400,
)

if stage_action.run_in_background:
background_job_id = start_quick_setup_stage_job(
quick_setup=quick_setup,
Expand All @@ -255,11 +274,13 @@ def quicksetup_run_stage_action(params: Mapping[str, Any]) -> Response:
response.location = urlparse(background_job_status_link["href"]).path
return response

result = validate_and_recap_stage(
result = verify_custom_validators_and_recap_stage(
quick_setup=quick_setup,
stage_index=stage_index,
stage_action_id=stage_action_id,
input_stages=body["stages"],
form_spec_map=form_spec_map,
built_stages=built_stages,
)
return _serve_action_result(
result, status_code=200 if result.validation_errors is None else 400
Expand Down
36 changes: 16 additions & 20 deletions cmk/gui/quick_setup/handlers/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,15 @@ def matching_stage_action(
raise InvalidStageException(f"Stage action '{stage_action_id}' not found")


def validate_stage(
def verify_stage_custom_validators(
quick_setup: QuickSetup,
stages_raw_formspecs: Sequence[RawFormData],
stage_index: StageIndex,
stage_action_id: ActionId,
stages: Sequence[QuickSetupStage],
quick_setup_formspec_map: FormspecMap,
) -> ValidationErrors | None:
"""Validate the form data of a Quick setup stage.
Notes:
* The validation process consists of three steps:
1. (Quick setup specific) Validate that all form spec keys are existing.
2. (Form spec specific) Validate the form data against the respective form spec.
3. (Quick setup specific) Validate against custom validators that are defined in the stage action.
"""Verify that the custom validators pass of a Quick setup stage.
Args:
quick_setup:
Expand All @@ -146,10 +140,7 @@ def validate_stage(
The form spec map of the quick setup across all stages. This map is based on the stages
definition
"""
errors = validate_stage_formspecs(stage_index, stages_raw_formspecs, quick_setup_formspec_map)
if errors.exist():
return errors

errors = ValidationErrors(stage_index=stage_index)
custom_validators = matching_stage_action(
stages[stage_index], stage_action_id
).custom_validators
Expand Down Expand Up @@ -227,23 +218,22 @@ def _file_path(work_dir: str) -> str:
)


def validate_and_recap_stage(
def verify_custom_validators_and_recap_stage(
quick_setup: QuickSetup,
stage_index: StageIndex,
stage_action_id: ActionId,
input_stages: Sequence[dict],
form_spec_map: FormspecMap,
built_stages: Sequence[QuickSetupStage],
) -> StageActionResult:
built_stages_up_to_index = [stage() for stage in quick_setup.stages[: stage_index + 1]]
form_spec_map = build_formspec_map_from_stages(built_stages_up_to_index)

response = StageActionResult()
if (
errors := validate_stage(
errors := verify_stage_custom_validators(
quick_setup=quick_setup,
stages_raw_formspecs=[RawFormData(stage["form_data"]) for stage in input_stages],
stage_index=stage_index,
stage_action_id=stage_action_id,
stages=built_stages_up_to_index,
stages=built_stages,
quick_setup_formspec_map=form_spec_map,
)
) is not None:
Expand All @@ -253,7 +243,7 @@ def validate_and_recap_stage(
response.stage_recap = recap_stage(
quick_setup_id=quick_setup.id,
stage_index=stage_index,
stages=built_stages_up_to_index,
stages=built_stages,
stage_action_id=stage_action_id,
stages_raw_formspecs=[RawFormData(stage["form_data"]) for stage in input_stages],
quick_setup_formspec_map=form_spec_map,
Expand Down Expand Up @@ -313,11 +303,17 @@ def _run_quick_setup_stage_action(self, job_interface: BackgroundProcessInterfac

register_config_setups(quick_setup_registry)
quick_setup = quick_setup_registry[self._quick_setup_id]
action_result = validate_and_recap_stage(
built_stages_up_to_index = [
stage() for stage in quick_setup.stages[: self._stage_index + 1]
]
form_spec_map = build_formspec_map_from_stages(built_stages_up_to_index)
action_result = verify_custom_validators_and_recap_stage(
quick_setup=quick_setup,
stage_index=self._stage_index,
stage_action_id=self._action_id,
input_stages=self._user_input_stages,
form_spec_map=form_spec_map,
built_stages=built_stages_up_to_index,
)

job_interface.send_progress_update(_("Saving the result..."))
Expand Down

0 comments on commit 6c571a7

Please sign in to comment.