예제 #1
0
파일: tags.py 프로젝트: petrows/checkmk
    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)
예제 #2
0
파일: tags.py 프로젝트: LinuxHaus/checkmk
    def action(self) -> ActionResult:
        if not transactions.check_transaction():
            return redirect(mode_url("tags"))

        vs = self._valuespec()
        tag_group_spec = vs.from_html_vars("tag_group")
        vs.validate_value(tag_group_spec, "tag_group")

        # Create new object with existing host tags
        changed_hosttags_config = cmk.utils.tags.TagConfig.from_config(
            self._tag_config_file.load_for_modification())
        changed_tag_group = cmk.utils.tags.TagGroup.from_config(tag_group_spec)
        self._tag_group = changed_tag_group

        if self._new:
            # Inserts and verifies changed tag group
            changed_hosttags_config.insert_tag_group(changed_tag_group)
            try:
                changed_hosttags_config.validate_config()
            except MKGeneralException as e:
                raise MKUserError(None, "%s" % e)
            self._save_tags_and_update_hosts(
                changed_hosttags_config.get_dict_format())
            _changes.add_change(
                "edit-hosttags",
                _("Created new host tag group '%s'") % changed_tag_group.id)
            flash(
                _("Created new host tag group '%s'") % changed_tag_group.title)
            return redirect(mode_url("tags"))

        # Updates and verifies changed tag group
        changed_hosttags_config.update_tag_group(changed_tag_group)
        try:
            changed_hosttags_config.validate_config()
        except MKGeneralException as e:
            raise MKUserError(None, "%s" % e)

        remove_tag_ids, replace_tag_ids = identify_modified_tags(
            changed_tag_group, self._untainted_tag_group)
        tg_id = self._tag_group.id
        if tg_id is None:
            raise Exception("tag group ID not set")
        operation = OperationReplaceGroupedTags(tg_id, remove_tag_ids,
                                                replace_tag_ids)

        # Now check, if any folders, hosts or rules are affected
        message = _rename_tags_after_confirmation(self.breadcrumb(), operation)
        if message is False:
            return FinalizeRequest(code=200)

        self._save_tags_and_update_hosts(
            changed_hosttags_config.get_dict_format())
        _changes.add_change(
            "edit-hosttags",
            _("Edited host tag group %s (%s)") % (message, self._id))
        if isinstance(message, str):
            flash(message)

        return redirect(mode_url("tags"))