-use serde::Serialize;
use std::collections::HashSet;
+use serde::{Deserialize, Serialize};
+
use proxmox_http_error::HttpError;
-use crate::Config;
+use crate::{Config, Origin};
pub mod common;
#[cfg(feature = "gotify")]
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) {
+ if let Ok(origin) = Origin::deserialize(&entity_config.1["origin"]) {
+ // Built-ins are never actually removed, only reset to their default
+ // It is thus safe to do the reset if another entity depends
+ // on it
+ if origin == Origin::Builtin || origin == Origin::ModifiedBuiltin {
+ return Ok(());
+ }
+ }
+ } else {
+ http_bail!(NOT_FOUND, "entity '{entity}' does not exist");
+ }
+
let referrers = get_referrers(config, entity)?;
if !referrers.is_empty() {
}
}
-#[cfg(all(test, gotify, sendmail))]
+#[cfg(all(test, feature = "gotify", feature = "sendmail"))]
mod tests {
use super::*;
use crate::endpoints::gotify::{GotifyConfig, GotifyPrivateConfig};
use crate::endpoints::sendmail::SendmailConfig;
- use crate::filter::FilterConfig;
- use crate::group::GroupConfig;
+ use crate::matcher::MatcherConfig;
fn prepare_config() -> Result<Config, HttpError> {
- let mut config = super::test_helpers::empty_config();
+ let mut config = test_helpers::empty_config();
- matcher::add_matcher(
+ sendmail::add_endpoint(
&mut config,
- &MatcherConfig {
- name: "matcher".to_string(),
- target: Some(vec!["sendmail".to_string(), "gotify".to_string()])
- ..Default::default(),
+ &SendmailConfig {
+ name: "sendmail".to_string(),
+ mailto: Some(vec!["foo@example.com".to_string()]),
+ ..Default::default()
},
)?;
sendmail::add_endpoint(
&mut config,
&SendmailConfig {
- name: "sendmail".to_string(),
+ name: "builtin".to_string(),
mailto: Some(vec!["foo@example.com".to_string()]),
+ origin: Some(Origin::Builtin),
..Default::default()
},
)?;
},
)?;
+ matcher::add_matcher(
+ &mut config,
+ &MatcherConfig {
+ name: "matcher".to_string(),
+ target: Some(vec![
+ "sendmail".to_string(),
+ "gotify".to_string(),
+ "builtin".to_string(),
+ ]),
+ ..Default::default()
+ },
+ )?;
+
Ok(config)
}
HashSet::from([
"matcher".to_string(),
"sendmail".to_string(),
+ "builtin".to_string(),
"gotify".to_string()
])
);
}
#[test]
- fn test_ensure_unused() {
+ fn test_ensure_safe_to_delete() {
let config = prepare_config().unwrap();
- assert!(ensure_unused(&config, "gotify").is_err());
- assert!(ensure_unused(&config, "sendmail").is_err());
- assert!(ensure_unused(&config, "matcher").is_ok());
+ assert!(ensure_safe_to_delete(&config, "gotify").is_err());
+ assert!(ensure_safe_to_delete(&config, "sendmail").is_err());
+ assert!(ensure_safe_to_delete(&config, "matcher").is_ok());
+
+ // built-ins are always safe to delete, since there is no way to actually
+ // delete them... they will only be reset to their default settings and
+ // will thus continue to exist
+ assert!(ensure_safe_to_delete(&config, "builtin").is_ok());
}
#[test]
let config = prepare_config().unwrap();
assert!(ensure_unique(&config, "sendmail").is_err());
- assert!(ensure_unique(&config, "group").is_err());
+ assert!(ensure_unique(&config, "matcher").is_err());
assert!(ensure_unique(&config, "new").is_ok());
}
fn test_ensure_endpoints_exist() {
let config = prepare_config().unwrap();
- assert!(ensure_endpoints_exist(&config, &vec!["sendmail", "gotify"]).is_ok());
+ assert!(ensure_endpoints_exist(&config, &["sendmail", "gotify", "builtin"]).is_ok());
}
}