Exemplo n.º 1
0
Arquivo: common.py Projeto: vvidic/pcs
def create_with_set(
    tag_name,
    prepare_options,
    env,
    resource_set_list,
    constraint_options,
    resource_in_clone_alowed=False,
    duplication_alowed=False,
    duplicate_check=None,
):
    """
    string tag_name is constraint tag name
    callable prepare_options takes
        cib(Element), options(dict), resource_set_list and return corrected
        options or if options not usable raises error
    env is library environment
    list resource_set_list is description of resource set, for example:
        {"ids": ["A", "B"], "options": {"sequential": "true"}},
    dict constraint_options is base for building attributes of constraint tag
    bool resource_in_clone_alowed flag for allowing to reference id which is
        in tag clone or master
    bool duplication_alowed flag for allowing create duplicate element
    callable duplicate_check takes two elements and decide if they are
        duplicates
    """
    cib = env.get_cib()

    find_valid_resource_id = partial(
        constraint.find_valid_resource_id,
        env.report_processor,
        cib,
        resource_in_clone_alowed,
    )

    constraint_section = get_constraints(cib)
    constraint_element = constraint.create_with_set(
        constraint_section,
        tag_name,
        options=prepare_options(cib, constraint_options, resource_set_list),
        resource_set_list=[
            resource_set.prepare_set(
                find_valid_resource_id, resource_set_item, env.report_processor
            )
            for resource_set_item in resource_set_list
        ],
    )

    if not duplicate_check:
        duplicate_check = constraint.have_duplicate_resource_sets

    constraint.check_is_without_duplication(
        env.report_processor,
        constraint_section,
        constraint_element,
        are_duplicate=duplicate_check,
        export_element=constraint.export_with_set,
        duplication_alowed=duplication_alowed,
    )

    env.push_cib()
Exemplo n.º 2
0
 def test_success_non_empty_list_for_remove(self):
     # pylint: disable=no-self-use
     assert_report_item_list_equal(
         lib.validate_remove_tag(
             get_constraints(self.tree_each_tag_has_one_constraint),
             ["not-referenced-tag"],
         ),
         [],
     )
Exemplo n.º 3
0
 def test_raise_if_missing(self):
     for section in self.cib.tree.findall(".//configuration/constraints"):
         section.getparent().remove(section)
     assert_raise_library_error(
         lambda: lib.get_constraints(self.cib.tree),
         (severities.ERROR, report_codes.CIB_CANNOT_FIND_MANDATORY_SECTION,
          {
              "section": "configuration/constraints",
          }),
     )
Exemplo n.º 4
0
def create_with_set(
    tag_name, prepare_options, env, resource_set_list, constraint_options,
    can_repair_to_clone=False,
    resource_in_clone_alowed=False,
    duplication_alowed=False,
    duplicate_check=None,
):
    """
    string tag_name is constraint tag name
    callable prepare_options takes
        cib(Element), options(dict), resource_set_list and return corrected
        options or if options not usable raises error
    env is library environment
    list resource_set_list is description of resource set, for example:
        {"ids": ["A", "B"], "options": {"sequential": "true"}},
    dict constraint_options is base for building attributes of constraint tag
    bool resource_in_clone_alowed flag for allowing to reference id which is
        in tag clone or master
    bool duplication_alowed flag for allowing create duplicate element
    callable duplicate_check takes two elements and decide if they are
        duplicates
    """
    cib = env.get_cib()

    find_valid_resource_id = partial(
        constraint.find_valid_resource_id,
        env.report_processor, cib, can_repair_to_clone, resource_in_clone_alowed
    )

    constraint_section = get_constraints(cib)
    constraint_element = constraint.create_with_set(
        constraint_section,
        tag_name,
        options=prepare_options(cib, constraint_options, resource_set_list),
        resource_set_list=[
             resource_set.prepare_set(find_valid_resource_id, resource_set_item)
             for resource_set_item in resource_set_list
        ]
    )

    if not duplicate_check:
        duplicate_check = constraint.have_duplicate_resource_sets

    constraint.check_is_without_duplication(
        env.report_processor,
        constraint_section,
        constraint_element,
        are_duplicate=duplicate_check,
        export_element=constraint.export_with_set,
        duplication_alowed=duplication_alowed,
    )

    env.push_cib(cib)
Exemplo n.º 5
0
 def test_fail_empty_list_for_remove(self):
     # pylint: disable=no-self-use
     assert_report_item_list_equal(
         lib.validate_remove_tag(
             get_constraints(self.tree_each_tag_has_one_constraint),
             [],
         ),
         [
             fixture.error(
                 reports.codes.TAG_CANNOT_REMOVE_TAGS_NO_TAGS_SPECIFIED)
         ],
     )
Exemplo n.º 6
0
 def test_raise_if_missing(self):
     for section in self.cib.tree.findall(".//configuration/constraints"):
         section.getparent().remove(section)
     assert_raise_library_error(
         lambda: lib.get_constraints(self.cib.tree),
         (
             severities.ERROR,
             report_codes.CIB_CANNOT_FIND_MANDATORY_SECTION,
             {
                 "section": "configuration/constraints",
             }
         ),
     )
Exemplo n.º 7
0
def remove(env, ticket_key, resource_id):
    """
    remove all ticket constraint from resource
    If resource is in resource set with another resources then only resource ref
    is removed. If resource is alone in resource set whole constraint is removed.
    """
    constraint_section = get_constraints(env.get_cib())
    any_plain_removed = ticket.remove_plain(constraint_section, ticket_key,
                                            resource_id)
    any_with_resource_set_removed = ticket.remove_with_resource_set(
        constraint_section, ticket_key, resource_id)

    env.push_cib()

    return any_plain_removed or any_with_resource_set_removed
Exemplo n.º 8
0
def show(tag_name, is_plain, env):
    """
    string tag_name is constraint tag name
    callable is_plain takes constraint element and returns if is plain (i.e.
        without resource set)
    env is library environment
    """
    constraints_info = {"plain": [], "with_resource_sets": []}
    for element in get_constraints(env.get_cib()).findall(".//" + tag_name):
        if is_plain(element):
            constraints_info["plain"].append(constraint.export_plain(element))
        else:
            constraints_info["with_resource_sets"].append(
                constraint.export_with_set(element))
    return constraints_info
Exemplo n.º 9
0
 def test_fail_tag_referenced_in_constraint(self):
     # pylint: disable=no-self-use
     assert_report_item_list_equal(
         lib.validate_remove_tag(
             get_constraints(self.tree_each_tag_has_one_constraint),
             ["tag-location"],
         ),
         [
             fixture.error(
                 # pylint: disable=line-too-long
                 reports.codes.TAG_CANNOT_REMOVE_TAG_REFERENCED_IN_CONSTRAINTS,
                 tag_id="tag-location",
                 constraint_id_list=["location"],
             )
         ],
     )
Exemplo n.º 10
0
def show(tag_name, is_plain, env):
    """
    string tag_name is constraint tag name
    callable is_plain takes constraint element and returns if is plain (i.e.
        without resource set)
    env is library environment
    """
    constraints_info = {"plain": [], "with_resource_sets": []}
    for element in get_constraints(env.get_cib()).findall(".//"+tag_name):
        if is_plain(element):
            constraints_info["plain"].append(constraint.export_plain(element))
        else:
            constraints_info["with_resource_sets"].append(
                constraint.export_with_set(element)
            )
    return constraints_info
Exemplo n.º 11
0
 def test_fail_tag_referenced_in_multiple_constraint(self):
     # pylint: disable=no-self-use
     assert_report_item_list_equal(
         lib.validate_remove_tag(
             get_constraints(self.tree_tag_has_multiple_constraints),
             ["multitag"],
         ),
         [
             fixture.error(
                 # pylint: disable=line-too-long
                 reports.codes.TAG_CANNOT_REMOVE_TAG_REFERENCED_IN_CONSTRAINTS,
                 tag_id="multitag",
                 constraint_id_list=sorted(
                     set(self.tag2constraint_id.values()),
                 ),
             )
         ],
     )
Exemplo n.º 12
0
def create(
    env,
    ticket_key,
    resource_id,
    options,
    resource_in_clone_alowed=False,
    duplication_alowed=False,
):
    """
    create ticket constraint
    string ticket_key ticket for constraining resource
    dict options desired constraint attributes
    bool resource_in_clone_alowed flag for allowing to reference id which is
        in tag clone or master
    bool duplication_alowed flag for allowing create duplicate element
    callable duplicate_check takes two elements and decide if they are
        duplicates
    """
    cib = env.get_cib()

    options = ticket.prepare_options_plain(
        cib,
        env.report_processor,
        options,
        ticket_key,
        constraint.find_valid_resource_id(env.report_processor, cib,
                                          resource_in_clone_alowed,
                                          resource_id),
    )

    constraint_section = get_constraints(cib)
    constraint_element = ticket.create_plain(constraint_section, options)

    constraint.check_is_without_duplication(
        env.report_processor,
        constraint_section,
        constraint_element,
        are_duplicate=ticket.get_duplicit_checker_callback(
            are_new_role_names_supported(constraint_section)),
        export_element=constraint.export_plain,
        duplication_alowed=duplication_alowed,
    )

    env.push_cib()
Exemplo n.º 13
0
Arquivo: tag.py Projeto: nrwahl2/pcs
def remove(env: LibraryEnvironment, tag_list: Iterable[str]) -> None:
    """
    Remove specified tags from a cib.

    env -- provides all for communication with externals
    tag_list -- list of tags for the removal
    """
    with cib_tags_section(env) as tags_section:
        env.report_processor.report_list(
            tag.validate_remove_tag(
                get_constraints(get_root(tags_section)), tag_list,
            )
        )
        tag_elements, report_list = tag.find_tag_elements_by_ids(
            tags_section, tag_list,
        )
        if env.report_processor.report_list(report_list).has_errors:
            raise LibraryError()
        tag.remove_tag(tag_elements)
Exemplo n.º 14
0
def create(
    env, ticket_key, resource_id, options,
    autocorrection_allowed=False,
    resource_in_clone_alowed=False,
    duplication_alowed=False,
):
    """
    create ticket constraint
    string ticket_key ticket for constraining resource
    dict options desired constraint attributes
    bool resource_in_clone_alowed flag for allowing to reference id which is
        in tag clone or master
    bool duplication_alowed flag for allowing create duplicate element
    callable duplicate_check takes two elements and decide if they are
        duplicates
    """
    cib = env.get_cib()

    options = ticket.prepare_options_plain(
        cib,
        options,
        ticket_key,
        constraint.find_valid_resource_id(
            env.report_processor, cib,
            autocorrection_allowed, resource_in_clone_alowed, resource_id
        ),
    )

    constraint_section = get_constraints(cib)
    constraint_element = ticket.create_plain(constraint_section, options)

    constraint.check_is_without_duplication(
        env.report_processor,
        constraint_section,
        constraint_element,
        are_duplicate=ticket.are_duplicate_plain,
        export_element=constraint.export_plain,
        duplication_alowed=duplication_alowed,
    )

    env.push_cib(cib)
Exemplo n.º 15
0
def remove(env, ticket_key, resource_id):
    """
    remove all ticket constraint from resource
    If resource is in resource set with another resources then only resource ref
    is removed. If resource is alone in resource set whole constraint is removed.
    """
    cib = env.get_cib()
    constraint_section = get_constraints(cib)
    any_plain_removed = ticket.remove_plain(
        constraint_section,
        ticket_key,
        resource_id
    )
    any_with_resource_set_removed = ticket.remove_with_resource_set(
        constraint_section,
        ticket_key,
        resource_id
    )

    env.push_cib(cib)

    return any_plain_removed or any_with_resource_set_removed
Exemplo n.º 16
0
 def test_success_if_exists(self):
     self.assertEqual("constraints", lib.get_constraints(self.cib.tree).tag)
Exemplo n.º 17
0
 def __init__(self, cib: _Element):
     self._cib = cib
     self._resources_section = tools.get_resources(self._cib)
     self._constraints_section = tools.get_constraints(self._cib)
Exemplo n.º 18
0
 def test_success_if_exists(self):
     self.assertEqual(
         "constraints",
         lib.get_constraints(self.cib.tree).tag
     )
Exemplo n.º 19
0
 def call_find_constraints_referencing_tag(tree, tag_id):
     return lib.find_constraints_referencing_tag(
         get_constraints(tree),
         tag_id,
     )