[pve-devel] [PATCH v3 proxmox 04/66] notify: api: add API for sending notifications/testing endpoints

Lukas Wagner l.wagner at proxmox.com
Mon Jul 17 16:59:49 CEST 2023


Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 proxmox-notify/src/api/common.rs | 44 ++++++++++++++++++++++++++++++++
 proxmox-notify/src/api/mod.rs    |  2 ++
 2 files changed, 46 insertions(+)
 create mode 100644 proxmox-notify/src/api/common.rs

diff --git a/proxmox-notify/src/api/common.rs b/proxmox-notify/src/api/common.rs
new file mode 100644
index 00000000..518caa8f
--- /dev/null
+++ b/proxmox-notify/src/api/common.rs
@@ -0,0 +1,44 @@
+use crate::api::ApiError;
+use crate::{Bus, Config, Notification};
+
+/// Send a notification to a given target.
+///
+/// The caller is responsible for any needed permission checks.
+/// Returns an `ApiError` in case of an error.
+pub fn send(config: &Config, channel: &str, notification: &Notification) -> Result<(), ApiError> {
+    let bus = Bus::from_config(config).map_err(|err| {
+        ApiError::internal_server_error(
+            "Could not instantiate notification bus",
+            Some(Box::new(err)),
+        )
+    })?;
+
+    bus.send(channel, notification);
+
+    Ok(())
+}
+
+/// Test target (group or single endpoint) identified by its `name`.
+///
+/// The caller is responsible for any needed permission checks.
+/// Returns an `ApiError` if sending via the endpoint failed.
+pub fn test_target(config: &Config, endpoint: &str) -> Result<(), ApiError> {
+    let bus = Bus::from_config(config).map_err(|err| {
+        ApiError::internal_server_error(
+            "Could not instantiate notification bus",
+            Some(Box::new(err)),
+        )
+    })?;
+
+    bus.test_target(endpoint).map_err(|err| match err {
+        crate::Error::TargetDoesNotExist(endpoint) => {
+            ApiError::not_found(format!("endpoint '{endpoint}' does not exist"), None)
+        }
+        _ => ApiError::internal_server_error(
+            format!("Could not test target: {err}"),
+            Some(Box::new(err)),
+        ),
+    })?;
+
+    Ok(())
+}
diff --git a/proxmox-notify/src/api/mod.rs b/proxmox-notify/src/api/mod.rs
index be596b93..db9ad1ca 100644
--- a/proxmox-notify/src/api/mod.rs
+++ b/proxmox-notify/src/api/mod.rs
@@ -4,6 +4,8 @@ use std::fmt::Display;
 use crate::Config;
 use serde::Serialize;
 
+pub mod common;
+
 #[derive(Debug, Serialize)]
 pub struct ApiError {
     /// HTTP Error code
-- 
2.39.2






More information about the pve-devel mailing list