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 58CC41FF0C0 for ; Wed, 18 Nov 2026 10:52:41 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id CB99721EA1; Wed, 18 Nov 2026 10:45:14 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox-perl-rs v2 49/63] pve: notify: adapt to changes in the public API of proxmox-notify Date: Wed, 18 Nov 2026 10:43:33 +0100 Message-ID: <20261118094348.273646-50-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: 1794995041244 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.186 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: NDFSZEYDOKBFUNNNFDXP36AC6HOXY7XB X-Message-ID-Hash: NDFSZEYDOKBFUNNNFDXP36AC6HOXY7XB 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: proxmox-notify no longer keeps a global context that has to be set when the module is loaded. Instead, the bindings now own a lazily initialized static PVE context and pass it explicitly when sending notifications. The context also supplies the default config, which is now handed to the config parser. The config update methods now take a ConfigDigest directly, so the digest parameter is parsed on deserialization and the local decode_digest helper is no longer needed. proxmox-notify no longer provides a test_target helper. Testing a target is now done by sending a regular test notification addressed to that target and checking the returned receipt, raising an error if delivery failed. The target is enabled in the in-memory copy of the config beforehand, so that disabled targets can still be tested. Apart from that, adapt to the types now being exported from proxmox_notify::api_types (with the Deleteable* -> Deletable* rename), the new notification template builder, and enable the api-impl and send-impl features, which are now required for the config API and for sending. Signed-off-by: Lukas Wagner --- pve-rs/Cargo.toml | 2 +- pve-rs/src/bindings/mod.rs | 3 - pve-rs/src/bindings/notify.rs | 175 +++++++++++++++++++--------------- pve-rs/src/lib.rs | 29 ++++-- 4 files changed, 116 insertions(+), 93 deletions(-) diff --git a/pve-rs/Cargo.toml b/pve-rs/Cargo.toml index d676395..9dbf4c9 100644 --- a/pve-rs/Cargo.toml +++ b/pve-rs/Cargo.toml @@ -43,7 +43,7 @@ proxmox-http = { version = "1.0.2", features = ["client-sync", "client-trait"] } proxmox-http-error = "1" proxmox-log = "1" proxmox-network-types = "1.1.2" -proxmox-notify = { version = "2", features = ["pve-context", "legacy-matchers"] } +proxmox-notify = { version = "2", features = [ "api-impl", "legacy-matchers", "pve-context", "send-impl" ] } proxmox-oci = "0.2.2" proxmox-openid = "1.0.2" proxmox-resource-scheduling = "2" diff --git a/pve-rs/src/bindings/mod.rs b/pve-rs/src/bindings/mod.rs index 143a62b..d91a122 100644 --- a/pve-rs/src/bindings/mod.rs +++ b/pve-rs/src/bindings/mod.rs @@ -31,13 +31,10 @@ pub mod proxmox_lib_pve { //! //! This contains the `init` function executed by the module on startup. - use proxmox_notify::context::pve::PVE_CONTEXT; - use crate::common; #[export] fn init() { common::logger::init("PVE_LOG", "info"); - proxmox_notify::context::set_context(&PVE_CONTEXT); } } diff --git a/pve-rs/src/bindings/notify.rs b/pve-rs/src/bindings/notify.rs index 546a742..a5faa93 100644 --- a/pve-rs/src/bindings/notify.rs +++ b/pve-rs/src/bindings/notify.rs @@ -13,31 +13,24 @@ pub mod pve_rs_notify { use std::sync::Mutex; use anyhow::{Error, bail}; - use serde_json::Value as JSONValue; + use serde_json::{Value as JSONValue, json}; use perlmod::Value; + use proxmox_config_digest::ConfigDigest; use proxmox_http_error::HttpError; - use proxmox_notify::api::Target; - use proxmox_notify::endpoints::gotify::{ - DeleteableGotifyProperty, GotifyConfig, GotifyConfigUpdater, GotifyPrivateConfig, - GotifyPrivateConfigUpdater, + use proxmox_notify::api_types::{ + DeletableGotifyProperty, DeletableMatcherProperty, DeletableSendmailProperty, + DeletableSmtpProperty, DeletableWebhookProperty, GotifyConfig, GotifyConfigUpdater, + GotifyPrivateConfig, GotifyPrivateConfigUpdater, MatcherConfig, MatcherConfigUpdater, + SendmailConfig, SendmailConfigUpdater, SmtpConfig, SmtpConfigUpdater, SmtpPrivateConfig, + SmtpPrivateConfigUpdater, Target, TargetReceiptStatus, WebhookConfig, WebhookConfigUpdater, }; - use proxmox_notify::endpoints::sendmail::{ - DeleteableSendmailProperty, SendmailConfig, SendmailConfigUpdater, - }; - use proxmox_notify::endpoints::smtp::{ - DeleteableSmtpProperty, SmtpConfig, SmtpConfigUpdater, SmtpPrivateConfig, - SmtpPrivateConfigUpdater, - }; - use proxmox_notify::endpoints::webhook::{ - DeleteableWebhookProperty, WebhookConfig, WebhookConfigUpdater, - }; - use proxmox_notify::matcher::{DeleteableMatcherProperty, MatcherConfig, MatcherConfigUpdater}; - use proxmox_notify::{Config, Notification, Severity, api}; + use proxmox_notify::{Config, Notification, Recipient, Severity, api}; + use proxmox_notify::{http_bail, http_err}; - /// A notification catalog instance. + /// A notification configuration instance. /// - /// See [`Config`]. + /// See [`proxmox_notify::Config`]. pub struct NotificationConfig { config: Mutex, } @@ -89,9 +82,11 @@ pub mod pve_rs_notify { let raw_config = std::str::from_utf8(raw_config)?; let raw_private_config = std::str::from_utf8(raw_private_config)?; + let default_config = crate::notify_context().default_config(); + Ok(perlmod::instantiate_magic!(&class, MAGIC => Box::new( NotificationConfig { - config: Mutex::new(Config::new(raw_config, raw_private_config)?) + config: Mutex::new(Config::parse_section_config(raw_config, raw_private_config, Some(default_config))?) } ))) } @@ -101,7 +96,7 @@ pub mod pve_rs_notify { pub fn write_config( #[try_from_ref] this: &NotificationConfig, ) -> Result<(String, String), Error> { - Ok(this.config.lock().unwrap().write()?) + Ok(this.config.lock().unwrap().to_section_config()?) } /// Method: Returns the SHA256 digest of the configuration. @@ -110,15 +105,16 @@ pub mod pve_rs_notify { #[export] pub fn digest(#[try_from_ref] this: &NotificationConfig) -> String { let config = this.config.lock().unwrap(); - hex::encode(config.digest()) + config.digest().to_string() } /// Method: Send a notification from a template. /// - /// This instantiates a [`Notification`] via [`from_template`](Notification::from_template()) + /// This instantiates a [`Notification`] via + /// [`template_builder`](Notification::template_builder()) /// and sends it according to the configuration. /// - /// See [`api::common::send`]. + /// See [`proxmox_notify::send`]. #[export(serialize_error)] pub fn send( #[try_from_ref] this: &NotificationConfig, @@ -128,14 +124,21 @@ pub mod pve_rs_notify { fields: Option>, ) -> Result<(), HttpError> { let config = this.config.lock().unwrap(); - let notification = Notification::from_template( - severity, - template_name, - template_data.unwrap_or_default(), - fields.unwrap_or_default(), - ); - api::common::send(&config, ¬ification) + let notification = + Notification::template_builder(template_name, template_data.unwrap_or_default()) + .severity(severity) + .fields(fields.unwrap_or_default()) + .build() + .map_err(|err| { + http_err!(INTERNAL_SERVER_ERROR, "failed to build notification: {err}") + })?; + + proxmox_notify::send(crate::notify_context(), &config, ¬ification).map_err(|err| { + http_err!(INTERNAL_SERVER_ERROR, "failed to send notification: {err}") + })?; + + Ok(()) } /// Method: Get a list of all notification targets. @@ -149,14 +152,58 @@ pub mod pve_rs_notify { api::get_targets(&config) } - /// Method: Test a target, see [`api::common::test_target`]. + /// Method: Test a target. #[export(serialize_error)] pub fn test_target( #[try_from_ref] this: &NotificationConfig, target: &str, ) -> Result<(), HttpError> { - let config = this.config.lock().unwrap(); - api::common::test_target(&config, target) + // create a copy to make sure that we don't persist the modified configuration + let mut config = { + let config = this.config.lock().unwrap(); + config.clone() + }; + + let context = crate::notify_context(); + + // Testing targets should also work if they are set to 'disabled' + // We don't persist the modified config, so this is ephemeral. + proxmox_notify::api::set_entity_disabled(&mut config, target, false)?; + + let notification = Notification::template_builder("test", json!({ "target": &target })) + .severity(Severity::Info) + .recipient(Recipient::Target(target.to_string())) + .build() + .map_err(|err| http_err!(INTERNAL_SERVER_ERROR, "{err}"))?; + + let receipt = proxmox_notify::send(context, &config, ¬ification) + .map_err(|err| http_err!(INTERNAL_SERVER_ERROR, "{err}"))?; + + match receipt + .target_receipts() + .first() + .ok_or_else(|| { + http_err!( + INTERNAL_SERVER_ERROR, + "target receipt array did not contain expected entry" + ) + })? + .status() + { + TargetReceiptStatus::Delivered => Ok(()), + TargetReceiptStatus::Error { error_message } => { + http_bail!( + INTERNAL_SERVER_ERROR, + "could not test target: {error_message}" + ) + } + TargetReceiptStatus::Disabled => { + http_bail!( + INTERNAL_SERVER_ERROR, + "target {target} disabled while sending test notification -- this should not happen" + ) + } + } } /// Method: Get sendmail endpoints. @@ -203,19 +250,12 @@ pub mod pve_rs_notify { #[try_from_ref] this: &NotificationConfig, name: &str, updater: SendmailConfigUpdater, - delete: Option>, - digest: Option<&str>, + delete: Option>, + digest: Option, ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); - let digest = decode_digest(digest)?; - api::sendmail::update_endpoint( - &mut config, - name, - updater, - delete.as_deref(), - digest.as_deref(), - ) + api::sendmail::update_endpoint(&mut config, name, updater, delete.as_deref(), digest) } /// Method: Delete a sendmail endpoint. @@ -275,11 +315,10 @@ pub mod pve_rs_notify { name: &str, updater: GotifyConfigUpdater, private_updater: GotifyPrivateConfigUpdater, - delete: Option>, - digest: Option<&str>, + delete: Option>, + digest: Option, ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); - let digest = decode_digest(digest)?; api::gotify::update_endpoint( &mut config, @@ -287,7 +326,7 @@ pub mod pve_rs_notify { updater, private_updater, delete.as_deref(), - digest.as_deref(), + digest, ) } @@ -348,11 +387,10 @@ pub mod pve_rs_notify { name: &str, updater: SmtpConfigUpdater, private_updater: SmtpPrivateConfigUpdater, - delete: Option>, - digest: Option<&str>, + delete: Option>, + digest: Option, ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); - let digest = decode_digest(digest)?; api::smtp::update_endpoint( &mut config, @@ -360,7 +398,7 @@ pub mod pve_rs_notify { updater, private_updater, delete.as_deref(), - digest.as_deref(), + digest, ) } @@ -419,19 +457,12 @@ pub mod pve_rs_notify { #[try_from_ref] this: &NotificationConfig, name: &str, config_updater: WebhookConfigUpdater, - delete: Option>, - digest: Option<&str>, + delete: Option>, + digest: Option, ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); - let digest = decode_digest(digest)?; - api::webhook::update_endpoint( - &mut config, - name, - config_updater, - delete.as_deref(), - digest.as_deref(), - ) + api::webhook::update_endpoint(&mut config, name, config_updater, delete.as_deref(), digest) } /// Method: Delete a webhook endpoint. @@ -495,19 +526,12 @@ pub mod pve_rs_notify { #[try_from_ref] this: &NotificationConfig, name: &str, updater: MatcherConfigUpdater, - delete: Option>, - digest: Option<&str>, + delete: Option>, + digest: Option, ) -> Result<(), HttpError> { let mut config = this.config.lock().unwrap(); - let digest = decode_digest(digest)?; - api::matcher::update_matcher( - &mut config, - name, - updater, - delete.as_deref(), - digest.as_deref(), - ) + api::matcher::update_matcher(&mut config, name, updater, delete.as_deref(), digest) } /// Method: Delete a matcher. @@ -533,11 +557,4 @@ pub mod pve_rs_notify { let config = this.config.lock().unwrap(); api::common::get_referenced_entities(&config, name) } - - fn decode_digest(digest: Option<&str>) -> Result>, HttpError> { - digest - .map(hex::decode) - .transpose() - .map_err(|e| api::http_err!(BAD_REQUEST, "invalid digest: {e}")) - } } diff --git a/pve-rs/src/lib.rs b/pve-rs/src/lib.rs index b32b061..a888877 100644 --- a/pve-rs/src/lib.rs +++ b/pve-rs/src/lib.rs @@ -2,7 +2,7 @@ #![deny(missing_docs)] -use std::collections::HashMap; +use std::sync::OnceLock; use anyhow::Error; use serde_json::json; @@ -24,9 +24,12 @@ fn send_notification(notification: &Notification) -> Result<(), Error> { proxmox_sys::fs::file_read_optional_string("/etc/pve/priv/notifications.cfg")? .unwrap_or_default(); - let config = Config::new(&config, &private_config)?; + let context = crate::notify_context(); - proxmox_notify::api::common::send(&config, notification)?; + let config = + Config::parse_section_config(&config, &private_config, Some(context.default_config()))?; + + proxmox_notify::send(context, &config, notification)?; Ok(()) } @@ -36,11 +39,6 @@ fn send_notification(notification: &Notification) -> Result<(), Error> { pub fn send_updates_available(updates: &[&APTUpdateInfo]) -> Result<(), Error> { let hostname = proxmox_sys::nodename().to_string(); - let metadata = HashMap::from([ - ("hostname".into(), hostname.clone()), - ("type".into(), "package-updates".into()), - ]); - // The template uses the `table` handlebars helper, so // we need to form the approriate data structure first. let update_table = json!({ @@ -68,9 +66,20 @@ pub fn send_updates_available(updates: &[&APTUpdateInfo]) -> Result<(), Error> { "updates": update_table, }); - let notification = - Notification::from_template(Severity::Info, "package-updates", template_data, metadata); + let notification = Notification::template_builder("package-updates", template_data) + .severity(Severity::Info) + .field("hostname", hostname) + .field("type", "package-updates") + .build()?; send_notification(¬ification)?; Ok(()) } + +static NOTIFY_CONTEXT: OnceLock = OnceLock::new(); + +/// Get a reference to the static notification context for PVE. +pub fn notify_context() -> &'static proxmox_notify::Context { + NOTIFY_CONTEXT + .get_or_init(|| proxmox_notify::Context::new(proxmox_notify::context::pve::PVEContext)) +} -- 2.47.3