Beispiel #1
0
def _rule_to_dto(rule_el: Element) -> CibRuleExpressionDto:
    children_dto_list = [
        _tag_to_export[child.tag](child)
        # The xpath method has a complicated return value, but we know our xpath
        # expression only returns elements.
        for child in cast(Element,
                          cast(_Element, rule_el).xpath(_xpath_for_export))
    ]
    # "and" is a documented pacemaker default
    # https://clusterlabs.org/pacemaker/doc/en-US/Pacemaker/2.0/html-single/Pacemaker_Explained/index.html#_rule_properties
    boolean_op = rule_el.get("boolean-op", "and")
    string_parts = []
    for child_dto in children_dto_list:
        if child_dto.type == CibRuleExpressionType.RULE:
            string_parts.append(f"({child_dto.as_string})")
        else:
            string_parts.append(child_dto.as_string)
    return CibRuleExpressionDto(
        rule_el.get("id", ""),
        _tag_to_type[rule_el.tag],
        False,  # TODO implement is_expired
        export_attributes(rule_el, with_id=False),
        None,
        None,
        children_dto_list,
        f" {boolean_op} ".join(string_parts),
    )
Beispiel #2
0
def export_with_set(element):
    return {
        "resource_sets": [
            resource_set.export(resource_set_item)
            for resource_set_item in element.findall(".//resource_set")
        ],
        "options": export_attributes(element),
    }
Beispiel #3
0
def export_with_set(element):
    return {
        "resource_sets": [
            resource_set.export(resource_set_item)
            for resource_set_item in element.findall(".//resource_set")
        ],
        "options": export_attributes(element),
    }
Beispiel #4
0
 def _common_expr_to_dto(self, expr_el: _Element) -> CibRuleExpressionDto:
     return CibRuleExpressionDto(
         str(expr_el.get("id", "")),
         self._tag_to_type[str(expr_el.tag)],
         CibRuleInEffectStatus.UNKNOWN,
         export_attributes(expr_el, with_id=False),
         None,
         None,
         [],
         self._str_eval.get_str(expr_el),
     )
Beispiel #5
0
def _common_expr_to_dto(expr_el: Element,
                        as_string: str) -> CibRuleExpressionDto:
    return CibRuleExpressionDto(
        expr_el.get("id", ""),
        _tag_to_type[expr_el.tag],
        False,
        export_attributes(expr_el, with_id=False),
        None,
        None,
        [],
        as_string,
    )
Beispiel #6
0
 def _date_expr_to_dto(self, expr_el: _Element) -> CibRuleExpressionDto:
     date_spec = expr_el.find("./date_spec")
     duration = expr_el.find("./duration")
     return CibRuleExpressionDto(
         str(expr_el.get("id", "")),
         self._tag_to_type[str(expr_el.tag)],
         CibRuleInEffectStatus.UNKNOWN,
         export_attributes(expr_el, with_id=False),
         None if date_spec is None else self._date_common_to_dto(date_spec),
         None if duration is None else self._date_common_to_dto(duration),
         [],
         self._str_eval.get_str(expr_el),
     )
Beispiel #7
0
def nvset_element_to_dto(nvset_el: _Element) -> CibNvsetDto:
    """
    Export an nvset xml element to its DTO
    """
    rule_el = nvset_el.find("./rule")
    return CibNvsetDto(
        str(nvset_el.get("id", "")),
        _tag_to_type[str(nvset_el.tag)],
        export_attributes(nvset_el, with_id=False),
        None if rule_el is None else rule_element_to_dto(rule_el),
        [
            nvpair_element_to_dto(nvpair_el)
            for nvpair_el in nvset_el.iterfind("./nvpair")
        ],
    )
Beispiel #8
0
 def _rule_to_dto(self, rule_el: _Element) -> CibRuleExpressionDto:
     children_dto_list = [
         self._tag_to_export[str(child.tag)](self, child)
         # The xpath method has a complicated return value, but we know our
         # xpath expression only returns elements.
         for child in cast(_Element, rule_el.xpath(self._xpath_for_export))
     ]
     rule_id = str(rule_el.get("id", ""))
     return CibRuleExpressionDto(
         rule_id,
         self._tag_to_type[str(rule_el.tag)],
         self._in_effect_eval.get_rule_status(rule_id),
         export_attributes(rule_el, with_id=False),
         None,
         None,
         children_dto_list,
         self._str_eval.get_str(rule_el),
     )
Beispiel #9
0
def nvset_element_to_dto(nvset_el: _Element,
                         rule_in_effect_eval: RuleInEffectEval) -> CibNvsetDto:
    """
    Export an nvset xml element to its DTO

    nvset_el -- an nvset element to be exported
    rule_in_effect_eval -- a class for evaluating if a rule is in effect
    """
    rule_dto = None
    rule_el = nvset_el.find("./rule")
    if rule_el is not None:
        rule_dto = rule_element_to_dto(rule_in_effect_eval, rule_el)
    return CibNvsetDto(
        str(nvset_el.get("id", "")),
        export_attributes(nvset_el, with_id=False),
        rule_dto,
        [
            nvpair_element_to_dto(nvpair_el)
            for nvpair_el in nvset_el.iterfind("./nvpair")
        ],
    )
Beispiel #10
0
def _date_expr_to_dto(expr_el: Element) -> CibRuleExpressionDto:
    date_spec = expr_el.find("./date_spec")
    duration = expr_el.find("./duration")

    string_parts = []
    # "operation" is defined as mandatory in CIB schema
    operation = expr_el.get("operation", "")
    if operation == "date_spec":
        string_parts.append("date-spec")
        if date_spec is not None:
            string_parts.append(_attrs_to_str(date_spec))
    elif operation == "in_range":
        string_parts.extend(["date", "in_range"])
        # CIB schema allows "start" + "duration" or optional "start" + "end"
        if "start" in expr_el.attrib:
            string_parts.extend([expr_el.get("start", ""), "to"])
        if "end" in expr_el.attrib:
            string_parts.append(expr_el.get("end", ""))
        if duration is not None:
            string_parts.append("duration")
            string_parts.append(_attrs_to_str(duration))
    else:
        # CIB schema allows operation=="gt" + "start" or operation=="lt" + "end"
        string_parts.extend(["date", expr_el.get("operation", "")])
        if "start" in expr_el.attrib:
            string_parts.append(expr_el.get("start", ""))
        if "end" in expr_el.attrib:
            string_parts.append(expr_el.get("end", ""))

    return CibRuleExpressionDto(
        expr_el.get("id", ""),
        _tag_to_type[expr_el.tag],
        False,
        export_attributes(expr_el, with_id=False),
        None if date_spec is None else _date_common_to_dto(date_spec),
        None if duration is None else _date_common_to_dto(duration),
        [],
        " ".join(string_parts),
    )
Beispiel #11
0
 def _attrs_to_str(el: _Element) -> str:
     return " ".join(
         format_name_value_list(
             sorted(export_attributes(el, with_id=False).items())
         )
     )
Beispiel #12
0
def export_plain(element: _Element) -> Dict[str, Any]:
    return {"options": export_attributes(element)}
Beispiel #13
0
def export_plain(element):
    return {"options": export_attributes(element)}
Beispiel #14
0
def export(element):
    return {
        "ids": get_resource_id_set_list(element),
        "options": export_attributes(element),
    }
Beispiel #15
0
def export(element):
    return {
        "ids": get_resource_id_set_list(element),
        "options": export_attributes(element),
    }
Beispiel #16
0
def export_plain(element):
    return {"options": export_attributes(element)}
Beispiel #17
0
def _date_common_to_dto(expr_el: Element) -> CibRuleDateCommonDto:
    return CibRuleDateCommonDto(
        expr_el.get("id", ""),
        export_attributes(expr_el, with_id=False),
    )