From 32f9ca379112e38d09bccc0fbde4fe8a8cfc6788 Mon Sep 17 00:00:00 2001 From: Wontek Hong Date: Mon, 7 Oct 2024 11:31:38 +0200 Subject: [PATCH] azure: addition of quick setup Change-Id: I559f6e812b9de8219ed4aff1eb13f653b0e81786 --- cmk/gui/quick_setup/config_setups/__init__.py | 2 + .../config_setups/azure/__init__.py | 4 + .../quick_setup/config_setups/azure/stages.py | 250 ++++++++++++++++++ 3 files changed, 256 insertions(+) create mode 100644 cmk/gui/quick_setup/config_setups/azure/__init__.py create mode 100644 cmk/gui/quick_setup/config_setups/azure/stages.py diff --git a/cmk/gui/quick_setup/config_setups/__init__.py b/cmk/gui/quick_setup/config_setups/__init__.py index 3ed95f18d48..94227a4afc0 100644 --- a/cmk/gui/quick_setup/config_setups/__init__.py +++ b/cmk/gui/quick_setup/config_setups/__init__.py @@ -5,7 +5,9 @@ from ..v0_unstable._registry import QuickSetupRegistry from .aws.stages import quick_setup_aws +from .azure.stages import quick_setup_azure def register(registry: QuickSetupRegistry) -> None: registry.register(quick_setup_aws) + registry.register(quick_setup_azure) diff --git a/cmk/gui/quick_setup/config_setups/azure/__init__.py b/cmk/gui/quick_setup/config_setups/azure/__init__.py new file mode 100644 index 00000000000..c42b91132bf --- /dev/null +++ b/cmk/gui/quick_setup/config_setups/azure/__init__.py @@ -0,0 +1,4 @@ +#!/usr/bin/env python3 +# Copyright (C) 2024 Checkmk GmbH - License: GNU General Public License v2 +# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and +# conditions defined in the file COPYING, which is part of this source code package. diff --git a/cmk/gui/quick_setup/config_setups/azure/stages.py b/cmk/gui/quick_setup/config_setups/azure/stages.py new file mode 100644 index 00000000000..f945f011cae --- /dev/null +++ b/cmk/gui/quick_setup/config_setups/azure/stages.py @@ -0,0 +1,250 @@ +#!/usr/bin/env python3 +# Copyright (C) 2024 Checkmk GmbH - License: GNU General Public License v2 +# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and +# conditions defined in the file COPYING, which is part of this source code package. + +from collections.abc import Mapping, Sequence + +from cmk.ccc.i18n import _ + +from cmk.utils.rulesets.definition import RuleGroup + +from cmk.gui.form_specs.private.dictionary_extended import DictionaryExtended +from cmk.gui.form_specs.vue.shared_type_defs import DictionaryLayout +from cmk.gui.quick_setup.v0_unstable.definitions import QSSiteSelection +from cmk.gui.quick_setup.v0_unstable.predefined import ( + collect_params_from_form_data, + complete, + recaps, + widgets, +) +from cmk.gui.quick_setup.v0_unstable.predefined import validators as qs_validators +from cmk.gui.quick_setup.v0_unstable.setups import ( + QuickSetup, + QuickSetupSaveAction, + QuickSetupStage, +) +from cmk.gui.quick_setup.v0_unstable.type_defs import ( + ParsedFormData, + QuickSetupId, + ServiceInterest, +) +from cmk.gui.quick_setup.v0_unstable.widgets import ( + Collapsible, + FormSpecId, + FormSpecWrapper, + ListOfWidgets, + Text, + Widget, +) +from cmk.gui.user_sites import get_configured_site_choices, site_attribute_default_value + +from cmk.plugins.azure.rulesets import ( # pylint: disable=cmk-module-layer-violation + azure, +) +from cmk.rulesets.v1 import Title +from cmk.rulesets.v1.form_specs import ( + DefaultValue, + DictElement, + Dictionary, + InputHint, + SingleChoice, + SingleChoiceElement, +) + + +def configure_authentication() -> QuickSetupStage: + return QuickSetupStage( + title=_("Prepare Azure for Checkmk"), + configure_components=[ + ListOfWidgets( + items=[ + Text( + text=_( + "Create an Azure app for Checkmk: Register the app in Azure Active Directory and note down the Application ID." + ) + ), + Text( + text=_( + 'Assign permissions to the app: Grant necessary access rights, assigning the "Reader" role.' + ), + ), + Text( + text=_( + "Generate a key for the app: Create a Secret key in the app settings and note it down." + ) + ), + Text( + text=_( + "Retrieve required information: Gather Subscription ID, Tenant ID, Client ID, and the Client secret from Azure." + ) + ), + Text( + text=_( + "Return to Checkmk: Define a unique Azure account name, and use the Subscription ID, Tenant ID, Client ID, and the Client secret below." + ) + ), + ], + list_type="ordered", + ), + widgets.unique_id_formspec_wrapper( + title=Title("Configuration name"), prefill_template="azure_config" + ), + FormSpecWrapper( + id=FormSpecId("credentials"), + form_spec=DictionaryExtended( + elements=azure.configuration_authentication(), + layout=DictionaryLayout.two_columns, + ), + ), + ], + custom_validators=[qs_validators.validate_unique_id], + recap=[recaps.recaps_form_spec], + button_label="Configure host and authority", + ) + + +def configure_host_and_authority() -> QuickSetupStage: + site_default_value = site_attribute_default_value() + return QuickSetupStage( + title=_("Configure host and authority"), + sub_title=_( + "Name your host, define the path and select the authority you would like to monitor" + ), + configure_components=[ + widgets.host_name_and_host_path_formspec_wrapper(host_prefill_template="azure"), + FormSpecWrapper( + id=FormSpecId("configure_authority"), + form_spec=DictionaryExtended( + elements=azure.configuration_authority(), + layout=DictionaryLayout.two_columns, + ), + ), + FormSpecWrapper( + id=FormSpecId("site"), + form_spec=DictionaryExtended( + elements={ + QSSiteSelection: DictElement( + parameter_form=SingleChoice( + elements=[ + SingleChoiceElement( + name=site_id, + title=Title( # pylint: disable=localization-of-non-literal-string + title + ), + ) + for site_id, title in get_configured_site_choices() + ], + title=Title("Site selection"), + prefill=( + DefaultValue(site_default_value) + if site_default_value + else InputHint(Title("Please choose")) + ), + ), + required=True, + ) + }, + layout=DictionaryLayout.two_columns, + ), + ), + ], + custom_validators=[qs_validators.validate_host_name_doesnt_exists], + recap=[recaps.recaps_form_spec], + button_label="Configure services to monitor", + ) + + +def _configure() -> Sequence[Widget]: + return [ + FormSpecWrapper( + id=FormSpecId("configure_services_to_monitor"), + form_spec=DictionaryExtended( + elements=azure.configuration_services(), + layout=DictionaryLayout.two_columns, + ), + ), + Collapsible( + title="Other options", + items=[ + FormSpecWrapper( + id=FormSpecId("configure_advanced"), + form_spec=DictionaryExtended( + elements=azure.configuration_advanced(), + layout=DictionaryLayout.two_columns, + ), + ), + ], + ), + ] + + +def configure_services_to_monitor() -> QuickSetupStage: + return QuickSetupStage( + title=_("Configure services to monitor"), + sub_title=_("Select and configure the Microsoft Azure services you would like to monitor"), + configure_components=_configure, + custom_validators=[], + recap=[ + recaps.recaps_form_spec, + ], + button_label="Review & run preview service discovery", + ) + + +def review_and_run_preview_service_discovery() -> QuickSetupStage: + return QuickSetupStage( + title=_("Review and run preview service discovery"), + sub_title=_("Review your configuration and run preview service discovery"), + configure_components=[], + custom_validators=[ + qs_validators.validate_test_connection_custom_collect_params( + rulespec_name=RuleGroup.SpecialAgents("azure"), + parameter_form=azure.formspec(), + custom_collect_params=azure_collect_params, + ) + ], + recap=[ + recaps.recap_service_discovery_custom_collect_params( + rulespec_name=RuleGroup.SpecialAgents("azure"), + parameter_form=azure.formspec(), + services_of_interest=[ServiceInterest(".*", "services")], + custom_collect_params=azure_collect_params, + ) + ], + button_label="Run preview service discovery", + ) + + +def save_action(all_stages_form_data: ParsedFormData) -> str: + return complete.create_and_save_special_agent_bundle_custom_collect_params( + special_agent_name="azure", + parameter_form=azure.formspec(), + all_stages_form_data=all_stages_form_data, + custom_collect_params=azure_collect_params, + ) + + +def azure_collect_params( + all_stages_form_data: ParsedFormData, parameter_form: Dictionary +) -> Mapping[str, object]: + return collect_params_from_form_data(all_stages_form_data, parameter_form) + + +quick_setup_azure = QuickSetup( + title=_("Microsoft Azure"), + id=QuickSetupId(RuleGroup.SpecialAgents("azure")), + stages=[ + configure_authentication, + configure_host_and_authority, + configure_services_to_monitor, + review_and_run_preview_service_discovery, + ], + save_actions=[ + QuickSetupSaveAction( + id="activate_changes", + label=_("Save & go to Activate changes"), + action=save_action, + ), + ], +)