[pbs-devel] [PATCH proxmox-backup v4 12/43] api: add endpoints for querying/testing notification targets
Lukas Wagner
l.wagner at proxmox.com
Mon Apr 22 14:38:10 CEST 2024
These endpoints require Sys.Audit permissions on
/system/notifications.
Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
Tested-by: Gabriel Goller <g.goller at proxmox.com>
Reviewed-by: Gabriel Goller <g.goller at proxmox.com>
---
src/api2/config/mod.rs | 2 +
src/api2/config/notifications/mod.rs | 12 +++++
src/api2/config/notifications/targets.rs | 63 ++++++++++++++++++++++++
3 files changed, 77 insertions(+)
create mode 100644 src/api2/config/notifications/mod.rs
create mode 100644 src/api2/config/notifications/targets.rs
diff --git a/src/api2/config/mod.rs b/src/api2/config/mod.rs
index 6cfeaea1..15dc5db9 100644
--- a/src/api2/config/mod.rs
+++ b/src/api2/config/mod.rs
@@ -11,6 +11,7 @@ pub mod datastore;
pub mod drive;
pub mod media_pool;
pub mod metrics;
+pub mod notifications;
pub mod prune;
pub mod remote;
pub mod sync;
@@ -28,6 +29,7 @@ const SUBDIRS: SubdirMap = &sorted!([
("drive", &drive::ROUTER),
("media-pool", &media_pool::ROUTER),
("metrics", &metrics::ROUTER),
+ ("notifications", ¬ifications::ROUTER),
("prune", &prune::ROUTER),
("remote", &remote::ROUTER),
("sync", &sync::ROUTER),
diff --git a/src/api2/config/notifications/mod.rs b/src/api2/config/notifications/mod.rs
new file mode 100644
index 00000000..f75e3910
--- /dev/null
+++ b/src/api2/config/notifications/mod.rs
@@ -0,0 +1,12 @@
+use proxmox_router::list_subdirs_api_method;
+use proxmox_router::{Router, SubdirMap};
+use proxmox_sortable_macro::sortable;
+
+mod targets;
+
+#[sortable]
+const SUBDIRS: SubdirMap = &sorted!([("targets", &targets::ROUTER),]);
+
+pub const ROUTER: Router = Router::new()
+ .get(&list_subdirs_api_method!(SUBDIRS))
+ .subdirs(SUBDIRS);
diff --git a/src/api2/config/notifications/targets.rs b/src/api2/config/notifications/targets.rs
new file mode 100644
index 00000000..6c5017b0
--- /dev/null
+++ b/src/api2/config/notifications/targets.rs
@@ -0,0 +1,63 @@
+use anyhow::Error;
+use serde_json::Value;
+
+use proxmox_notify::api::Target;
+use proxmox_notify::schema::ENTITY_NAME_SCHEMA;
+use proxmox_router::{list_subdirs_api_method, Permission, Router, RpcEnvironment, SubdirMap};
+use proxmox_schema::api;
+use proxmox_sortable_macro::sortable;
+
+use pbs_api_types::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
+
+#[api(
+ protected: true,
+ input: {
+ properties: {},
+ },
+ returns: {
+ description: "List of all entities which can be used as notification targets.",
+ type: Array,
+ items: { type: Target },
+ },
+ access: {
+ permission: &Permission::Privilege(&["system", "notifications"], PRIV_SYS_AUDIT, false),
+ },
+)]
+/// List all notification targets.
+pub fn list_targets(_param: Value, _rpcenv: &mut dyn RpcEnvironment) -> Result<Vec<Target>, Error> {
+ let config = pbs_config::notifications::config()?;
+ let targets = proxmox_notify::api::get_targets(&config)?;
+
+ Ok(targets)
+}
+
+#[api(
+ protected: true,
+ input: {
+ properties: {
+ name: {
+ schema: ENTITY_NAME_SCHEMA,
+ },
+ }
+ },
+ access: {
+ permission: &Permission::Privilege(&["system", "notifications"], PRIV_SYS_MODIFY, false),
+ },
+)]
+/// Test a given notification target.
+pub fn test_target(name: String, _rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
+ let config = pbs_config::notifications::config()?;
+ proxmox_notify::api::common::test_target(&config, &name)?;
+ Ok(())
+}
+
+#[sortable]
+const SUBDIRS: SubdirMap = &sorted!([("test", &TEST_ROUTER),]);
+const TEST_ROUTER: Router = Router::new().post(&API_METHOD_TEST_TARGET);
+const ITEM_ROUTER: Router = Router::new()
+ .get(&list_subdirs_api_method!(SUBDIRS))
+ .subdirs(SUBDIRS);
+
+pub const ROUTER: Router = Router::new()
+ .get(&API_METHOD_LIST_TARGETS)
+ .match_all("name", &ITEM_ROUTER);
--
2.39.2
More information about the pbs-devel
mailing list