[pbs-devel] [PATCH proxmox-backup v2 18/43] api: add endpoints for querying known notification values/fields

Lukas Wagner l.wagner at proxmox.com
Wed Apr 17 16:34:49 CEST 2024


These endpoints require Sys.Audit/Sys.Modify permissions on
/system/notifications.

Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 src/api2/config/notifications/mod.rs | 195 ++++++++++++++++++++++++++-
 1 file changed, 194 insertions(+), 1 deletion(-)

diff --git a/src/api2/config/notifications/mod.rs b/src/api2/config/notifications/mod.rs
index 9c4a0cd9..05a51f24 100644
--- a/src/api2/config/notifications/mod.rs
+++ b/src/api2/config/notifications/mod.rs
@@ -1,7 +1,22 @@
-use proxmox_router::list_subdirs_api_method;
+use anyhow::Error;
+use serde::Serialize;
+use serde_json::Value;
+use std::cmp::Ordering;
+
+use proxmox_router::{list_subdirs_api_method, Permission, RpcEnvironment};
 use proxmox_router::{Router, SubdirMap};
+use proxmox_schema::api;
 use proxmox_sortable_macro::sortable;
 
+use pbs_api_types::PRIV_SYS_AUDIT;
+
+use crate::api2::admin::prune::list_prune_jobs;
+use crate::api2::admin::sync::list_sync_jobs;
+use crate::api2::admin::verify::list_verification_jobs;
+use crate::api2::config::datastore::list_datastores;
+use crate::api2::config::media_pool::list_pools;
+use crate::api2::tape::backup::list_tape_backup_jobs;
+
 mod gotify;
 mod matchers;
 mod sendmail;
@@ -11,6 +26,8 @@ mod targets;
 #[sortable]
 const SUBDIRS: SubdirMap = &sorted!([
     ("endpoints", &ENDPOINT_ROUTER),
+    ("fields", &FIELD_ROUTER),
+    ("values", &VALUE_ROUTER),
     ("targets", &targets::ROUTER),
     ("matchers", &matchers::ROUTER),
 ]);
@@ -29,3 +46,179 @@ const ENDPOINT_SUBDIRS: SubdirMap = &sorted!([
 const ENDPOINT_ROUTER: Router = Router::new()
     .get(&list_subdirs_api_method!(ENDPOINT_SUBDIRS))
     .subdirs(ENDPOINT_SUBDIRS);
+
+const FIELD_ROUTER: Router = Router::new().get(&API_METHOD_GET_FIELDS);
+const VALUE_ROUTER: Router = Router::new().get(&API_METHOD_GET_VALUES);
+
+#[api]
+#[derive(Serialize)]
+/// A matchable field.
+pub struct MatchableField {
+    /// Name of the field
+    name: String,
+}
+
+#[api]
+#[derive(Serialize)]
+/// A matchable metadata field value.
+pub struct MatchableValue {
+    /// Field this value belongs to.
+    field: String,
+    /// Notification metadata value known by the system.
+    value: String,
+    /// Additional comment for this value.
+    comment: Option<String>,
+    /// Set if `value` was generated by the system and can safely be used
+    /// as a base for translations.
+    internal: Option<bool>,
+}
+
+#[api(
+    protected: false,
+    input: {
+        properties: {},
+    },
+    returns: {
+        description: "List of known metadata fields.",
+        type: Array,
+        items: { type: MatchableField },
+    },
+    access: {
+        permission: &Permission::Privilege(&["system", "notifications"], PRIV_SYS_AUDIT, false),
+    },
+)]
+/// Get all known metadata fields.
+pub fn get_fields() -> Result<Vec<MatchableField>, Error> {
+    let fields = [
+        "datastore",
+        "hostname",
+        "media-pool",
+        "prune-job",
+        "sync-job",
+        "type",
+        "tape-job",
+        "verification-job",
+    ]
+    .into_iter()
+    .map(Into::into)
+    .map(|name| MatchableField { name })
+    .collect();
+
+    Ok(fields)
+}
+
+#[api(
+    protected: false,
+    input: {
+        properties: {},
+    },
+    returns: {
+        description: "List of known metadata field values.",
+        type: Array,
+        items: { type: MatchableValue },
+    },
+    access: {
+        permission: &Permission::Privilege(&["system", "notifications"], PRIV_SYS_AUDIT, false),
+    },
+)]
+/// List all known, matchable metadata field values.
+pub fn get_values(
+    param: Value,
+    rpcenv: &mut dyn RpcEnvironment,
+) -> Result<Vec<MatchableValue>, Error> {
+    let mut values = Vec::new();
+
+    let datastores = list_datastores(param.clone(), rpcenv)?;
+
+    for datastore in datastores {
+        values.push(MatchableValue {
+            field: "datastore".into(),
+            value: datastore.name.clone(),
+            comment: datastore.comment.clone(),
+            internal: Some(false),
+        });
+    }
+
+    let pools = list_pools(rpcenv)?;
+    for pool in pools {
+        values.push(MatchableValue {
+            field: "media-pool".into(),
+            value: pool.name.clone(),
+            comment: None,
+            internal: Some(false),
+        });
+    }
+
+    let tape_backup_jobs = list_tape_backup_jobs(param.clone(), rpcenv)?;
+    for job in tape_backup_jobs {
+        values.push(MatchableValue {
+            field: "tape-job".into(),
+            value: job.config.id,
+            comment: job.config.comment,
+            internal: Some(false),
+        });
+    }
+
+    let prune_jobs = list_prune_jobs(None, param.clone(), rpcenv)?;
+    for job in prune_jobs {
+        values.push(MatchableValue {
+            field: "prune-job".into(),
+            value: job.config.id,
+            comment: job.config.comment,
+            internal: Some(false),
+        });
+    }
+
+    let sync_jobs = list_sync_jobs(None, param.clone(), rpcenv)?;
+    for job in sync_jobs {
+        values.push(MatchableValue {
+            field: "sync-job".into(),
+            value: job.config.id,
+            comment: job.config.comment,
+            internal: Some(false),
+        });
+    }
+
+    let verify_jobs = list_verification_jobs(None, param.clone(), rpcenv)?;
+    for job in verify_jobs {
+        values.push(MatchableValue {
+            field: "verification-job".into(),
+            value: job.config.id,
+            comment: job.config.comment,
+            internal: Some(false),
+        });
+    }
+
+    values.push(MatchableValue {
+        field: "hostname".into(),
+        value: proxmox_sys::nodename().into(),
+        comment: None,
+        internal: Some(false),
+    });
+
+    for ty in [
+        "acme",
+        "gc",
+        "package-updates",
+        "prune",
+        "sync",
+        "system-mail",
+        "tape-backup",
+        "tape-load",
+        "verify",
+    ] {
+        values.push(MatchableValue {
+            field: "type".into(),
+            value: ty.into(),
+            comment: None,
+            internal: Some(true),
+        });
+    }
+
+    values.sort_by(|a, b| match a.field.cmp(&b.field) {
+        Ordering::Equal => a.value.cmp(&b.value),
+        ord => ord,
+    });
+
+    Ok(values)
+}
-- 
2.39.2





More information about the pbs-devel mailing list