proxmox/proxmox-notify/src/api/common.rs
Lukas Wagner b421a7ca24 notify: replace filters and groups with matcher-based system
This shifts notification routing into the matcher-system. Every
notification has associated metadata (key-value fields, severity -
to be extended) that can be match with match directives in
notification matchers. Right now, there are 2 matching directives,
match-field and match-severity. The first one allows one to do a
regex match/exact match on a metadata field, the other one allows one
to match one or more severites.
Every matcher also allows 'target' directives, these decide which
target(s) will be notified if a matcher matches a notification.

Since routing now happens in matchers, the API for sending is
simplified, since we do not need to specify a target any more.

The API routes for filters and groups have been removed completely.
The parser for the configuration file will still accept filter/group
entries, but will delete them once the config is saved again. This is
needed to allow a smooth transition from the old system to the new
system, since the old system was already available on pvetest.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
2023-11-17 08:31:36 +01:00

55 lines
1.9 KiB
Rust

use proxmox_http_error::HttpError;
use super::http_err;
use crate::{Bus, Config, Notification};
/// Send a notification to a given target.
///
/// The caller is responsible for any needed permission checks.
/// Returns an `anyhow::Error` in case of an error.
pub fn send(config: &Config, notification: &Notification) -> Result<(), HttpError> {
let bus = Bus::from_config(config).map_err(|err| {
http_err!(
INTERNAL_SERVER_ERROR,
"Could not instantiate notification bus: {err}"
)
})?;
bus.send(notification);
Ok(())
}
/// Test target (group or single endpoint) identified by its `name`.
///
/// The caller is responsible for any needed permission checks.
/// Returns an `anyhow::Error` if sending via the endpoint failed.
pub fn test_target(config: &Config, endpoint: &str) -> Result<(), HttpError> {
let bus = Bus::from_config(config).map_err(|err| {
http_err!(
INTERNAL_SERVER_ERROR,
"Could not instantiate notification bus: {err}"
)
})?;
bus.test_target(endpoint).map_err(|err| match err {
crate::Error::TargetDoesNotExist(endpoint) => {
http_err!(NOT_FOUND, "endpoint '{endpoint}' does not exist")
}
_ => http_err!(INTERNAL_SERVER_ERROR, "Could not test target: {err}"),
})?;
Ok(())
}
/// Return all entities (targets, groups, filters) that are linked to the entity.
/// For instance, if a group 'grp1' contains the targets 'a', 'b' and 'c',
/// where grp1 has 'filter1' and 'a' has 'filter2' as filters, then
/// the result for 'grp1' would be [grp1, a, b, c, filter1, filter2].
/// The result will always contain the entity that was passed as a parameter.
/// If the entity does not exist, the result will only contain the entity.
pub fn get_referenced_entities(config: &Config, entity: &str) -> Result<Vec<String>, HttpError> {
let entities = super::get_referenced_entities(config, entity);
Ok(Vec::from_iter(entities))
}