]> git.proxmox.com Git - proxmox-perl-rs.git/commitdiff
add PVE::RS::Notify module
authorLukas Wagner <l.wagner@proxmox.com>
Thu, 20 Jul 2023 14:31:53 +0000 (16:31 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Mon, 24 Jul 2023 09:17:31 +0000 (11:17 +0200)
Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
pve-rs/Cargo.toml
pve-rs/Makefile
pve-rs/src/lib.rs
pve-rs/src/notify.rs [new file with mode: 0644]

index 3076a139305441dc306266351f8cdbffb680c2eb..b28c1182dc2d44e736962b535003a632edc71685 100644 (file)
@@ -34,6 +34,7 @@ perlmod = { version = "0.13", features = [ "exporter" ] }
 
 proxmox-apt = "0.10"
 proxmox-http = { version = "0.9", features = ["client-sync", "client-trait"] }
+proxmox-notify = "0.1"
 proxmox-openid = "0.10"
 proxmox-resource-scheduling = "0.3.0"
 proxmox-subscription = "0.4"
index de35c699e0dc7fe3066d122d965a1121bb6fd55d..9d737c0c1c224d8ee4f461bddb110430300dc41b 100644 (file)
@@ -27,6 +27,7 @@ PERLMOD_GENPACKAGE := /usr/lib/perlmod/genpackage.pl \
 
 PERLMOD_PACKAGES := \
          PVE::RS::APT::Repositories \
+         PVE::RS::Notify \
          PVE::RS::OpenId \
          PVE::RS::ResourceScheduling::Static \
          PVE::RS::TFA
index eb6ae02e16e2569b92ff6986af85ff4aa912c0ef..0d63c2826f65ee00b7ef5691544962fd774bd4ed 100644 (file)
@@ -4,6 +4,7 @@
 pub mod common;
 
 pub mod apt;
+pub mod notify;
 pub mod openid;
 pub mod resource_scheduling;
 pub mod tfa;
diff --git a/pve-rs/src/notify.rs b/pve-rs/src/notify.rs
new file mode 100644 (file)
index 0000000..6ea9b78
--- /dev/null
@@ -0,0 +1,74 @@
+#[perlmod::package(name = "PVE::RS::Notify")]
+mod export {
+    use anyhow::{bail, Error};
+    use perlmod::Value;
+
+    use std::sync::Mutex;
+
+    use proxmox_notify::Config;
+
+    pub struct NotificationConfig {
+        config: Mutex<Config>,
+    }
+
+    perlmod::declare_magic!(Box<NotificationConfig> : &NotificationConfig as "PVE::RS::Notify");
+
+    /// 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 parse_config(
+        #[raw] class: Value,
+        raw_config: &[u8],
+        raw_private_config: &[u8],
+    ) -> Result<Value, Error> {
+        let raw_config = std::str::from_utf8(raw_config)?;
+        let raw_private_config = std::str::from_utf8(raw_private_config)?;
+
+        Ok(perlmod::instantiate_magic!(&class, MAGIC => Box::new(
+            NotificationConfig {
+                config: Mutex::new(Config::new(raw_config, raw_private_config)?)
+            }
+        )))
+    }
+
+    #[export]
+    fn write_config(#[try_from_ref] this: &NotificationConfig) -> Result<(String, String), Error> {
+        Ok(this.config.lock().unwrap().write()?)
+    }
+
+    #[export]
+    fn digest(#[try_from_ref] this: &NotificationConfig) -> String {
+        let config = this.config.lock().unwrap();
+        hex::encode(config.digest())
+    }
+}