public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox-perl-rs 08/18] add basic bindings for the proxmox_notification crate
Date: Mon, 27 Mar 2023 17:18:47 +0200	[thread overview]
Message-ID: <20230327151857.495565-9-l.wagner@proxmox.com> (raw)
In-Reply-To: <20230327151857.495565-1-l.wagner@proxmox.com>

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---

Notes:
    notifications.rs should be  moved to common/src at some point.
    Currently it resides in pve-rs/src, since rust-analyzer breaks for me
    for all code in common/src.

 Cargo.toml                 |   2 +
 common/pkg/Makefile        |   1 +
 pve-rs/Cargo.toml          |   1 +
 pve-rs/src/lib.rs          |   1 +
 pve-rs/src/notification.rs | 128 +++++++++++++++++++++++++++++++++++++
 5 files changed, 133 insertions(+)
 create mode 100644 pve-rs/src/notification.rs

diff --git a/Cargo.toml b/Cargo.toml
index 94e29d5..fc0f7c1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -25,6 +25,7 @@ perlmod = { version = "0.13", features = [ "exporter" ] }
 proxmox-acme-rs = { version = "0.4", features = ["client"] }
 proxmox-apt = "0.9"
 proxmox-http = { version = "0.8", features = ["client-sync", "client-trait"] }
+proxmox-notification = "0.1"
 proxmox-openid = "0.9.8"
 proxmox-resource-scheduling = "0.2.1"
 proxmox-subscription = "0.3"
@@ -37,3 +38,4 @@ proxmox-time = "1.1.3"
 #proxmox-tfa = { path = "../proxmox/proxmox-tfa" }
 #proxmox-time = { path = "../proxmox/proxmox-time" }
 #proxmox-uuid = { path = "../proxmox/proxmox-uuid" }
+#proxmox-notification = { path = "../proxmox/proxmox-notification" }
diff --git a/common/pkg/Makefile b/common/pkg/Makefile
index bf22a7b..658d336 100644
--- a/common/pkg/Makefile
+++ b/common/pkg/Makefile
@@ -17,6 +17,7 @@ Proxmox/RS/CalendarEvent.pm: ../scripts/genpackage.pl
 	perl ../scripts/genpackage.pl Common \
 	  Proxmox::RS::APT::Repositories \
 	  Proxmox::RS::CalendarEvent \
+	  Proxmox::RS::Notification \
 	  Proxmox::RS::Subscription
 
 all: Proxmox/RS/CalendarEvent.pm
diff --git a/pve-rs/Cargo.toml b/pve-rs/Cargo.toml
index 0108eb2..9eafc25 100644
--- a/pve-rs/Cargo.toml
+++ b/pve-rs/Cargo.toml
@@ -33,6 +33,7 @@ perlmod.workspace = true
 
 proxmox-apt.workspace = true
 proxmox-http.workspace = true
+proxmox-notification.workspace = true
 proxmox-openid.workspace = true
 proxmox-resource-scheduling.workspace = true
 proxmox-subscription.workspace = true
diff --git a/pve-rs/src/lib.rs b/pve-rs/src/lib.rs
index af3076e..7126e25 100644
--- a/pve-rs/src/lib.rs
+++ b/pve-rs/src/lib.rs
@@ -4,6 +4,7 @@
 pub mod common;
 
 pub mod apt;
+pub mod notification;
 pub mod openid;
 pub mod resource_scheduling;
 pub mod tfa;
diff --git a/pve-rs/src/notification.rs b/pve-rs/src/notification.rs
new file mode 100644
index 0000000..ab325a8
--- /dev/null
+++ b/pve-rs/src/notification.rs
@@ -0,0 +1,128 @@
+#[perlmod::package(name = "Proxmox::RS::Notification")]
+mod export {
+    use anyhow::{bail, Error};
+    use perlmod::Value;
+    use std::sync::Mutex;
+
+    use std::collections::HashMap;
+
+    use proxmox_notification::{
+        filter::FilterModeOperator, methods, Config, Notification, Severity,
+    };
+
+    pub struct NotificationConfig {
+        config: Mutex<Config>,
+    }
+
+    perlmod::declare_magic!(Box<NotificationConfig> : &NotificationConfig as "Proxmox::RS::Notification");
+
+    /// Support `dclone` so this can be put into the `ccache` of `PVE::Cluster`.
+    #[export(name = "STORABLE_freeze", raw_return)]
+    fn storable_freeze(
+        #[try_from_ref] this: &NotificationConfig,
+        cloning: bool,
+    ) -> Result<Value, Error> {
+        if !cloning {
+            bail!("freezing Notification config not supported!");
+        }
+
+        let mut cloned = Box::new(NotificationConfig {
+            config: Mutex::new(this.config.lock().unwrap().clone()),
+        });
+        let value = Value::new_pointer::<NotificationConfig>(&mut *cloned);
+        let _perl = Box::leak(cloned);
+        Ok(value)
+    }
+
+    /// Instead of `thaw` we implement `attach` for `dclone`.
+    #[export(name = "STORABLE_attach", raw_return)]
+    fn storable_attach(
+        #[raw] class: Value,
+        cloning: bool,
+        #[raw] serialized: Value,
+    ) -> Result<Value, Error> {
+        if !cloning {
+            bail!("STORABLE_attach called with cloning=false");
+        }
+        let data = unsafe { Box::from_raw(serialized.pv_raw::<NotificationConfig>()?) };
+        Ok(perlmod::instantiate_magic!(&class, MAGIC => data))
+    }
+
+    #[export(raw_return)]
+    fn new(#[raw] class: Value, raw_config: &str) -> Result<Value, Error> {
+        Ok(perlmod::instantiate_magic!(&class, MAGIC => Box::new(
+            NotificationConfig {
+                config: Mutex::new(Config::new(raw_config)?)
+            }
+        )))
+    }
+
+    #[export]
+    fn write(#[try_from_ref] this: &NotificationConfig) -> Result<String, Error> {
+        this.config.lock().unwrap().write()
+    }
+
+    #[export]
+    fn send(
+        #[try_from_ref] this: &NotificationConfig,
+        severity: Severity,
+        title: &str,
+        message: &str,
+        properties: Option<HashMap<String, String>>,
+    ) -> Result<(), Error> {
+        let config = this.config.lock().unwrap();
+
+        let notification_bus = config.instantiate()?;
+
+        let notification = Notification {
+            title: title.into(),
+            body: message.into(),
+            severity,
+            properties: properties.unwrap_or_default(),
+        };
+
+        notification_bus.send(&notification)?;
+
+        Ok(())
+    }
+
+    #[export]
+    fn add_sendmail_endpoint(
+        #[try_from_ref] this: &NotificationConfig,
+        name: &str,
+        recipient: Vec<String>,
+        from_address: Option<String>,
+        author: Option<String>,
+        filter: Option<String>,
+    ) -> Result<(), Error> {
+        let mut config = this.config.lock().unwrap();
+        methods::add_sendmail_endpoint(&mut config, name, recipient, from_address, author, filter)?;
+
+        Ok(())
+    }
+
+    #[export]
+    fn add_filter(
+        #[try_from_ref] this: &NotificationConfig,
+        name: &str,
+        min_severity: Option<Severity>,
+        sub_filter: Option<Vec<String>>,
+        mode: Option<FilterModeOperator>,
+        match_properties: Option<Vec<String>>,
+        invert_match: Option<bool>,
+    ) -> Result<(), Error> {
+        let mut config = this.config.lock().unwrap();
+
+        methods::add_filter(
+            &mut config,
+            name,
+            min_severity,
+            sub_filter,
+            mode,
+            match_properties,
+            invert_match,
+        )?;
+
+        Ok(())
+    }
+}
-- 
2.30.2





  parent reply	other threads:[~2023-03-27 15:25 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-27 15:18 [pve-devel] [PATCH cluster/manager/ha-manager/proxmox{, -perl-rs} 00/18] fix #4156: introduce new notification module Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH proxmox 01/18] add proxmox-notification crate Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH proxmox 02/18] notification: implement sendmail endpoint Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH proxmox 03/18] notification: add notification filter mechanism Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH proxmox 04/18] notification: implement gotify endpoint Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH proxmox 05/18] notification: allow adding new sendmail endpoints / filters Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH proxmox 06/18] notification: add debian packaging Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH proxmox-perl-rs 07/18] log: set default log level to 'info', add product specfig logging env var1 Lukas Wagner
2023-03-31  9:17   ` Lukas Wagner
2023-03-27 15:18 ` Lukas Wagner [this message]
2023-03-27 15:18 ` [pve-devel] [PATCH pve-cluster 09/18] cluster files: add notifications.cfg Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH pve-manager 10/18] test: fix names of .PHONY targets Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH pve-manager 11/18] add PVE::Notification module Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH pve-manager 12/18] vzdump: send notifications via new notification module Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH pve-manager 13/18] vzdump: rename 'sendmail' sub to 'send_notification' Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH pve-manager 14/18] test: rename mail_test.pl to vzdump_notification_test.pl Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH pve-manager 15/18] api: apt: send notification via new notification module Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH pve-manager 16/18] api: replication: send notifications " Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH pve-ha-manager 17/18] manager: " Lukas Wagner
2023-03-27 15:18 ` [pve-devel] [PATCH pve-ha-manager 18/18] manager: rename 'sendmail' --> 'send_notification' Lukas Wagner
2023-04-14  6:19 ` [pve-devel] [PATCH cluster/manager/ha-manager/proxmox{, -perl-rs} 00/18] fix #4156: introduce new notification module Thomas Lamprecht
2023-04-14  9:47   ` 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=20230327151857.495565-9-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pve-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