Esempio n. 1
0
    def _update_rule_of_host(self, ruleset: watolib.Ruleset, service_patterns: List[Dict[str, str]],
                             value: Any) -> List[watolib.CREFolder]:
        folder = self._host.folder()
        rule = self._get_rule_of_host(ruleset, value)

        if rule:
            for service_condition in service_patterns:
                if service_condition not in rule.conditions.service_description:
                    rule.conditions.service_description.append(service_condition)

        elif service_patterns:
            rule = watolib.Rule.create(folder, ruleset)

            conditions = RuleConditions(folder.path())
            conditions.host_name = [self._host.name()]
            # mypy is wrong here vor some reason:
            # Invalid index type "str" for "Union[Dict[str, str], str]"; expected type "Union[int, slice]"  [index]
            conditions.service_description = sorted(
                service_patterns, key=lambda x: x["$regex"])  # type: ignore[index]
            rule.update_conditions(conditions)

            rule.value = value
            ruleset.prepend_rule(folder, rule)

        if rule:
            return [rule.folder]
        return []
Esempio n. 2
0
def _serialize_ruleset(ruleset: watolib.Ruleset) -> DomainObject:
    members = {}
    if ruleset.num_rules() > 0:
        members["rules"] = constructors.collection_property(
            name="rules",
            value=[],
            base="",
        )

    return constructors.domain_object(
        domain_type="ruleset",
        identifier=ruleset.name.replace(":", "-"),
        title=ruleset.title() or ruleset.name,
        editable=False,
        deletable=False,
        members=members,
        extensions={
            "name": ruleset.name,
            "title": ruleset.title(),
            "item_type": ruleset.item_type(),
            "item_name": ruleset.item_name(),
            "item_enum": ruleset.item_enum(),
            "match_type": ruleset.match_type(),
            "help": ruleset.help(),
            "number_of_rules": ruleset.num_rules(),
        },
    )