In the 'delete'-handler targets, we check if a
target is still referenced by a matcher - if it is, we return an
error. For built-in targets, this is actually not necessary, since
'deleting' a built-in only resets it to its default settings - it will
continue to exist after that.
The user could easily trigger this if 'mail-to-root', which is
referenced by 'default-matcher' is modified and then reset to its
defaults: An error is shown, the built-in target is not reset.
This commit disables this check if it is a built-in target.
Renamed the helper 'ensure_unused' to 'ensure_safe_to_delete' in the
process.
Also fixed the tests in api::test - they were never executed due to a
faulty #[cfg] directive.
Signed-off-by: Lukas Wagner <[email protected]>
---
proxmox-notify/src/api/gotify.rs | 2 +-
proxmox-notify/src/api/mod.rs | 74 ++++++++++++++++++++++--------
proxmox-notify/src/api/sendmail.rs | 2 +-
proxmox-notify/src/api/smtp.rs | 2 +-
4 files changed, 59 insertions(+), 21 deletions(-)
diff --git a/proxmox-notify/src/api/gotify.rs b/proxmox-notify/src/api/gotify.rs
index 98ff255..a93a024 100644
--- a/proxmox-notify/src/api/gotify.rs
+++ b/proxmox-notify/src/api/gotify.rs
@@ -136,7 +136,7 @@ pub fn update_endpoint(
pub fn delete_gotify_endpoint(config: &mut Config, name: &str) -> Result<(),
HttpError> {
// Check if the endpoint exists
let _ = get_endpoint(config, name)?;
- super::ensure_unused(config, name)?;
+ super::ensure_safe_to_delete(config, name)?;
remove_private_config_entry(config, name)?;
config.config.sections.remove(name);
diff --git a/proxmox-notify/src/api/mod.rs b/proxmox-notify/src/api/mod.rs
index 762d448..919dcb9 100644
--- a/proxmox-notify/src/api/mod.rs
+++ b/proxmox-notify/src/api/mod.rs
@@ -3,7 +3,7 @@ use std::collections::HashSet;
use proxmox_http_error::HttpError;
-use crate::Config;
+use crate::{Config, Origin};
pub mod common;
#[cfg(feature = "gotify")]
@@ -111,7 +111,26 @@ fn get_referrers(config: &Config, entity: &str) ->
Result<HashSet<String>, HttpE
Ok(referrers)
}
-fn ensure_unused(config: &Config, entity: &str) -> Result<(), HttpError> {
+fn ensure_safe_to_delete(config: &Config, entity: &str) -> Result<(),
HttpError> {
+ if let Some(entity_config) = config.config.sections.get(entity) {
+ let origin: Option<Origin> = entity_config
+ .1
+ .as_object()
+ .and_then(|obj| obj.get("origin"))
+ .cloned()
+ .and_then(|origin_value|
serde_json::from_value(origin_value).ok());