Exemple #1
0
def get_all_alerts(tree):
    """
    Returns list of all alerts specified in tree. Format:
    [
        {
            "id": <id of alert>,
            "path": <path to script>,
            "description": <alert description>,
            "instance_attributes": <list of nvpairs>,
            "meta_attributes": <list of nvpairs>,
            "recipients_list": <list of alert's recipients>
        }
    ]

    tree -- cib etree node
    """
    alert_list = []
    for alert in get_alerts(tree).findall("./alert"):
        alert_list.append({
            "id": alert.get("id"),
            "path": alert.get("path"),
            "description": alert.get("description", ""),
            "instance_attributes": get_nvset(
                get_sub_element(alert, "instance_attributes")
            ),
            "meta_attributes": get_nvset(
                get_sub_element(alert, "meta_attributes")
            ),
            "recipient_list": get_all_recipients(alert)
        })
    return alert_list
Exemple #2
0
def update_recipient(
    reporter,
    tree,
    recipient_id,
    recipient_value=None,
    description=None,
    allow_same_value=False
):
    """
    Update specified recipient. Returns updated recipient element.
    Raises LibraryError if recipient doesn't exist.

    reporter -- report processor
    tree -- cib etree node
    recipient_id -- id of recipient to be updated
    recipient_value -- recipient value, stay unchanged if None
    description -- description, if empty it will be removed, stay unchanged
        if None
    allow_same_value -- if True unique recipient value is not required
    """
    recipient = find_recipient(get_alerts(tree), recipient_id)
    if recipient_value is not None:
        ensure_recipient_value_is_unique(
            reporter,
            recipient.getparent(),
            recipient_value,
            recipient_id=recipient_id,
            allow_duplicity=allow_same_value
        )
        recipient.set("value", recipient_value)
    _update_optional_attribute(recipient, "description", description)
    return recipient
Exemple #3
0
def remove_recipient(tree, recipient_id):
    """
    Remove specified recipient.
    Raises LibraryError if recipient doesn't exist.

    tree -- cib etree node
    recipient_id -- id of recipient to be removed
    """
    recipient = find_recipient(get_alerts(tree), recipient_id)
    recipient.getparent().remove(recipient)
Exemple #4
0
def remove_alert(tree, alert_id):
    """
    Remove alert with specified id.
    Raises LibraryError if alert with specified id doesn't exist.

    tree -- cib etree node
    alert_id -- id of alert which should be removed
    """
    alert = find_alert(get_alerts(tree), alert_id)
    alert.getparent().remove(alert)
Exemple #5
0
def remove_recipient(tree, recipient_id):
    """
    Remove specified recipient.
    Raises LibraryError if recipient doesn't exist.

    tree -- cib etree node
    recipient_id -- id of recipient to be removed
    """
    recipient = find_recipient(get_alerts(tree), recipient_id)
    recipient.getparent().remove(recipient)
Exemple #6
0
def remove_alert(tree, alert_id):
    """
    Remove alert with specified id.
    Raises LibraryError if alert with specified id doesn't exist.

    tree -- cib etree node
    alert_id -- id of alert which should be removed
    """
    alert = find_alert(get_alerts(tree), alert_id)
    alert.getparent().remove(alert)
Exemple #7
0
def get_alert_by_id(tree, alert_id):
    """
    Returns alert element with specified id.
    Raises LibraryError if alert with specified id doesn't exist.

    tree -- cib etree node
    alert_id -- id of alert
    """
    alert = get_alerts(tree).find("./alert[@id='{0}']".format(alert_id))
    if alert is None:
        raise LibraryError(reports.cib_alert_not_found(alert_id))
    return alert
Exemple #8
0
def get_alert_by_id(tree, alert_id):
    """
    Returns alert element with specified id.
    Raises LibraryError if alert with specified id doesn't exist.

    tree -- cib etree node
    alert_id -- id of alert
    """
    alert = get_alerts(tree).find("./alert[@id='{0}']".format(alert_id))
    if alert is None:
        raise LibraryError(reports.cib_alert_not_found(alert_id))
    return alert
Exemple #9
0
def get_recipient_by_id(tree, recipient_id):
    """
    Returns recipient element with value recipient_value which belong to
    specified alert.
    Raises LibraryError if recipient doesn't exist.

    tree -- cib etree node
    recipient_id -- id of recipient
    """
    recipient = get_alerts(tree).find(
        "./alert/recipient[@id='{0}']".format(recipient_id))
    if recipient is None:
        raise LibraryError(reports.id_not_found(recipient_id, "Recipient"))
    return recipient
Exemple #10
0
def get_recipient_by_id(tree, recipient_id):
    """
    Returns recipient element with value recipient_value which belong to
    specified alert.
    Raises LibraryError if recipient doesn't exist.

    tree -- cib etree node
    recipient_id -- id of recipient
    """
    recipient = get_alerts(tree).find(
        "./alert/recipient[@id='{0}']".format(recipient_id)
    )
    if recipient is None:
        raise LibraryError(reports.id_not_found(recipient_id, "Recipient"))
    return recipient
Exemple #11
0
def update_alert(tree, alert_id, path, description=None):
    """
    Update existing alert. Return updated alert element.
    Raises LibraryError if alert with specified id doesn't exist.

    tree -- cib etree node
    alert_id -- id of alert to be updated
    path -- new value of path, stay unchanged if None
    description -- new value of description, stay unchanged if None, remove
        if empty
    """
    alert = find_alert(get_alerts(tree), alert_id)
    if path:
        alert.set("path", path)
    _update_optional_attribute(alert, "description", description)
    return alert
Exemple #12
0
def update_alert(tree, alert_id, path, description=None):
    """
    Update existing alert. Return updated alert element.
    Raises LibraryError if alert with specified id doesn't exist.

    tree -- cib etree node
    alert_id -- id of alert to be updated
    path -- new value of path, stay unchanged if None
    description -- new value of description, stay unchanged if None, remove
        if empty
    """
    alert = find_alert(get_alerts(tree), alert_id)
    if path:
        alert.set("path", path)
    _update_optional_attribute(alert, "description", description)
    return alert
Exemple #13
0
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
Exemple #14
0
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
Exemple #15
0
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
Exemple #16
0
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