mirror of
https://git.proxmox.com/git/proxmox
synced 2025-05-02 01:18:52 +00:00

This commit has the aim of making template rendering a bit more robust. It does so by a.) Accepting also strings for helpers that expect a number, parsing the number if needed, and b.) Ignoring errors if a template helper fails to render a value and showing an error in the logs, instead of failing to render the whole template (leading to no notification being sent). Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
64 lines
1.6 KiB
Rust
64 lines
1.6 KiB
Rust
use proxmox_notify::renderer::{render_template, TemplateRenderer};
|
|
use proxmox_notify::Error;
|
|
|
|
use serde_json::json;
|
|
|
|
const TEMPLATE: &str = r#"
|
|
{{ heading-1 "Backup Report"}}
|
|
A backup job on host {{host}} was run.
|
|
|
|
{{ heading-2 "Guests"}}
|
|
{{ table table }}
|
|
The total size of all backups is {{human-bytes total-size}}.
|
|
|
|
The backup job took {{duration total-time}}.
|
|
|
|
{{ heading-2 "Logs"}}
|
|
{{ verbatim-monospaced logs}}
|
|
|
|
{{ heading-2 "Objects"}}
|
|
{{ object table }}
|
|
"#;
|
|
|
|
fn main() -> Result<(), Error> {
|
|
let properties = json!({
|
|
"host": "pali",
|
|
"logs": "100: starting backup\n100: backup failed",
|
|
"total-size": 1024 * 1024 + 2048 * 1024,
|
|
"total-time": 100,
|
|
"table": {
|
|
"schema": {
|
|
"columns": [
|
|
{
|
|
"label": "VMID",
|
|
"id": "vmid"
|
|
},
|
|
{
|
|
"label": "Size",
|
|
"id": "size",
|
|
"renderer": "human-bytes"
|
|
}
|
|
],
|
|
},
|
|
"data" : [
|
|
{
|
|
"vmid": 1001,
|
|
"size": "1048576"
|
|
},
|
|
{
|
|
"vmid": 1002,
|
|
"size": 2048 * 1024,
|
|
}
|
|
]
|
|
}
|
|
});
|
|
|
|
let output = render_template(TemplateRenderer::Html, TEMPLATE, Some(&properties))?;
|
|
println!("{output}");
|
|
|
|
let output = render_template(TemplateRenderer::Plaintext, TEMPLATE, Some(&properties))?;
|
|
println!("{output}");
|
|
|
|
Ok(())
|
|
}
|