Skip to content

Commit 0de5e42

Browse files
committed
quick setup: Fix error when fetching non-existent background jobs
Instead of returning a status 400 (Bad request) when a background job does not exist, return a status 404 (Not found) CMK-21809 Change-Id: Ib65ab31e76924129c0e92e233d810f09e2af3c99
1 parent 046c44c commit 0de5e42

6 files changed

Lines changed: 57 additions & 5 deletions

File tree

cmk/gui/openapi/endpoints/quick_setup/__init__.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@
5757
validate_stage_formspecs,
5858
verify_custom_validators_and_recap_stage,
5959
)
60-
from cmk.gui.quick_setup.handlers.utils import form_spec_parse, ValidationErrors
60+
from cmk.gui.quick_setup.handlers.utils import (
61+
form_spec_parse,
62+
MKJobNotFoundException,
63+
ValidationErrors,
64+
)
6165
from cmk.gui.quick_setup.v0_unstable._registry import quick_setup_registry
6266
from cmk.gui.quick_setup.v0_unstable.definitions import QuickSetupSaveRedirect
6367
from cmk.gui.quick_setup.v0_unstable.predefined import build_formspec_map_from_stages
@@ -409,8 +413,16 @@ def fetch_quick_setup_stage_action_result(params: Mapping[str, Any]) -> Response
409413
)
410414
)
411415
)
416+
412417
else:
413-
action_result = StageActionResult.load_from_job_result(job_id=action_background_job_id)
418+
try:
419+
action_result = StageActionResult.load_from_job_result(job_id=action_background_job_id)
420+
421+
except MKJobNotFoundException:
422+
return _serve_error(
423+
"Job not found", f"Background job '{action_background_job_id}' not found", 404
424+
)
425+
414426
return _serve_action_result(action_result)
415427

416428

@@ -556,7 +568,14 @@ def complete_quick_setup_action(params: Mapping[str, Any], mode: QuickSetupActio
556568
def fetch_quick_setup_action_result(params: Mapping[str, Any]) -> Response:
557569
"""Fetch the Quick action background job result"""
558570
action_background_job_id = params["job_id"]
559-
action_result = CompleteActionResult.load_from_job_result(job_id=action_background_job_id)
571+
try:
572+
action_result = CompleteActionResult.load_from_job_result(job_id=action_background_job_id)
573+
574+
except MKJobNotFoundException:
575+
return _serve_error(
576+
"Job not found", f"Background job '{action_background_job_id}' not found", 404
577+
)
578+
560579
return _serve_action_result(action_result)
561580

562581

cmk/gui/quick_setup/handlers/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
InfoLogger,
4040
JobBasedProgressLogger,
4141
LOAD_WAIT_LABEL,
42+
MKJobNotFoundException,
4243
NEXT_BUTTON_ARIA_LABEL,
4344
NEXT_BUTTON_LABEL,
4445
PREV_BUTTON_ARIA_LABEL,
@@ -263,7 +264,7 @@ class CompleteActionResult(BaseModel):
263264
def load_from_job_result(cls, job_id: str) -> "CompleteActionResult":
264265
work_dir = str(Path(BackgroundJobDefines.base_dir) / job_id)
265266
if not os.path.exists(work_dir):
266-
raise MKInternalError(None, _("Action result not found"))
267+
raise MKJobNotFoundException(None, _("Action result not found"))
267268
content = store.load_text_from_file(cls._file_path(work_dir))
268269
try:
269270
return cls.model_validate_json(content)

cmk/gui/quick_setup/handlers/stage.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
InfoLogger,
4545
JobBasedProgressLogger,
4646
LOAD_WAIT_LABEL,
47+
MKJobNotFoundException,
4748
NEXT_BUTTON_ARIA_LABEL,
4849
NEXT_BUTTON_LABEL,
4950
PREV_BUTTON_ARIA_LABEL,
@@ -229,7 +230,7 @@ class StageActionResult(BaseModel, frozen=False):
229230
def load_from_job_result(cls, job_id: str) -> "StageActionResult":
230231
work_dir = str(Path(BackgroundJobDefines.base_dir) / job_id)
231232
if not os.path.exists(work_dir):
232-
raise MKInternalError(None, _("Stage action result not found"))
233+
raise MKJobNotFoundException(None, _("Stage action result not found"))
233234
content = store.load_text_from_file(cls._file_path(work_dir))
234235
try:
235236
return cls.model_validate_json(content)

cmk/gui/quick_setup/handlers/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pydantic import BaseModel
1111

1212
from cmk.gui.background_job import BackgroundProcessInterface
13+
from cmk.gui.exceptions import MKInternalError
1314
from cmk.gui.form_specs.vue.form_spec_visitor import (
1415
serialize_data_for_frontend,
1516
transform_to_disk_model,
@@ -260,3 +261,7 @@ def validate_custom_validators(
260261
class BackgroundJobException(BaseModel):
261262
message: str
262263
traceback: str
264+
265+
266+
class MKJobNotFoundException(MKInternalError):
267+
pass

tests/testlib/unit/rest_api_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,6 +3043,20 @@ def edit_quick_setup(
30433043
expect_ok=expect_ok,
30443044
)
30453045

3046+
def get_stage_action_result(self, job_id: str, expect_ok: bool = True) -> Response:
3047+
return self.request(
3048+
"get",
3049+
url=f"/objects/quick_setup_stage_action_result/{job_id}",
3050+
expect_ok=expect_ok,
3051+
)
3052+
3053+
def get_action_result(self, job_id: str, expect_ok: bool = True) -> Response:
3054+
return self.request(
3055+
"get",
3056+
url=f"/objects/quick_setup_action_result/{job_id}",
3057+
expect_ok=expect_ok,
3058+
)
3059+
30463060

30473061
class ConfigurationEntityClient(RestApiClient):
30483062
domain: API_DOMAIN = "configuration_entity"

tests/unit/cmk/gui/openapi/test_openapi_quick_setup.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,3 +813,15 @@ def test_fail_validate_permission(clients: ClientRegistry) -> None:
813813
resp.json["detail"]
814814
== "Action with id 'action' requires 'impossible_permisson' permissions."
815815
)
816+
817+
818+
def test_stage_action_result_should_return_404(clients: ClientRegistry) -> None:
819+
resp = clients.QuickSetup.get_stage_action_result("foo", expect_ok=False)
820+
assert resp.status_code == 404
821+
assert resp.json["detail"] == "Background job 'foo' not found"
822+
823+
824+
def test_action_result_should_return_404(clients: ClientRegistry) -> None:
825+
resp = clients.QuickSetup.get_action_result("foo", expect_ok=False)
826+
assert resp.status_code == 404
827+
assert resp.json["detail"] == "Background job 'foo' not found"

0 commit comments

Comments
 (0)