Exemplo n.º 1
0
def prepare_options_with_set(cib, options, resource_set_list):
    options = constraint.prepare_options(
        tuple(ATTRIB.keys()),
        options,
        create_id=partial(
            constraint.create_id, cib, TAG_NAME, resource_set_list
        ),
        validate_id=partial(check_new_id_applicable, cib, DESCRIPTION),
    )

    report_items = []
    if "kind" in options:
        kind = options["kind"].lower().capitalize()
        if kind not in ATTRIB["kind"]:
            report_items.append(reports.invalid_option_value(
                "kind", options["kind"], ATTRIB["kind"]
            ))
        options["kind"] = kind

    if "symmetrical" in options:
        symmetrical = options["symmetrical"].lower()
        if symmetrical not in ATTRIB["symmetrical"]:
            report_items.append(reports.invalid_option_value(
                "symmetrical", options["symmetrical"], ATTRIB["symmetrical"]
            ))
        options["symmetrical"] = symmetrical

    if report_items:
        raise LibraryError(*report_items)

    return options
Exemplo n.º 2
0
 def test_complete_id(self):
     mock_create_id = mock.MagicMock()
     mock_create_id.return_value = "new-id"
     self.assertEqual({"id": "new-id"},
                      constraint.prepare_options(("a", ), {},
                                                 mock_create_id,
                                                 mock.MagicMock()))
Exemplo n.º 3
0
def prepare_options_plain(cib, options, ticket, resource_id):
    options = options.copy()

    report = _validate_options_common(options)

    if not ticket:
        report.append(reports.required_options_are_missing(['ticket']))
    options["ticket"] = ticket

    if not resource_id:
        report.append(reports.required_options_are_missing(['rsc']))
    options["rsc"] = resource_id

    if "rsc-role" in options:
        if options["rsc-role"]:
            resource_role = options["rsc-role"].lower().capitalize()
            if resource_role not in ATTRIB_PLAIN["rsc-role"]:
                report.append(
                    reports.invalid_option_value("rsc-role",
                                                 options["rsc-role"],
                                                 ATTRIB_PLAIN["rsc-role"]))
            options["rsc-role"] = resource_role
        else:
            del options["rsc-role"]

    if report:
        raise LibraryError(*report)

    return constraint.prepare_options(
        tuple(list(ATTRIB) + list(ATTRIB_PLAIN)), options,
        partial(_create_id, cib, options["ticket"], resource_id,
                options.get("rsc-role", "")),
        partial(tools.check_new_id_applicable, cib, DESCRIPTION))
Exemplo n.º 4
0
def prepare_options_with_set(cib, options, resource_set_list):
    options = constraint.prepare_options(
        tuple(ATTRIB.keys()),
        options,
        create_id=partial(
            constraint.create_id, cib, TAG_NAME, resource_set_list
        ),
        validate_id=partial(check_new_id_applicable, cib, DESCRIPTION),
    )

    report_items = []
    if "kind" in options:
        kind = options["kind"].lower().capitalize()
        if kind not in ATTRIB["kind"]:
            report_items.append(reports.invalid_option_value(
                "kind", options["kind"], ATTRIB["kind"]
            ))
        options["kind"] = kind

    if "symmetrical" in options:
        symmetrical = options["symmetrical"].lower()
        if symmetrical not in ATTRIB["symmetrical"]:
            report_items.append(reports.invalid_option_value(
                "symmetrical", options["symmetrical"], ATTRIB["symmetrical"]
            ))
        options["symmetrical"] = symmetrical

    if report_items:
        raise LibraryError(*report_items)

    return options
Exemplo n.º 5
0
 def test_refuse_unknown_option(self):
     assert_raise_library_error(
         lambda: constraint.prepare_options(
             ("a", ), {"b": "c"}, mock.MagicMock(), mock.MagicMock()),
         (severities.ERROR, report_codes.INVALID_OPTION, {
             "option_names": ["b"],
             "option_type": None,
             "allowed": ["a", "id"],
         }),
     )
Exemplo n.º 6
0
 def test_refuse_invalid_id(self):
     class SomeException(Exception):
         pass
     mock_validate_id = mock.MagicMock()
     mock_validate_id.side_effect = SomeException()
     self.assertRaises(
         SomeException,
         lambda: constraint.prepare_options(
             ("a", ), {"id": "invalid"}, mock.MagicMock(), mock_validate_id
         ),
     )
     mock_validate_id.assert_called_once_with("invalid")
Exemplo n.º 7
0
 def test_has_no_side_efect_on_input_options(self):
     mock_create_id = mock.MagicMock()
     mock_create_id.return_value = "new-id"
     options = {"a": "b"}
     self.assertEqual({
         "id": "new-id",
         "a": "b"
     },
                      constraint.prepare_options(("a", ), options,
                                                 mock_create_id,
                                                 mock.MagicMock()))
     self.assertEqual({"a": "b"}, options)
Exemplo n.º 8
0
    def test_refuse_invalid_id(self):
        class SomeException(Exception):
            pass

        mock_validate_id = mock.MagicMock()
        mock_validate_id.side_effect = SomeException()
        self.assertRaises(
            SomeException,
            lambda: constraint.prepare_options((
                "a", ), {"id": "invalid"}, mock.MagicMock(), mock_validate_id),
        )
        mock_validate_id.assert_called_once_with("invalid")
Exemplo n.º 9
0
 def test_has_no_side_efect_on_input_options(self):
     mock_create_id = mock.MagicMock()
     mock_create_id.return_value = "new-id"
     options = {"a": "b"}
     self.assertEqual(
         {"id": "new-id", "a": "b"},
         constraint.prepare_options(
             ("a",),
             options,
             mock_create_id, mock.MagicMock()
         )
     )
     self.assertEqual({"a": "b"}, options)
Exemplo n.º 10
0
def prepare_options_with_set(cib, options, resource_set_list):
    options = constraint.prepare_options(
        tuple(ATTRIB.keys()),
        options,
        create_id_fn=partial(constraint.create_id, cib, TAG_NAME,
                             resource_set_list),
        validate_id=partial(tools.check_new_id_applicable, cib, DESCRIPTION),
    )
    report = _validate_options_common(options)
    if "ticket" not in options or not options["ticket"].strip():
        report.append(reports.required_options_are_missing(['ticket']))
    if report:
        raise LibraryError(*report)
    return options
Exemplo n.º 11
0
def prepare_options_with_set(cib, options, resource_set_list):
    options = constraint.prepare_options(
        tuple(ATTRIB.keys()),
        options,
        create_id_fn=partial(
            constraint.create_id, cib, TAG_NAME, resource_set_list
        ),
        validate_id=partial(tools.check_new_id_applicable, cib, DESCRIPTION),
    )
    report = _validate_options_common(options)
    if "ticket" not in options or not options["ticket"].strip():
        report.append(reports.required_option_is_missing(['ticket']))
    if report:
        raise LibraryError(*report)
    return options
Exemplo n.º 12
0
 def test_refuse_unknown_option(self):
     assert_raise_library_error(
         lambda: constraint.prepare_options(
             ("a", ), {"b": "c"}, mock.MagicMock(), mock.MagicMock()
         ),
         (
             severities.ERROR,
             report_codes.INVALID_OPTION,
             {
                 "option_name": "b",
                 "option_type": None,
                 "allowed": ["a", "id"],
             }
         ),
     )
Exemplo n.º 13
0
def prepare_options_with_set(cib, options, resource_set_list):
    options = constraint.prepare_options(
        tuple(ATTRIB.keys()),
        options,
        create_id_fn=partial(constraint.create_id, cib, "ticket",
                             resource_set_list),
        validate_id=partial(tools.check_new_id_applicable, cib, DESCRIPTION),
    )
    report_list = _validate_options_common(options)
    if "ticket" not in options or not options["ticket"].strip():
        report_list.append(
            ReportItem.error(
                reports.messages.RequiredOptionsAreMissing(["ticket"])))
    if report_list:
        raise LibraryError(*report_list)
    return options
Exemplo n.º 14
0
def prepare_options_with_set(cib, options, resource_set_list):
    options = constraint.prepare_options(
        ("score", ),
        options,
        partial(constraint.create_id, cib, "colocation", resource_set_list),
        partial(check_new_id_applicable, cib, DESCRIPTION),
    )

    if "score" in options:
        if not is_score(options["score"]):
            raise LibraryError(
                ReportItem.error(
                    reports.messages.InvalidScore(options["score"])))
    else:
        options["score"] = SCORE_INFINITY

    return options
Exemplo n.º 15
0
def prepare_options_with_set(cib, options, resource_set_list):
    options = constraint.prepare_options(
        tuple(SCORE_NAMES),
        options,
        partial(constraint.create_id, cib, TAG_NAME, resource_set_list),
        partial(check_new_id_applicable, cib, DESCRIPTION),
    )

    if "score" in options and not is_score(options["score"]):
        raise LibraryError(reports.invalid_score(options["score"]))

    score_attrs_count = len(
        [name for name in options.keys() if name in SCORE_NAMES])
    if score_attrs_count > 1:
        raise LibraryError(reports.multiple_score_options())

    if score_attrs_count == 0:
        options["score"] = SCORE_INFINITY

    return options
Exemplo n.º 16
0
def prepare_options_with_set(cib, options, resource_set_list):
    options = constraint.prepare_options(
        tuple(SCORE_NAMES),
        options,
        partial(constraint.create_id, cib, TAG_NAME, resource_set_list),
        partial(check_new_id_applicable, cib, DESCRIPTION),
    )

    if "score" in options and not is_score(options["score"]):
        raise LibraryError(reports.invalid_score(options["score"]))

    score_attrs_count = len([
        name for name in options.keys() if name in SCORE_NAMES
    ])
    if score_attrs_count > 1:
        raise LibraryError(reports.multiple_score_options())

    if score_attrs_count == 0:
        options["score"] = SCORE_INFINITY

    return options
Exemplo n.º 17
0
def prepare_options_plain(cib, options, ticket, resource_id):
    options = options.copy()

    report = _validate_options_common(options)

    if not ticket:
        report.append(reports.required_option_is_missing(['ticket']))
    options["ticket"] = ticket

    if not resource_id:
        report.append(reports.required_option_is_missing(['rsc']))
    options["rsc"] = resource_id

    if "rsc-role" in options:
        if options["rsc-role"]:
            resource_role = options["rsc-role"].lower().capitalize()
            if resource_role not in ATTRIB_PLAIN["rsc-role"]:
                report.append(reports.invalid_option_value(
                    "rsc-role", options["rsc-role"], ATTRIB_PLAIN["rsc-role"]
                ))
            options["rsc-role"] = resource_role
        else:
            del options["rsc-role"]

    if report:
        raise LibraryError(*report)

    return constraint.prepare_options(
        tuple(list(ATTRIB) + list(ATTRIB_PLAIN)),
        options,
        partial(
            _create_id,
            cib,
            options["ticket"],
            resource_id,
            options.get("rsc-role", "")
        ),
        partial(tools.check_new_id_applicable, cib, DESCRIPTION)
    )
Exemplo n.º 18
0
def prepare_options_plain(
    cib: _Element,
    report_processor: reports.ReportProcessor,
    options,
    ticket: str,
    resource_id,
):
    options = options.copy()

    report_processor.report_list(_validate_options_common(options))

    if not ticket:
        report_processor.report(
            ReportItem.error(
                reports.messages.RequiredOptionsAreMissing(["ticket"])))
    else:
        report_processor.report_list(validate_ticket_name(ticket))
    options["ticket"] = ticket

    if not resource_id:
        report_processor.report(
            ReportItem.error(
                reports.messages.RequiredOptionsAreMissing(["rsc"])))
    options["rsc"] = resource_id

    role_value_validator = validate.ValueIn("rsc-role",
                                            const.PCMK_ROLES,
                                            option_name_for_report="role")
    role_value_validator.empty_string_valid = True

    validators = [
        role_value_validator,
        validate.ValueDeprecated(
            "rsc-role",
            {
                const.PCMK_ROLE_PROMOTED_LEGACY: const.PCMK_ROLE_PROMOTED,
                const.PCMK_ROLE_UNPROMOTED_LEGACY: const.PCMK_ROLE_UNPROMOTED,
            },
            reports.ReportItemSeverity.deprecation(),
            option_name_for_report="role",
        ),
    ]
    report_processor.report_list(
        validate.ValidatorAll(validators).validate(
            validate.values_to_pairs(
                options,
                validate.option_value_normalization(
                    {"rsc-role": lambda value: value.capitalize()}),
            )))

    if report_processor.has_errors:
        raise LibraryError()

    if "rsc-role" in options:
        if options["rsc-role"]:
            options["rsc-role"] = pacemaker.role.get_value_for_cib(
                options["rsc-role"].capitalize(),
                tools.are_new_role_names_supported(cib),
            )
        else:
            del options["rsc-role"]

    return constraint.prepare_options(
        tuple(list(ATTRIB) + list(ATTRIB_PLAIN)),
        options,
        partial(
            _create_id,
            cib,
            options["ticket"],
            resource_id,
            options.get("rsc-role", ""),
        ),
        partial(tools.check_new_id_applicable, cib, DESCRIPTION),
    )
Exemplo n.º 19
0
 def test_complete_id(self):
     mock_create_id = mock.MagicMock()
     mock_create_id.return_value = "new-id"
     self.assertEqual({"id": "new-id"}, constraint.prepare_options(
         ("a",), {}, mock_create_id, mock.MagicMock()
     ))