def delete_host_tag_group(params): """Delete a host tag group""" ident = params['name'] if is_builtin(ident): return problem( status=405, title="Built-in cannot be delete", detail=f"The built-in host tag group {ident} cannot be deleted", ) affected = change_host_tags_in_folders(OperationRemoveTagGroup(ident), TagCleanupMode.CHECK, watolib.Folder.root_folder()) if any(affected): if not params["repair"]: return problem( 401, f'Deleting this host tag group "{ident}" requires additional authorization', 'The host tag group you intend to delete is used by other instances. You must authorize Checkmk ' 'to update the relevant instances using the repair parameter') watolib.host_attributes.undeclare_host_tag_attribute(ident) _ = change_host_tags_in_folders(OperationRemoveTagGroup(ident), TagCleanupMode("delete"), watolib.Folder.root_folder()) tag_config = load_tag_config() tag_config.remove_tag_group(ident) update_tag_config(tag_config) return Response(status=204)
def _show_tag_row(self, table: Table, tag_group: cmk.utils.tags.TagGroup, tag: cmk.utils.tags.GroupedTag) -> None: table.row() table.cell(_("Actions"), css="buttons") self._show_tag_group_icons(tag_group) table.cell(_("Tag group"), _u(tag_group.choice_title)) # TODO: This check shouldn't be necessary if we get our types right. if tag.title is None or tag_group.id is None: raise Exception("uninitialized tag/tag group") table.cell(_("Tag"), _u(tag.title)) operation = OperationReplaceGroupedTags(tag_group.id, remove_tag_ids=[tag.id], replace_tag_ids={}) affected_folders, affected_hosts, affected_rulesets = \ change_host_tags_in_folders(operation, TagCleanupMode.CHECK, watolib.Folder.root_folder()) table.cell(_("Explicitly set on folders")) if affected_folders: _show_affected_folders(affected_folders) table.cell(_("Explicitly set on hosts")) if affected_hosts: _show_affected_hosts(affected_hosts) table.cell(_("Used in rulesets")) if affected_rulesets: _show_affected_rulesets(affected_rulesets)
def _show_aux_tag_row(self, table: Table, aux_tag: cmk.utils.tags.AuxTag) -> None: table.row() table.cell(_("Actions"), css="buttons") self._show_aux_tag_icons(aux_tag) table.cell(_("Tag"), _u(aux_tag.choice_title)) table.cell(_("Used by tags")) _show_aux_tag_used_by_tags(self._get_tags_using_aux_tag(aux_tag)) # TODO: This check shouldn't be necessary if we get our types right. if aux_tag.id is None: raise Exception("uninitialized tag") operation = OperationRemoveAuxTag(aux_tag.id) affected_folders, affected_hosts, affected_rulesets = \ change_host_tags_in_folders(operation, TagCleanupMode.CHECK, watolib.Folder.root_folder()) table.cell(_("Explicitly set on folders")) if affected_folders: _show_affected_folders(affected_folders) table.cell(_("Explicitly set on hosts")) if affected_hosts: _show_affected_hosts(affected_hosts) table.cell(_("Used in rulesets")) if affected_rulesets: _show_affected_rulesets(affected_rulesets)
def delete_host_tag_group(params): """Delete a host tag group""" ident = params['name'] allow_repair = params['body'].get("repair", False) affected = change_host_tags_in_folders(OperationRemoveTagGroup(ident), TagCleanupMode.CHECK, watolib.Folder.root_folder()) if any(affected): if not allow_repair: return problem( 401, f'Deleting this host tag group "{ident}" requires additional authorization', 'The host tag group you intend to delete is used by other instances. You must authorize Checkmk ' 'to update the relevant instances using the repair parameter') watolib.host_attributes.undeclare_host_tag_attribute(ident) _ = change_host_tags_in_folders(OperationRemoveTagGroup(ident), TagCleanupMode("delete"), watolib.Folder.root_folder()) tag_config = load_tag_config() tag_config.remove_tag_group(ident) update_tag_config(tag_config) return Response(status=204)
def _rename_tags_after_confirmation(breadcrumb: Breadcrumb, operation: ABCOperation) -> Union[bool, str]: """Handle renaming and deletion of tags Find affected hosts, folders and rules. Remove or fix those rules according the users' wishes. Returns: True: Proceed, no "question" dialog shown False: "Question dialog" shown str: Action done after "question" dialog """ repair_mode = request.var("_repair") if repair_mode is not None: try: mode = TagCleanupMode(repair_mode) except ValueError: raise MKUserError("_repair", "Invalid mode") if mode == TagCleanupMode.ABORT: raise MKUserError("id_0", _("Aborting change.")) # make attribute unknown to system, important for save() operations if isinstance(operation, OperationRemoveTagGroup): watolib.host_attributes.undeclare_host_tag_attribute(operation.tag_group_id) affected_folders, affected_hosts, affected_rulesets = \ change_host_tags_in_folders(operation, mode, watolib.Folder.root_folder()) return _("Modified folders: %d, modified hosts: %d, modified rulesets: %d") % \ (len(affected_folders), len(affected_hosts), len(affected_rulesets)) message = HTML() affected_folders, affected_hosts, affected_rulesets = \ change_host_tags_in_folders(operation, TagCleanupMode.CHECK, watolib.Folder.root_folder()) if affected_folders: with output_funnel.plugged(): html.write_text( _("Affected folders with an explicit reference to this tag " "group and that are affected by the change") + ":") _show_affected_folders(affected_folders) message += HTML(output_funnel.drain()) if affected_hosts: with output_funnel.plugged(): html.write_text( _("Hosts where this tag group is explicitely set " "and that are effected by the change") + ":") _show_affected_hosts(affected_hosts) message += HTML(output_funnel.drain()) if affected_rulesets: with output_funnel.plugged(): html.write_text( _("Rulesets that contain rules with references to the changed tags") + ":") _show_affected_rulesets(affected_rulesets) message += HTML(output_funnel.drain()) if message: wato_html_head(title=operation.confirm_title(), breadcrumb=breadcrumb) html.open_div(class_="really") html.h3(_("Your modifications affect some objects")) html.write_text(message) html.br() html.write_text( _("Setup can repair things for you. It can rename tags in folders, host and rules. " "Removed tag groups will be removed from hosts and folders, removed tags will be " "replaced with the default value for the tag group (for hosts and folders). What " "rules concern, you have to decide how to proceed.")) html.begin_form("confirm", method="POST") if affected_rulesets and _is_removing_tags(operation): html.br() html.b( _("Some tags that are used in rules have been removed by you. What " "shall we do with that rules?")) html.open_ul() html.radiobutton("_repair", "remove", True, _("Just remove the affected tags from the rules.")) html.br() html.radiobutton( "_repair", "delete", False, _("Delete rules containing tags that have been removed, if tag is used in a positive sense. Just remove that tag if it's used negated." )) else: html.open_ul() html.radiobutton("_repair", "repair", True, _("Fix affected folders, hosts and rules.")) html.br() html.radiobutton("_repair", "abort", False, _("Abort your modifications.")) html.close_ul() html.button("_do_confirm", _("Proceed"), "") html.hidden_fields(add_action_vars=True) html.end_form() html.close_div() return False return True