def update_node_instance_attrs(cib, node_name, attrs, state_nodes=None): """ Update nvpairs in instance_attributes for a node specified by its name. Automatically creates instance_attributes element if needed. If the node has more than one instance_attributes element, the first one is modified. If the node is missing in the CIB, it is automatically created if its state is provided in state_nodes. etree cib -- cib string node_name -- name of the node to be updated dict attrs -- attrs to update, e.g. {'A': 'a', 'B': ''} iterable state_nodes -- optional list of node state objects """ node_el = _ensure_node_exists(get_nodes(cib), node_name, state_nodes) # If no instance_attributes id is specified, crm_attribute modifies the # first one found. So we just mimic this behavior here. attrs_el = node_el.find("./instance_attributes") if attrs_el is None: attrs_el = etree.SubElement( node_el, "instance_attributes", id=find_unique_id(cib, "nodes-{0}".format(node_el.get("id"))) ) update_nvset(attrs_el, attrs)
def create(parent, resource_set): """ parent - lxml element for append new resource_set """ element = etree.SubElement(parent, "resource_set") element.attrib.update(resource_set["options"]) element.attrib["id"] = find_unique_id(parent.getroottree(), "pcs_rsc_set_{0}".format("_".join(resource_set["ids"]))) for id in resource_set["ids"]: etree.SubElement(element, "resource_ref").attrib["id"] = id return element
def create_id(primitive_element): """ Create id for clone element based on contained primitive_element. etree.Element primitive_element is resource which will be cloned. It must be connected into the cib to ensure that the resulting id is unique! """ return find_unique_id( primitive_element, "{0}-{1}".format(primitive_element.get("id"), TAG_CLONE) )
def create_id(clone_tag, primitive_element): """ Create id for clone element based on contained primitive_element. string clone_tag is tag of clone element. Specialization of "clone" is "master" and this function is common for both - "clone" and "master". etree.Element primitive_element is resource which will be cloned. It must be connected into the cib to ensure that the resulting id is unique! """ return find_unique_id( primitive_element, "{0}-{1}".format(primitive_element.get("id"), clone_tag))
def create_id(cib, type_prefix, resource_set_list): # Create a semi-random id. We need it to be predictable (for testing), short # and somehow different than other ids so that we don't spend much time in # find_unique_id. # Avoid using actual resource names. It makes the id very long (consider 10 # or more resources in a set constraint). Also, if a resource is deleted # and therefore removed from the constraint, the id no longer matches the # constraint. resource_ids = [] for _set in resource_set_list: resource_ids.extend(_set["ids"]) id_part = "".join([_id[0] + _id[-1] for _id in resource_ids][:3]) return find_unique_id(cib, "{0}_set_{1}".format(type_prefix, id_part))
def create(parent, resource_set): """ parent - lxml element for append new resource_set """ element = etree.SubElement(parent, "resource_set") element.attrib.update(resource_set["options"]) element.attrib["id"] = find_unique_id( parent.getroottree(), "pcs_rsc_set_{0}".format("_".join(resource_set["ids"]))) for _id in resource_set["ids"]: etree.SubElement(element, "resource_ref").attrib["id"] = _id return element
def create_id(clone_tag, primitive_element): """ Create id for clone element based on contained primitive_element. string clone_tag is tag of clone element. Specialization of "clone" is "master" and this function is common for both - "clone" and "master". etree.Element primitive_element is resource which will be cloned. It must be connected into the cib to ensure that the resulting id is unique! """ return find_unique_id( primitive_element, "{0}-{1}".format(primitive_element.get("id"), clone_tag) )
def create(parent, resource_set): """ parent - lxml element for append new resource_set """ element = etree.SubElement(parent, "resource_set") element.attrib.update(resource_set["options"]) element.attrib["id"] = find_unique_id( parent.getroottree(), "{0}_set".format(parent.attrib.get("id", "constraint_set")), ) for _id in resource_set["ids"]: etree.SubElement(element, "resource_ref").attrib["id"] = _id return element
def _append_level_element(tree, level, target_type, target_value, devices): level_el = etree.SubElement(tree, "fencing-level", index=str(level), devices=",".join(devices)) if target_type == TARGET_TYPE_NODE: level_el.set("target", target_value) id_part = target_value elif target_type == TARGET_TYPE_REGEXP: level_el.set("target-pattern", target_value) id_part = target_value elif target_type == TARGET_TYPE_ATTRIBUTE: level_el.set("target-attribute", target_value[0]) level_el.set("target-value", target_value[1]) id_part = target_value[0] level_el.set( "id", find_unique_id(tree, sanitize_id("fl-{0}-{1}".format(id_part, level)))) return level_el
def create(parent, resource_set): """ parent - lxml element for append new resource_set """ element = etree.SubElement(parent, "resource_set") if "role" in resource_set["options"]: resource_set["options"]["role"] = pacemaker.role.get_value_for_cib( resource_set["options"]["role"], is_latest_supported=are_new_role_names_supported(parent), ) element.attrib.update(resource_set["options"]) element.attrib["id"] = find_unique_id( parent.getroottree(), "{0}_set".format(parent.attrib.get("id", "constraint_set")), ) for _id in resource_set["ids"]: etree.SubElement(element, "resource_ref").attrib["id"] = _id return element
def create_alert(tree, alert_id, path, description=""): """ Create new alert element. Returns newly created element. Raises LibraryError if element with specified id already exists. tree -- cib etree node alert_id -- id of new alert, it will be generated if it is None path -- path to script description -- description """ if alert_id: check_new_id_applicable(tree, "alert-id", alert_id) else: alert_id = find_unique_id(tree, "alert") alert = etree.SubElement(get_alerts(tree), "alert", id=alert_id, path=path) if description: alert.set("description", description) return alert
def add_recipient( reporter: ReportProcessor, tree, alert_id, recipient_value, recipient_id=None, description="", allow_same_value=False, ): """ Add recipient to alert with specified id. Returns added recipient element. Raises LibraryError if alert with specified recipient_id doesn't exist. Raises LibraryError if recipient already exists. reporter -- report processor tree -- cib etree node alert_id -- id of alert which should be parent of new recipient recipient_value -- value of recipient recipient_id -- id of new recipient, if None it will be generated description -- description of recipient allow_same_value -- if True unique recipient value is not required """ if recipient_id is None: recipient_id = find_unique_id(tree, "{0}-recipient".format(alert_id)) else: validate_id_does_not_exist(tree, recipient_id) alert = find_alert(get_alerts(tree), alert_id) ensure_recipient_value_is_unique(reporter, alert, recipient_value, allow_duplicity=allow_same_value) recipient = etree.SubElement(alert, "recipient", id=recipient_id, value=recipient_value) if description: recipient.attrib["description"] = description return recipient
def add_permissions_to_role(role_el, permission_info_list): """ Add permissions from permission_info_list to role_el. role_el -- acl_role element to which permissions should be added permission_info_list -- list of tuples, each contains (permission, scope_type, scope) """ area_type_attribute_map = { 'xpath': 'xpath', 'id': 'reference', } for permission, scope_type, scope in permission_info_list: perm = etree.SubElement(role_el, "acl_permission") perm.set( "id", find_unique_id( role_el, "{0}-{1}".format(role_el.get("id", "role"), permission))) perm.set("kind", permission) perm.set(area_type_attribute_map[scope_type], scope)
def add_permissions_to_role(tree, role_id, permission_info_list): """ tree etree node role_id value of atribute id, which exists in dom permission_info_list list of tuples, each contains (permission, scope_type, scope) """ __validate_permissions(tree, permission_info_list) area_type_attribute_map = { 'xpath': 'xpath', 'id': 'reference', } for permission, scope_type, scope in permission_info_list: perm = etree.SubElement(__find_role(tree, role_id), "acl_permission") perm.set( "id", find_unique_id(tree, "{0}-{1}".format(role_id, permission)) ) perm.set("kind", permission) perm.set(area_type_attribute_map[scope_type], scope)
def _append_level_element(tree, level, target_type, target_value, devices): level_el = etree.SubElement( tree, "fencing-level", index=str(level), devices=",".join(devices) ) if target_type == TARGET_TYPE_NODE: level_el.set("target", target_value) id_part = target_value elif target_type == TARGET_TYPE_REGEXP: level_el.set("target-pattern", target_value) id_part = target_value elif target_type == TARGET_TYPE_ATTRIBUTE: level_el.set("target-attribute", target_value[0]) level_el.set("target-value", target_value[1]) id_part = target_value[0] level_el.set( "id", find_unique_id(tree, sanitize_id("fl-{0}-{1}".format(id_part, level))) ) return level_el
def add_recipient( reporter, tree, alert_id, recipient_value, recipient_id=None, description="", allow_same_value=False ): """ Add recipient to alert with specified id. Returns added recipient element. Raises LibraryError if alert with specified recipient_id doesn't exist. Raises LibraryError if recipient already exists. reporter -- report processor tree -- cib etree node alert_id -- id of alert which should be parent of new recipient recipient_value -- value of recipient recipient_id -- id of new recipient, if None it will be generated description -- description of recipient allow_same_value -- if True unique recipient value is not required """ if recipient_id is None: recipient_id = find_unique_id(tree, "{0}-recipient".format(alert_id)) else: validate_id_does_not_exist(tree, recipient_id) alert = find_alert(get_alerts(tree), alert_id) ensure_recipient_value_is_unique( reporter, alert, recipient_value, allow_duplicity=allow_same_value ) recipient = etree.SubElement( alert, "recipient", id=recipient_id, value=recipient_value ) if description: recipient.set("description", description) return recipient
def add_permissions_to_role(role_el, permission_info_list): """ Add permissions from permission_info_list to role_el. role_el -- acl_role element to which permissions should be added permission_info_list -- list of tuples, each contains (permission, scope_type, scope) """ area_type_attribute_map = { 'xpath': 'xpath', 'id': 'reference', } for permission, scope_type, scope in permission_info_list: perm = etree.SubElement(role_el, "acl_permission") perm.set( "id", find_unique_id( role_el, "{0}-{1}".format(role_el.get("id", "role"), permission) ) ) perm.set("kind", permission) perm.set(area_type_attribute_map[scope_type], scope)
def test_add_first_available_suffix(self): self.fixture_add_primitive_with_id("myId") self.fixture_add_primitive_with_id("myId-1") self.fixture_add_primitive_with_id("myId-3") self.assertEqual("myId-2", lib.find_unique_id(self.cib.tree, "myId"))
def test_suffix_not_needed(self): self.fixture_add_primitive_with_id("myId-1") self.assertEqual("myId", lib.find_unique_id(self.cib.tree, "myId"))
def test_already_unique(self): self.fixture_add_primitive_with_id("myId") self.assertEqual("other", lib.find_unique_id(self.cib.tree, "other"))
def create_resource_id(resources_section, name, suffix): return find_unique_id(resources_section.getroottree(), "booth-{0}-{1}".format(name, suffix))
def _create_id(cib, ticket, resource_id, resource_role): return tools.find_unique_id( cib, "-".join(('ticket', ticket, resource_id)) +("-{0}".format(resource_role) if resource_role else "") )
def test_reserved_ids(self): self.fixture_add_primitive_with_id("myId-1") self.assertEqual( "myId-3", lib.find_unique_id(self.cib.tree, "myId", ["myId", "myId-2"]) )
def create_id(cib, type_prefix, resource_set_list): id = "pcs_" +type_prefix +"".join([ "_set_"+"_".join(id_set) for id_set in resource_set.extract_id_set_list(resource_set_list) ]) return find_unique_id(cib, id)
def create_id(cib, type_prefix, resource_set_list): id = "pcs_" + type_prefix + "".join([ "_set_" + "_".join(id_set) for id_set in resource_set.extract_id_set_list(resource_set_list) ]) return find_unique_id(cib, id)
def create_resource_id(resources_section, name, suffix): return find_unique_id( resources_section.getroottree(), "booth-{0}-{1}".format(name, suffix) )
def test_reserved_ids(self): self.fixture_add_primitive_with_id("myId-1") self.assertEqual( "myId-3", lib.find_unique_id(self.cib.tree, "myId", ["myId", "myId-2"]))
def _create_id(cib, ticket, resource_id, resource_role): return tools.find_unique_id( cib, "-".join(('ticket', ticket, resource_id, resource_role)) )