Skip to content

Commit

Permalink
fix site selection crash on remote sites
Browse files Browse the repository at this point in the history
SingleChoice checks if the prefill value is in the elements.
On remote sites, the available sites are probably just themselves, which would lead to an error.
SingleChoiceExtended doesn't have this validation.

Change-Id: I53efa9872e9cd42b6abece5694c17288aff577f4
JIRA-Ref: CMK-20938
  • Loading branch information
ottermata committed Jan 13, 2025
1 parent 9963fba commit 983a927
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
22 changes: 9 additions & 13 deletions cmk/gui/form_specs/generators/setup_site_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@
from cmk.ccc.site import omd_site

from cmk.gui import site_config
from cmk.gui.form_specs.private import SingleChoiceElementExtended, SingleChoiceExtended
from cmk.gui.logged_in import user as global_user
from cmk.gui.site_config import configured_sites
from cmk.gui.user_sites import activation_sites, site_choices

from cmk.rulesets.v1 import Help, Label, Title
from cmk.rulesets.v1.form_specs import (
DefaultValue,
InvalidElementValidator,
SingleChoice,
SingleChoiceElement,
)
from cmk.rulesets.v1.form_specs import DefaultValue, InvalidElementValidator

T = TypeVar("T")

Expand All @@ -42,11 +38,11 @@ def _compute_default_prefill() -> DefaultValue[str]:
return DefaultValue(authorized_site_ids[0])


def _compute_site_choices() -> Sequence[SingleChoiceElement]:
def _compute_site_choices() -> Sequence[SingleChoiceElementExtended[str]]:
return [
SingleChoiceElement(
choice[0],
Title(choice[1]), # pylint: disable=localization-of-non-literal-string
SingleChoiceElementExtended(
name=choice[0],
title=Title(choice[1]), # pylint: disable=localization-of-non-literal-string
)
for choice in site_choices(activation_sites())
]
Expand All @@ -56,16 +52,16 @@ def create_setup_site_choice(
title: Title = Title("Site"),
help_text: Help | None = None,
label: Label | None = None,
elements: Sequence[SingleChoiceElement] | None = None,
elements: Sequence[SingleChoiceElementExtended[str]] | None = None,
prefill: DefaultValue[str] | None = None,
invalid_element_validation: InvalidElementValidator | None = None,
) -> SingleChoice:
) -> SingleChoiceExtended[str]:
if elements is None:
elements = _compute_site_choices()
if prefill is None:
prefill = _compute_default_prefill()

return SingleChoice(
return SingleChoiceExtended(
title=title,
help_text=help_text,
label=label,
Expand Down
3 changes: 1 addition & 2 deletions cmk/gui/watolib/builtin_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
InvalidElementValidator,
List,
MonitoredHost,
SingleChoice,
String,
)

Expand Down Expand Up @@ -1193,7 +1192,7 @@ def valuespec(self) -> ValueSpec:
),
)

def form_spec(self) -> SingleChoice:
def form_spec(self) -> SingleChoiceExtended[str]:
return create_setup_site_choice(
title=Title("Monitored on site"),
help_text=Help("Specify the site that should monitor this host."),
Expand Down

0 comments on commit 983a927

Please sign in to comment.