[pve-devel] [PATCH v2 proxmox 07/42] notify: api: add API for sending notifications/testing endpoints
Lukas Wagner
l.wagner at proxmox.com
Wed May 24 15:56:14 CEST 2023
Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
proxmox-notify/src/api/common.rs | 46 ++++++++++++++++++++++++++++++++
proxmox-notify/src/api/mod.rs | 2 ++
2 files changed, 48 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..2465279e
--- /dev/null
+++ b/proxmox-notify/src/api/common.rs
@@ -0,0 +1,46 @@
+use crate::api::ApiError;
+use crate::{Bus, Config, Notification};
+
+/// Send a notifcation to a given channel.
+///
+/// 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).map_err(|err| match err {
+ crate::Error::ChannelDoesNotExist(channel) => {
+ ApiError::not_found(format!("channel '{channel}' does not exist"), None)
+ }
+ _ => ApiError::internal_server_error("Could not send notification", Some(Box::new(err))),
+ })?;
+
+ Ok(())
+}
+
+/// Test an 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_endpoint(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_endpoint(endpoint).map_err(|err| match err {
+ crate::Error::EndpointDoesNotExist(endpoint) => {
+ ApiError::not_found(format!("endpoint '{endpoint}' does not exist"), None)
+ }
+ _ => ApiError::internal_server_error("Could not test endpoint", Some(Box::new(err))),
+ })?;
+
+ Ok(())
+}
diff --git a/proxmox-notify/src/api/mod.rs b/proxmox-notify/src/api/mod.rs
index db8623ea..839a75cc 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.30.2
More information about the pve-devel
mailing list