[pbs-devel] [PATCH proxmox-backup 11/33] api: add endpoints for querying known notification values/fields

Lukas Wagner l.wagner at proxmox.com
Fri Apr 12 12:06:09 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 | 146 ++++++++++++++++++++++++++-
 1 file changed, 145 insertions(+), 1 deletion(-)

diff --git a/src/api2/config/notifications/mod.rs b/src/api2/config/notifications/mod.rs
index 9c4a0cd9..2c8b780e 100644
--- a/src/api2/config/notifications/mod.rs
+++ b/src/api2/config/notifications/mod.rs
@@ -1,7 +1,16 @@
-use proxmox_router::list_subdirs_api_method;
+use anyhow::Error;
+use serde::Serialize;
+use serde_json::Value;
+
+use proxmox_router::{list_subdirs_api_method, Permission, RpcEnvironment};
 use proxmox_router::{Router, SubdirMap};
+use proxmox_schema::api;
 use proxmox_sortable_macro::sortable;
 
+use crate::api2::config::datastore::list_datastores;
+use crate::api2::config::media_pool::list_pools;
+use pbs_api_types::PRIV_SYS_AUDIT;
+
 mod gotify;
 mod matchers;
 mod sendmail;
@@ -11,6 +20,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 +40,136 @@ 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(
+    _param: Value,
+    _rpcenv: &mut dyn RpcEnvironment,
+) -> Result<Vec<MatchableField>, Error> {
+    let fields = vec![
+        MatchableField {
+            name: "type".into(),
+        },
+        MatchableField {
+            name: "hostname".into(),
+        },
+        MatchableField {
+            name: "datastore".into(),
+        },
+        MatchableField {
+            name: "media-pool".into(),
+        },
+    ];
+
+    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, 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),
+        });
+    }
+
+    values.push(MatchableValue {
+        field: "hostname".into(),
+        value: proxmox_sys::nodename().into(),
+        comment: None,
+        internal: Some(false),
+    });
+
+    for ty in [
+        "prune",
+        "gc",
+        "sync",
+        "verify",
+        "tape-backup",
+        "tape-load",
+        "package-updates",
+        "acme",
+    ] {
+        values.push(MatchableValue {
+            field: "type".into(),
+            value: ty.into(),
+            comment: None,
+            internal: Some(true),
+        });
+    }
+
+    Ok(values)
+}
-- 
2.39.2





More information about the pbs-devel mailing list