Пример #1
0
def add_group(name: GroupName, group_type: GroupType,
              extra_info: GroupSpec) -> None:
    _check_modify_group_permissions(group_type)
    all_groups = load_group_information()
    groups = all_groups.get(group_type, {})

    # Check group name
    if len(name) == 0:
        raise MKUserError("name", _("Please specify a name of the new group."))
    if " " in name:
        raise MKUserError("name",
                          _("Sorry, spaces are not allowed in group names."))
    if not re.match(r"^[-a-z0-9A-Z_\.]*$", name):
        raise MKUserError(
            "name",
            _("Invalid group name. Only the characters a-z, A-Z, 0-9, _, . and - are allowed."
              ),
        )
    if name in groups:
        raise MKUserError("name",
                          _("Sorry, there is already a group with that name"))

    _set_group(all_groups, group_type, name, extra_info)
    _add_group_change(extra_info, "edit-%sgroups" % group_type,
                      _("Create new %s group %s") % (group_type, name))
Пример #2
0
def edit_group(name, group_type, extra_info):
    _check_modify_group_permissions(group_type)
    all_groups = load_group_information()
    groups = all_groups.get(group_type, {})

    if name not in groups:
        raise MKUserError("name", _("Unknown group: %s") % name)

    old_group_backup = copy.deepcopy(groups[name])

    _set_group(all_groups, group_type, name, extra_info)
    if cmk_version.is_managed_edition():
        old_customer = managed.get_customer_id(old_group_backup)
        new_customer = managed.get_customer_id(extra_info)
        if old_customer != new_customer:
            _add_group_change(
                old_group_backup, "edit-%sgroups" % group_type,
                _("Removed %sgroup %s from customer %s") %
                (group_type, name,
                 managed.get_customer_name_by_id(old_customer)))
            _add_group_change(
                extra_info, "edit-%sgroups" % group_type,
                _("Moved %sgroup %s to customer %s. Additional properties may have changed."
                  ) % (group_type, name,
                       managed.get_customer_name_by_id(new_customer)))
        else:
            _add_group_change(
                old_group_backup, "edit-%sgroups" % group_type,
                _("Updated properties of %sgroup %s") % (group_type, name))
    else:
        _add_group_change(
            extra_info, "edit-%sgroups" % group_type,
            _("Updated properties of %s group %s") % (group_type, name))
Пример #3
0
def is_alias_used(my_what: Union[GroupType, Literal["roles", "timeperiods"]],
                  my_name: GroupName,
                  my_alias: str) -> Tuple[bool, Optional[str]]:
    # Host / Service / Contact groups
    all_groups = load_group_information()
    for what, groups in all_groups.items():
        for gid, group in groups.items():
            if group["alias"] == my_alias and (my_what != what
                                               or my_name != gid):
                return False, _(
                    "This alias is already used in the %s group %s.") % (what,
                                                                         gid)

    # Timeperiods
    timeperiods = cmk.gui.watolib.timeperiods.load_timeperiods()
    for key, value in timeperiods.items():
        if timeperiod_spec_alias(value) == my_alias and (
                my_what != "timeperiods" or my_name != key):
            return False, _(
                "This alias is already used in timeperiod %s.") % key

    # Roles
    roles = userdb_utils.load_roles()
    for key, value in roles.items():
        if value.get("alias") == my_alias and (my_what != "roles"
                                               or my_name != key):
            return False, _("This alias is already used in the role %s.") % key

    return True, None
Пример #4
0
def prepare_groups(group_type: str,
                   entries: List[Dict[str, Any]]) -> Dict[str, Dict[str, Any]]:
    specific_existing_groups = load_group_information()[group_type]
    groups: Dict[str, Dict[str, Any]] = {}
    already_existing = []
    for details in entries:
        name = details['name']
        if name in specific_existing_groups:
            already_existing.append(name)
            continue
        group_details = {'alias': details['alias']}
        if version.is_managed_edition():
            group_details = update_customer_info(group_details,
                                                 details['customer'])
        groups[name] = group_details

    if already_existing:
        raise ProblemException(
            status=400,
            title=f"Some {group_type} groups already exist",
            detail=
            f"The following {group_type} group names already exist: {', '.join(already_existing)}",
        )

    return groups
Пример #5
0
def is_alias_used(my_what, my_name, my_alias):
    # Host / Service / Contact groups
    all_groups = load_group_information()
    for what, groups in all_groups.items():
        for gid, group in groups.items():
            if group['alias'] == my_alias and (my_what != what
                                               or my_name != gid):
                return False, _(
                    "This alias is already used in the %s group %s.") % (what,
                                                                         gid)

    # Timeperiods
    timeperiods = cmk.gui.watolib.timeperiods.load_timeperiods()
    for key, value in timeperiods.items():
        if timeperiod_spec_alias(value) == my_alias and (
                my_what != "timeperiods" or my_name != key):
            return False, _(
                "This alias is already used in timeperiod %s.") % key

    # Roles
    roles = userdb.load_roles()
    for key, value in roles.items():
        if value.get("alias") == my_alias and (my_what != "roles"
                                               or my_name != key):
            return False, _("This alias is already used in the role %s.") % key

    return True, None
Пример #6
0
def fetch_group(
    ident: str,
    group_type: GroupType,
    status: int = 404,
    message: Optional[str] = None,
) -> GroupSpec:
    groups = load_group_information()[group_type]
    group = _retrieve_group(ident, groups, status, message)
    group['id'] = ident
    return group
Пример #7
0
def fetch_group(
    ident: str,
    group_type: GroupType,
    status: int = 404,
    message: Optional[str] = None,
) -> GroupSpec:
    if user:
        check_modify_group_permissions(group_type)
    groups = load_group_information()[group_type]
    group = _retrieve_group(ident, groups, status, message)
    group["id"] = ident
    return group
Пример #8
0
def _verify_groups_exist(group_type: str, entries: List[Dict[str, Any]]):
    specific_existing_groups = load_group_information()[group_type]
    missing_groups = []
    for details in entries:
        name = details['name']
        if name not in specific_existing_groups:
            missing_groups.append(name)

    if missing_groups:
        raise ProblemException(
            status=400,
            title=f"Some {group_type} groups do not exist",
            detail=f"The following {group_type} groups do not exist: {', '.join(missing_groups)}")
Пример #9
0
def fetch_specific_groups(
    idents: List[str],
    group_type: GroupType,
    status: int = 404,
    message: Optional[str] = None,
) -> List[GroupSpec]:
    groups = load_group_information()[group_type]
    result = []
    for ident in idents:
        group = _retrieve_group(ident, groups, status, message)
        group['id'] = ident
        result.append(group)
    return result
Пример #10
0
def load_groups(group_type: str, entries: List[Dict[str, Any]]) -> Dict[str, Optional[str]]:
    specific_existing_groups = load_group_information()[group_type]
    group_details = {}
    already_existing = []
    for details in entries:
        name = details['name']
        if name in specific_existing_groups:
            already_existing.append(name)
            continue
        group_details[name] = details.get('alias')

    if already_existing:
        raise ProblemException(
            status=400,
            title=f"Some {group_type} groups already exist",
            detail=
            f"The following {group_type} group names already exist: {', '.join(already_existing)}",
        )

    return group_details
Пример #11
0
def delete_group(name: GroupName, group_type: GroupType) -> None:
    check_modify_group_permissions(group_type)
    # Check if group exists
    all_groups = load_group_information()
    groups = all_groups.get(group_type, {})
    if name not in groups:
        raise MKUserError(None, _("Unknown %s group: %s") % (group_type, name))

    # Check if still used
    usages = find_usages_of_group(name, group_type)
    if usages:
        raise MKUserError(
            None,
            _("Unable to delete group. It is still in use by: %s")
            % ", ".join([e[0] for e in usages]),
        )

    # Delete group
    group = groups.pop(name)
    save_group_information(all_groups)
    _add_group_change(group, "edit-%sgroups", _("Deleted %s group %s") % (group_type, name))
Пример #12
0
def verify_group_exists(group_type: GroupType, name: GroupName) -> bool:
    specific_existing_groups = load_group_information()[group_type]
    return name in specific_existing_groups
Пример #13
0
def verify_group_exist(group_type: str, name):
    specific_existing_groups = load_group_information()[group_type]
    return name in specific_existing_groups