From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id C73D81FF0C0 for ; Wed, 18 Nov 2026 10:49:00 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 53A2B21B1E; Wed, 18 Nov 2026 10:45:03 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox v2 37/63] notify: remove 'test_target' API Date: Wed, 18 Nov 2026 10:43:21 +0100 Message-ID: <20261118094348.273646-38-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261118094348.273646-1-l.wagner@proxmox.com> References: <20261118094348.273646-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794995039613 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.191 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 2PY4ACH4KX3S3E3RGUF4FMX3A7VDFFIY X-Message-ID-Hash: 2PY4ACH4KX3S3E3RGUF4FMX3A7VDFFIY X-MailFrom: l.wagner@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Test notifications are now handled via a regular 'send', this is now possible via the new recipient property of a notification, which allows us to notify a target directly. This shifts some of the aspects of test notifications towards the application code, such as the name of the template, template data and metadata. Since testing a target should also work when the target is set disabled, a new generic 'set_entity_disabled' API method is added. This allows us to temporarily enable the target in the product code, just before sending the test notification. The modified entity is never persisted. Signed-off-by: Lukas Wagner --- proxmox-notify/src/api/mod.rs | 58 +++++++++++++++++++++++++++++++++++ proxmox-notify/src/lib.rs | 2 +- proxmox-notify/src/send.rs | 47 ++-------------------------- 3 files changed, 61 insertions(+), 46 deletions(-) diff --git a/proxmox-notify/src/api/mod.rs b/proxmox-notify/src/api/mod.rs index fb6ba9ad9..b09c9e536 100644 --- a/proxmox-notify/src/api/mod.rs +++ b/proxmox-notify/src/api/mod.rs @@ -5,6 +5,7 @@ use proxmox_http_error::HttpError; use crate::Config; use crate::api_types::{EndpointType, Origin, Target}; +use crate::config::ConfigEntity; pub mod common; pub mod gotify; @@ -84,6 +85,34 @@ pub fn get_targets(config: &Config) -> Result, HttpError> { Ok(targets) } +/// Set the `disable` property of an entity. +/// +/// The caller is responsible for any needed permission checks. +/// The caller also responsible for locking the configuration files. +/// +/// Returns a `HttpError` if: +/// - the entity does not exist (`404 Not found`) +/// +pub fn set_entity_disabled( + config: &mut Config, + name: &str, + disable: bool, +) -> Result<(), HttpError> { + if let Some(entity) = config.config.get_mut(name) { + match entity { + ConfigEntity::Sendmail(config) => config.disable = Some(disable), + ConfigEntity::Gotify(config) => config.disable = Some(disable), + ConfigEntity::Matcher(config) => config.disable = Some(disable), + ConfigEntity::Smtp(config) => config.disable = Some(disable), + ConfigEntity::Webhook(config) => config.disable = Some(disable), + } + } else { + http_bail!(NOT_FOUND, "entity '{name}' not found"); + } + + Ok(()) +} + fn verify_digest(config: &Config, digest: Option) -> Result<(), HttpError> { config .digest() @@ -318,4 +347,33 @@ mod tests { assert!(ensure_endpoints_exist(&config, &["sendmail", "gotify", "builtin"]).is_ok()); } + + #[test] + fn test_set_target_disabled() -> Result<(), HttpError> { + let mut config = prepare_config()?; + + set_entity_disabled(&mut config, "matcher", true)?; + assert_eq!( + matcher::get_matcher(&config, "matcher")?.disable, + Some(true) + ); + + set_entity_disabled(&mut config, "sendmail", true)?; + set_entity_disabled(&mut config, "gotify", true)?; + assert_eq!( + sendmail::get_endpoint(&config, "sendmail")?.disable, + Some(true) + ); + assert_eq!(gotify::get_endpoint(&config, "gotify")?.disable, Some(true)); + + set_entity_disabled(&mut config, "sendmail", false)?; + assert_eq!( + sendmail::get_endpoint(&config, "sendmail")?.disable, + Some(false) + ); + + assert!(set_entity_disabled(&mut config, "nonexistent", true).is_err()); + + Ok(()) + } } diff --git a/proxmox-notify/src/lib.rs b/proxmox-notify/src/lib.rs index c446b52de..4304e2977 100644 --- a/proxmox-notify/src/lib.rs +++ b/proxmox-notify/src/lib.rs @@ -26,7 +26,7 @@ mod renderer; #[cfg(feature = "send-impl")] mod send; #[cfg(feature = "send-impl")] -pub use send::{send, test_target}; +pub use send::send; mod error; pub use error::Error; diff --git a/proxmox-notify/src/send.rs b/proxmox-notify/src/send.rs index f05661367..42cd0bb1a 100644 --- a/proxmox-notify/src/send.rs +++ b/proxmox-notify/src/send.rs @@ -1,8 +1,6 @@ use std::collections::{HashMap, HashSet}; use proxmox_section_config::typed::{convert_to_typed_array, lookup}; -use proxmox_uuid::Uuid; -use serde_json::json; use tracing::{error, info}; use crate::api_types::{MatcherConfig, SendmailConfig}; @@ -12,9 +10,7 @@ use crate::endpoints::gotify::GotifyEndpoint; use crate::endpoints::sendmail::SendmailEndpoint; use crate::endpoints::smtp::SmtpEndpoint; use crate::endpoints::webhook::WebhookEndpoint; -use crate::{ - Config, Content, Context, EmailRecipient, Error, Metadata, Notification, Recipient, Severity, -}; +use crate::{Config, Context, EmailRecipient, Error, Notification, Recipient}; /// Send a notification to a given target. /// @@ -27,17 +23,6 @@ pub fn send(context: &Context, config: &Config, notification: &Notification) -> Ok(()) } -/// Test target (group or single endpoint) identified by its `name`. -/// -/// The caller is responsible for any needed permission checks. -/// Returns an `anyhow::Error` if sending via the endpoint failed. -pub fn test_target(context: &Context, config: &Config, endpoint: &str) -> Result<(), Error> { - let bus = Bus::from_config(config)?; - bus.test_target(context, endpoint)?; - - Ok(()) -} - /// Notification bus - distributes notifications to all registered endpoints // The reason for the split between `Config` and this struct is to make testing with mocked // endpoints a bit easier. @@ -212,35 +197,6 @@ impl Bus { } } - /// Send a test notification to a target (endpoint or group). - /// - /// In contrast to the `send` function, this function will return - /// any errors to the caller. - pub fn test_target(&self, context: &Context, target: &str) -> Result<(), Error> { - let notification = Notification { - metadata: Metadata { - severity: Severity::Info, - // TODO: what fields would make sense for test notifications? - additional_fields: Default::default(), - timestamp: proxmox_time::epoch_i64(), - }, - content: Content::Template { - template_name: "test".to_string(), - data: json!({ "target": target }), - }, - id: Uuid::generate(), - recipient: Recipient::Target(target.into()), - }; - - if let Some(endpoint) = self.endpoints.get(target) { - endpoint.send(context, ¬ification)?; - } else { - return Err(Error::target_does_not_exist(target)); - } - - Ok(()) - } - fn check_matches(&self, notification: &Notification) -> HashSet<&str> { let mut targets = HashSet::new(); let mut matching_matchers = Vec::new(); @@ -285,6 +241,7 @@ mod tests { use proxmox_match_expression::Expression; use crate::{ + Severity, api_types::{expression::NotificationMatcher, severity::SeverityMatcher}, context::{Context, test}, }; -- 2.47.3