public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup v2 04/10] notifications: add type for APT notification template data
Date: Thu, 27 Mar 2025 15:55:27 +0100	[thread overview]
Message-ID: <20250327145533.274348-5-l.wagner@proxmox.com> (raw)
In-Reply-To: <20250327145533.274348-1-l.wagner@proxmox.com>

This commit adds a separate type for the data passed to this type of
notification template. Also we make sure that we do not expose any
non-primitive types to the template renderer, any data
needed in the template is mapped into the new dedicated
template data type.
This ensures that any changes in types defined in other places
do not leak into the template rendering process by accident.

This commit also tries to unify the style and naming of template
variables.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
Reviewed-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---
 src/server/notifications/mod.rs               | 23 ++++++-----
 src/server/notifications/template_data.rs     | 41 ++++++++++++++++++-
 .../default/package-updates-body.txt.hbs      |  8 ++--
 .../default/package-updates-subject.txt.hbs   |  2 +-
 4 files changed, 57 insertions(+), 17 deletions(-)

diff --git a/src/server/notifications/mod.rs b/src/server/notifications/mod.rs
index a2730d71..ba55e2b1 100644
--- a/src/server/notifications/mod.rs
+++ b/src/server/notifications/mod.rs
@@ -23,7 +23,10 @@ const SPOOL_DIR: &str = concatcp!(pbs_buildcfg::PROXMOX_BACKUP_STATE_DIR, "/noti
 
 mod template_data;
 
-use template_data::{AcmeErrTemplateData, CommonData, GcErrTemplateData, GcOkTemplateData};
+use template_data::{
+    AcmeErrTemplateData, CommonData, GcErrTemplateData, GcOkTemplateData,
+    PackageUpdatesTemplateData,
+};
 
 /// Initialize the notification system by setting context in proxmox_notify
 pub fn init() -> Result<(), Error> {
@@ -464,23 +467,21 @@ fn get_server_url() -> (String, usize) {
 }
 
 pub fn send_updates_available(updates: &[&APTUpdateInfo]) -> Result<(), Error> {
-    let (fqdn, port) = get_server_url();
     let hostname = proxmox_sys::nodename().to_string();
 
-    let data = json!({
-        "fqdn": fqdn,
-        "hostname": &hostname,
-        "port": port,
-        "updates": updates,
-    });
-
     let metadata = HashMap::from([
         ("hostname".into(), hostname),
         ("type".into(), "package-updates".into()),
     ]);
 
-    let notification =
-        Notification::from_template(Severity::Info, "package-updates", data, metadata);
+    let template_data = PackageUpdatesTemplateData::new(updates);
+
+    let notification = Notification::from_template(
+        Severity::Info,
+        "package-updates",
+        serde_json::to_value(template_data)?,
+        metadata,
+    );
 
     send_notification(notification)?;
     Ok(())
diff --git a/src/server/notifications/template_data.rs b/src/server/notifications/template_data.rs
index c3e31367..60f4eba2 100644
--- a/src/server/notifications/template_data.rs
+++ b/src/server/notifications/template_data.rs
@@ -1,4 +1,4 @@
-use pbs_api_types::GarbageCollectionStatus;
+use pbs_api_types::{APTUpdateInfo, GarbageCollectionStatus};
 use serde::Serialize;
 
 // NOTE: For some of these types, the `XyzOkTemplateData` and `XyzErrTemplateData`
@@ -141,3 +141,42 @@ pub struct AcmeErrTemplateData {
     /// The error that occured when trying to request the certificate.
     pub error: String,
 }
+
+#[derive(Serialize)]
+#[serde(rename_all = "kebab-case")]
+/// A single package which can be upgraded.
+pub struct UpgradablePackage {
+    /// The name of the package.
+    package_name: String,
+    /// The new version which can be installed.
+    available_version: String,
+    /// The currently installed version.
+    installed_version: String,
+}
+
+/// Template data for the package-updates template.
+#[derive(Serialize)]
+#[serde(rename_all = "kebab-case")]
+pub struct PackageUpdatesTemplateData {
+    /// Common properties.
+    #[serde(flatten)]
+    pub common: CommonData,
+    pub available_updates: Vec<UpgradablePackage>,
+}
+
+impl PackageUpdatesTemplateData {
+    /// Create new a new instance.
+    pub fn new(updates: &[&APTUpdateInfo]) -> Self {
+        Self {
+            common: CommonData::new(),
+            available_updates: updates
+                .iter()
+                .map(|info| UpgradablePackage {
+                    package_name: info.package.clone(),
+                    available_version: info.version.clone(),
+                    installed_version: info.old_version.clone(),
+                })
+                .collect(),
+        }
+    }
+}
diff --git a/templates/default/package-updates-body.txt.hbs b/templates/default/package-updates-body.txt.hbs
index 62f9c7c4..cf69ae69 100644
--- a/templates/default/package-updates-body.txt.hbs
+++ b/templates/default/package-updates-body.txt.hbs
@@ -1,8 +1,8 @@
 Proxmox Backup Server has the following updates available:
-{{#each updates }}
-    {{Package}}: {{OldVersion}} -> {{Version~}}
-{{/each }}
+{{#each available-updates}}
+    {{this.package-name}}: {{this.installed-version}} -> {{this.available-version~}}
+{{/each}}
 
 To upgrade visit the web interface:
 
-<https://{{fqdn}}:{{port}}/#pbsServerAdministration:updates>
+<{{base-url}}/#pbsServerAdministration:updates>
diff --git a/templates/default/package-updates-subject.txt.hbs b/templates/default/package-updates-subject.txt.hbs
index c8a775d5..556a67b8 100644
--- a/templates/default/package-updates-subject.txt.hbs
+++ b/templates/default/package-updates-subject.txt.hbs
@@ -1 +1 @@
-New software packages available ({{ hostname }})
+New software packages available ({{hostname}})
-- 
2.39.5



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


  parent reply	other threads:[~2025-03-27 14:55 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-27 14:55 [pbs-devel] [PATCH proxmox-backup v2 00/10] notifications: cleanup in preparation of overridable templates Lukas Wagner
2025-03-27 14:55 ` [pbs-devel] [PATCH proxmox-backup v2 01/10] notifications: move make notifications module a dir-style module Lukas Wagner
2025-03-27 14:55 ` [pbs-devel] [PATCH proxmox-backup v2 02/10] notifications: add type for GC notification template data Lukas Wagner
2025-03-27 14:55 ` [pbs-devel] [PATCH proxmox-backup v2 03/10] notifications: add type for ACME " Lukas Wagner
2025-03-27 14:55 ` Lukas Wagner [this message]
2025-03-27 14:55 ` [pbs-devel] [PATCH proxmox-backup v2 05/10] notifications: add type for prune " Lukas Wagner
2025-03-27 14:55 ` [pbs-devel] [PATCH proxmox-backup v2 06/10] notifications: add type for sync " Lukas Wagner
2025-03-27 14:55 ` [pbs-devel] [PATCH proxmox-backup v2 07/10] notifications: add type for tape backup " Lukas Wagner
2025-03-27 14:55 ` [pbs-devel] [PATCH proxmox-backup v2 08/10] notifications: add type for tape load " Lukas Wagner
2025-03-27 14:55 ` [pbs-devel] [PATCH proxmox-backup v2 09/10] notifications: add type for verify " Lukas Wagner
2025-03-27 14:55 ` [pbs-devel] [PATCH proxmox-backup v2 10/10] notifications: remove HTML template for test notification Lukas Wagner
2025-03-28 10:23 ` [pbs-devel] superseded: [PATCH proxmox-backup v2 00/10] notifications: cleanup in preparation of overridable templates Lukas Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250327145533.274348-5-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal