use serde_json::Value as JSONValue;
use std::sync::Mutex;
+ use proxmox_http_error::HttpError;
use proxmox_notify::endpoints::gotify::{
DeleteableGotifyProperty, GotifyConfig, GotifyConfigUpdater, GotifyPrivateConfig,
GotifyPrivateConfigUpdater,
DeleteableFilterProperty, FilterConfig, FilterConfigUpdater, FilterModeOperator,
};
use proxmox_notify::group::{DeleteableGroupProperty, GroupConfig, GroupConfigUpdater};
- use proxmox_notify::{api, api::ApiError, Config, Notification, Severity};
+ use proxmox_notify::{api, Config, Notification, Severity};
pub struct NotificationConfig {
config: Mutex<Config>,
title: String,
body: String,
properties: Option<JSONValue>,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let config = this.config.lock().unwrap();
let notification = Notification {
fn test_target(
#[try_from_ref] this: &NotificationConfig,
target: &str,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let config = this.config.lock().unwrap();
api::common::test_target(&config, target)
}
#[export(serialize_error)]
- fn get_groups(#[try_from_ref] this: &NotificationConfig) -> Result<Vec<GroupConfig>, ApiError> {
+ fn get_groups(
+ #[try_from_ref] this: &NotificationConfig,
+ ) -> Result<Vec<GroupConfig>, HttpError> {
let config = this.config.lock().unwrap();
api::group::get_groups(&config)
}
fn get_group(
#[try_from_ref] this: &NotificationConfig,
id: &str,
- ) -> Result<GroupConfig, ApiError> {
+ ) -> Result<GroupConfig, HttpError> {
let config = this.config.lock().unwrap();
api::group::get_group(&config, id)
}
endpoints: Vec<String>,
comment: Option<String>,
filter: Option<String>,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let mut config = this.config.lock().unwrap();
api::group::add_group(
&mut config,
filter: Option<String>,
delete: Option<Vec<DeleteableGroupProperty>>,
digest: Option<&str>,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let mut config = this.config.lock().unwrap();
- let digest = digest.map(hex::decode).transpose().map_err(|e| {
- ApiError::internal_server_error(format!("invalid digest: {e}"), Some(Box::new(e)))
- })?;
+ let digest = decode_digest(digest)?;
api::group::update_group(
&mut config,
}
#[export(serialize_error)]
- fn delete_group(#[try_from_ref] this: &NotificationConfig, name: &str) -> Result<(), ApiError> {
+ fn delete_group(
+ #[try_from_ref] this: &NotificationConfig,
+ name: &str,
+ ) -> Result<(), HttpError> {
let mut config = this.config.lock().unwrap();
api::group::delete_group(&mut config, name)
}
#[export(serialize_error)]
fn get_sendmail_endpoints(
#[try_from_ref] this: &NotificationConfig,
- ) -> Result<Vec<SendmailConfig>, ApiError> {
+ ) -> Result<Vec<SendmailConfig>, HttpError> {
let config = this.config.lock().unwrap();
api::sendmail::get_endpoints(&config)
}
fn get_sendmail_endpoint(
#[try_from_ref] this: &NotificationConfig,
id: &str,
- ) -> Result<SendmailConfig, ApiError> {
+ ) -> Result<SendmailConfig, HttpError> {
let config = this.config.lock().unwrap();
api::sendmail::get_endpoint(&config, id)
}
author: Option<String>,
comment: Option<String>,
filter: Option<String>,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let mut config = this.config.lock().unwrap();
api::sendmail::add_endpoint(
filter: Option<String>,
delete: Option<Vec<DeleteableSendmailProperty>>,
digest: Option<&str>,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let mut config = this.config.lock().unwrap();
- let digest = digest.map(hex::decode).transpose().map_err(|e| {
- ApiError::internal_server_error(format!("invalid digest: {e}"), Some(Box::new(e)))
- })?;
+ let digest = decode_digest(digest)?;
api::sendmail::update_endpoint(
&mut config,
fn delete_sendmail_endpoint(
#[try_from_ref] this: &NotificationConfig,
name: &str,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let mut config = this.config.lock().unwrap();
api::sendmail::delete_endpoint(&mut config, name)
}
#[export(serialize_error)]
fn get_gotify_endpoints(
#[try_from_ref] this: &NotificationConfig,
- ) -> Result<Vec<GotifyConfig>, ApiError> {
+ ) -> Result<Vec<GotifyConfig>, HttpError> {
let config = this.config.lock().unwrap();
api::gotify::get_endpoints(&config)
}
fn get_gotify_endpoint(
#[try_from_ref] this: &NotificationConfig,
id: &str,
- ) -> Result<GotifyConfig, ApiError> {
+ ) -> Result<GotifyConfig, HttpError> {
let config = this.config.lock().unwrap();
api::gotify::get_endpoint(&config, id)
}
token: String,
comment: Option<String>,
filter: Option<String>,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let mut config = this.config.lock().unwrap();
api::gotify::add_endpoint(
&mut config,
filter: Option<String>,
delete: Option<Vec<DeleteableGotifyProperty>>,
digest: Option<&str>,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let mut config = this.config.lock().unwrap();
- let digest = digest.map(hex::decode).transpose().map_err(|e| {
- ApiError::internal_server_error(format!("invalid digest: {e}"), Some(Box::new(e)))
- })?;
+ let digest = decode_digest(digest)?;
api::gotify::update_endpoint(
&mut config,
fn delete_gotify_endpoint(
#[try_from_ref] this: &NotificationConfig,
name: &str,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let mut config = this.config.lock().unwrap();
api::gotify::delete_gotify_endpoint(&mut config, name)
}
#[export(serialize_error)]
fn get_filters(
#[try_from_ref] this: &NotificationConfig,
- ) -> Result<Vec<FilterConfig>, ApiError> {
+ ) -> Result<Vec<FilterConfig>, HttpError> {
let config = this.config.lock().unwrap();
api::filter::get_filters(&config)
}
fn get_filter(
#[try_from_ref] this: &NotificationConfig,
id: &str,
- ) -> Result<FilterConfig, ApiError> {
+ ) -> Result<FilterConfig, HttpError> {
let config = this.config.lock().unwrap();
api::filter::get_filter(&config, id)
}
mode: Option<FilterModeOperator>,
invert_match: Option<bool>,
comment: Option<String>,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let mut config = this.config.lock().unwrap();
api::filter::add_filter(
&mut config,
comment: Option<String>,
delete: Option<Vec<DeleteableFilterProperty>>,
digest: Option<&str>,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let mut config = this.config.lock().unwrap();
- let digest = digest.map(hex::decode).transpose().map_err(|e| {
- ApiError::internal_server_error(format!("invalid digest: {e}"), Some(Box::new(e)))
- })?;
+ let digest = decode_digest(digest)?;
api::filter::update_filter(
&mut config,
fn delete_filter(
#[try_from_ref] this: &NotificationConfig,
name: &str,
- ) -> Result<(), ApiError> {
+ ) -> Result<(), HttpError> {
let mut config = this.config.lock().unwrap();
api::filter::delete_filter(&mut config, name)
}
fn get_referenced_entities(
#[try_from_ref] this: &NotificationConfig,
name: &str,
- ) -> Result<Vec<String>, ApiError> {
+ ) -> Result<Vec<String>, HttpError> {
let config = this.config.lock().unwrap();
api::common::get_referenced_entities(&config, name)
}
+
+ fn decode_digest(digest: Option<&str>) -> Result<Option<Vec<u8>>, HttpError> {
+ digest
+ .map(hex::decode)
+ .transpose()
+ .map_err(|e| api::http_err!(BAD_REQUEST, "invalid digest: {e}"))
+ }
}