]> git.proxmox.com Git - proxmox.git/blobdiff - proxmox-notify/src/api/sendmail.rs
notify: api: allow resetting built-in targets if used by a matcher
[proxmox.git] / proxmox-notify / src / api / sendmail.rs
index ac8737c0bb774f36d1cf229d989324e608fa5a95..e9115051a9a0a1147b3278a23d97ef6a1d801f5f 100644 (file)
@@ -35,17 +35,11 @@ pub fn get_endpoint(config: &Config, name: &str) -> Result<SendmailConfig, HttpE
 /// The caller also responsible for locking the configuration files.
 /// Returns a `HttpError` if:
 ///   - an entity with the same name already exists (`400 Bad request`)
-///   - a referenced filter does not exist (`400 Bad request`)
 ///   - the configuration could not be saved (`500 Internal server error`)
 ///   - mailto *and* mailto_user are both set to `None`
 pub fn add_endpoint(config: &mut Config, endpoint: &SendmailConfig) -> Result<(), HttpError> {
     super::ensure_unique(config, &endpoint.name)?;
 
-    if let Some(filter) = &endpoint.filter {
-        // Check if filter exists
-        super::filter::get_filter(config, filter)?;
-    }
-
     if endpoint.mailto.is_none() && endpoint.mailto_user.is_none() {
         http_bail!(
             BAD_REQUEST,
@@ -70,7 +64,6 @@ pub fn add_endpoint(config: &mut Config, endpoint: &SendmailConfig) -> Result<()
 /// The caller is responsible for any needed permission checks.
 /// The caller also responsible for locking the configuration files.
 /// Returns a `HttpError` if:
-///   - a referenced filter does not exist (`400 Bad request`)
 ///   - the configuration could not be saved (`500 Internal server error`)
 ///   - mailto *and* mailto_user are both set to `None`
 pub fn update_endpoint(
@@ -90,9 +83,9 @@ pub fn update_endpoint(
                 DeleteableSendmailProperty::FromAddress => endpoint.from_address = None,
                 DeleteableSendmailProperty::Author => endpoint.author = None,
                 DeleteableSendmailProperty::Comment => endpoint.comment = None,
-                DeleteableSendmailProperty::Filter => endpoint.filter = None,
                 DeleteableSendmailProperty::Mailto => endpoint.mailto = None,
                 DeleteableSendmailProperty::MailtoUser => endpoint.mailto_user = None,
+                DeleteableSendmailProperty::Disable => endpoint.disable = None,
             }
         }
     }
@@ -117,9 +110,8 @@ pub fn update_endpoint(
         endpoint.comment = Some(comment.into());
     }
 
-    if let Some(filter) = &updater.filter {
-        let _ = super::filter::get_filter(config, filter)?;
-        endpoint.filter = Some(filter.into());
+    if let Some(disable) = &updater.disable {
+        endpoint.disable = Some(*disable);
     }
 
     if endpoint.mailto.is_none() && endpoint.mailto_user.is_none() {
@@ -152,14 +144,14 @@ pub fn update_endpoint(
 pub fn delete_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)?;
 
     config.config.sections.remove(name);
 
     Ok(())
 }
 
-#[cfg(test)]
+#[cfg(all(feature = "pve-context", test))]
 pub mod tests {
     use super::*;
     use crate::api::test_helpers::*;
@@ -178,6 +170,7 @@ pub mod tests {
                 author: Some("root".into()),
                 comment: Some("Comment".into()),
                 filter: None,
+                ..Default::default()
             },
         )?;
 
@@ -189,12 +182,10 @@ pub mod tests {
     fn test_sendmail_create() -> Result<(), HttpError> {
         let mut config = empty_config();
 
-        assert_eq!(get_endpoints(&config)?.len(), 0);
         add_sendmail_endpoint_for_test(&mut config, "sendmail-endpoint")?;
 
         // Endpoints must have a unique name
         assert!(add_sendmail_endpoint_for_test(&mut config, "sendmail-endpoint").is_err());
-        assert_eq!(get_endpoints(&config)?.len(), 1);
         Ok(())
     }
 
@@ -221,7 +212,7 @@ pub mod tests {
                 from_address: Some("root@example.com".into()),
                 author: Some("newauthor".into()),
                 comment: Some("new comment".into()),
-                filter: None,
+                ..Default::default()
             },
             None,
             Some(&[0; 32]),
@@ -247,7 +238,7 @@ pub mod tests {
                 from_address: Some("root@example.com".into()),
                 author: Some("newauthor".into()),
                 comment: Some("new comment".into()),
-                filter: None,
+                ..Default::default()
             },
             None,
             Some(&digest),
@@ -294,7 +285,6 @@ pub mod tests {
 
         delete_endpoint(&mut config, "sendmail-endpoint")?;
         assert!(delete_endpoint(&mut config, "sendmail-endpoint").is_err());
-        assert_eq!(get_endpoints(&config)?.len(), 0);
 
         Ok(())
     }