Skip to content

Commit

Permalink
ms_teams: Migrate to FormSpecs
Browse files Browse the repository at this point in the history
CMK-19286

Change-Id: Ibd7d156b4114b0731c712acbd4401bf32da61a88
  • Loading branch information
loocars committed Oct 29, 2024
1 parent c4510ca commit 221a1ae
Showing 1 changed file with 96 additions and 83 deletions.
179 changes: 96 additions & 83 deletions cmk/gui/wato/_notification_parameter/_ms_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# conditions defined in the file COPYING, which is part of this source code package.


from typing import cast

from cmk.utils.ms_teams_constants import (
ms_teams_tmpl_host_details,
ms_teams_tmpl_host_summary,
Expand All @@ -13,21 +15,32 @@
ms_teams_tmpl_svc_title,
)

from cmk.gui.i18n import _
from cmk.gui.valuespec import (
CascadingDropdown,
from cmk.gui.form_specs.private import SingleChoiceElementExtended, SingleChoiceExtended
from cmk.gui.form_specs.vue.visitors.recomposers.unknown_form_spec import recompose
from cmk.gui.valuespec import Dictionary as ValueSpecDictionary
from cmk.gui.watolib.password_store import passwordstore_choices_without_user

from cmk.rulesets.v1 import Help, Label, Message, Title
from cmk.rulesets.v1.form_specs import (
CascadingSingleChoice,
CascadingSingleChoiceElement,
DefaultValue,
DictElement,
Dictionary,
DropdownChoice,
FieldSize,
FixedValue,
HTTPUrl,
TextAreaUnicode,
TextInput,
migrate_to_proxy,
MultilineText,
Proxy,
String,
)
from cmk.gui.wato import HTTPProxyReference
from cmk.gui.watolib.password_store import passwordstore_choices
from cmk.rulesets.v1.form_specs.validators import LengthInRange

from ._base import NotificationParameter
from ._helpers import get_url_prefix_specs, local_site_url, notification_macro_help
from ._helpers import (
_get_url_prefix_setting,
notification_macro_help_fs,
)


class NotificationParameterMsTeams(NotificationParameter):
Expand All @@ -36,107 +49,107 @@ def ident(self) -> str:
return "msteams"

@property
def spec(self) -> Dictionary:
def spec(self) -> ValueSpecDictionary:
# TODO needed because of mixed Form Spec and old style setup
return cast(ValueSpecDictionary, recompose(self._form_spec()).valuespec)

def _form_spec(self):
return Dictionary(
title=_("Create notification with the following parameters"),
elements=[
(
"webhook_url",
CascadingDropdown(
title=_("Webhook URL"),
help=_(
title=Title("Create notification with the following parameters"),
elements={
"webhook_url": DictElement(
parameter_form=CascadingSingleChoice(
title=Title("Webhook URL"),
help_text=Help(
"Create a workflow 'Post to a channel when a "
"webhook request is received' for a channel in MS "
"Teams and use the generated webook URL.<br><br>"
"This URL can also be collected from the Password "
"Store from Checkmk."
),
choices=[
(
"webhook_url",
_("Webhook URL"),
HTTPUrl(size=80, allow_empty=False),
elements=[
CascadingSingleChoiceElement(
name="webhook_url",
title=Title("Webhook URL"),
parameter_form=String(custom_validate=[LengthInRange(min_value=1)]),
),
(
"store",
_("URL from password store"),
DropdownChoice(
sorted=True,
choices=passwordstore_choices,
CascadingSingleChoiceElement(
name="store",
title=Title("URL from password store"),
parameter_form=SingleChoiceExtended(
no_elements_text=Message(
"There are no elements defined for this selection yet."
),
elements=[
SingleChoiceElementExtended(
title=Title("%s") % title, name=ident
)
for ident, title in passwordstore_choices_without_user()
if ident is not None
],
type=str,
),
),
],
sorted=False,
),
),
("proxy_url", HTTPProxyReference()),
("url_prefix", get_url_prefix_specs(local_site_url)),
(
"host_title",
TextInput(
title=_("Title for host notifications"),
help=notification_macro_help(),
default_value=ms_teams_tmpl_host_title(),
size=64,
"proxy_url": DictElement(parameter_form=Proxy(migrate=migrate_to_proxy)),
"url_prefix": _get_url_prefix_setting(),
"host_title": DictElement(
parameter_form=String(
title=Title("Title for host notifications"),
help_text=notification_macro_help_fs(),
prefill=DefaultValue(ms_teams_tmpl_host_title()),
field_size=FieldSize.LARGE,
),
),
(
"service_title",
TextInput(
title=_("Title for service notifications"),
help=notification_macro_help(),
default_value=ms_teams_tmpl_svc_title(),
size=64,
"service_title": DictElement(
parameter_form=String(
title=Title("Title for service notifications"),
help_text=notification_macro_help_fs(),
prefill=DefaultValue(ms_teams_tmpl_svc_title()),
field_size=FieldSize.LARGE,
),
),
(
"host_summary",
TextInput(
title=_("Summary for host notifications"),
help=notification_macro_help(),
default_value=ms_teams_tmpl_host_summary(),
size=64,
"host_summary": DictElement(
parameter_form=String(
title=Title("Summary for host notifications"),
help_text=notification_macro_help_fs(),
prefill=DefaultValue(ms_teams_tmpl_host_summary()),
field_size=FieldSize.LARGE,
),
),
(
"service_summary",
TextInput(
title=_("Summary for service notifications"),
help=notification_macro_help(),
default_value=ms_teams_tmpl_svc_summary(),
size=64,
"service_summary": DictElement(
parameter_form=String(
title=Title("Summary for service notifications"),
help_text=notification_macro_help_fs(),
prefill=DefaultValue(ms_teams_tmpl_svc_summary()),
field_size=FieldSize.LARGE,
),
),
(
"host_details",
TextAreaUnicode(
title=_("Details for host notifications"),
help=notification_macro_help(),
rows=9,
cols=58,
"host_details": DictElement(
parameter_form=MultilineText(
title=Title("Details for host notifications"),
help_text=notification_macro_help_fs(),
monospaced=True,
default_value=ms_teams_tmpl_host_details(),
prefill=DefaultValue(ms_teams_tmpl_host_details()),
),
),
(
"service_details",
TextAreaUnicode(
title=_("Details for service notifications"),
help=notification_macro_help(),
rows=11,
cols=58,
"service_details": DictElement(
parameter_form=MultilineText(
title=Title("Details for service notifications"),
help_text=notification_macro_help_fs(),
monospaced=True,
default_value=ms_teams_tmpl_svc_details(),
prefill=DefaultValue(ms_teams_tmpl_svc_details()),
),
),
(
"affected_host_groups",
FixedValue(
"affected_host_groups": DictElement(
parameter_form=FixedValue(
value=True,
title=_("Show affected host groups"),
totext=_("Show affected host groups"),
help=_("Show affected host groups in the created message."),
title=Title("Show affected host groups"),
label=Label("Show affected host groups"),
help_text=Help("Show affected host groups in the created message."),
),
),
],
},
)

0 comments on commit 221a1ae

Please sign in to comment.