This module exposes the functionality provided proxmox-ve-config for
the SDN fabrics to perl. We add initial support for reading and
writing the section config stored in /etc/pve/sdn/fabrics.cfg as well
as the running configuration, stored in /etc/pve/sdn/.running-config.
It also provides a helper method for calculating the digest of the
configuration.

Co-authored-by: Gabriel Goller <g.gol...@proxmox.com>
Signed-off-by: Stefan Hanreich <s.hanre...@proxmox.com>
---
 pve-rs/Cargo.toml                  |  4 +-
 pve-rs/Makefile                    |  1 +
 pve-rs/debian/control              |  2 +
 pve-rs/src/bindings/mod.rs         |  3 +
 pve-rs/src/bindings/sdn/fabrics.rs | 95 ++++++++++++++++++++++++++++++
 pve-rs/src/bindings/sdn/mod.rs     |  1 +
 6 files changed, 105 insertions(+), 1 deletion(-)
 create mode 100644 pve-rs/src/bindings/sdn/fabrics.rs
 create mode 100644 pve-rs/src/bindings/sdn/mod.rs

diff --git a/pve-rs/Cargo.toml b/pve-rs/Cargo.toml
index 82ebce4..fbeff72 100644
--- a/pve-rs/Cargo.toml
+++ b/pve-rs/Cargo.toml
@@ -33,15 +33,17 @@ perlmod = { version = "0.13.5", features = ["exporter"] }
 proxmox-apt = { version = "0.11.5", features = ["cache"] }
 proxmox-apt-api-types = "1.0"
 proxmox-config-digest = "0.1"
+proxmox-frr = { version = "0.1" }
 proxmox-http = { version = "0.9", features = ["client-sync", "client-trait"] }
 proxmox-http-error = "0.1.0"
 proxmox-log = "0.2"
 proxmox-notify = { version = "0.5.4", features = ["pve-context"] }
 proxmox-openid = "0.10.4"
 proxmox-resource-scheduling = "0.3.0"
+proxmox-section-config = "3"
 proxmox-shared-cache = "0.1.0"
 proxmox-subscription = "0.5"
 proxmox-sys = "0.6"
 proxmox-tfa = { version = "5", features = ["api"] }
 proxmox-time = "2"
-proxmox-ve-config = { version = "0.2.1" }
+proxmox-ve-config = { version = "0.2.2", features = [ "frr" ] }
diff --git a/pve-rs/Makefile b/pve-rs/Makefile
index 4a5a277..56e78d4 100644
--- a/pve-rs/Makefile
+++ b/pve-rs/Makefile
@@ -30,6 +30,7 @@ PERLMOD_PACKAGES := \
          PVE::RS::Firewall::SDN \
          PVE::RS::OpenId \
          PVE::RS::ResourceScheduling::Static \
+         PVE::RS::SDN::Fabrics \
          PVE::RS::TFA
 
 PERLMOD_PACKAGE_FILES := $(addsuffix .pm,$(subst ::,/,$(PERLMOD_PACKAGES)))
diff --git a/pve-rs/debian/control b/pve-rs/debian/control
index 1eaca52..9d772d8 100644
--- a/pve-rs/debian/control
+++ b/pve-rs/debian/control
@@ -18,6 +18,7 @@ Build-Depends: cargo:native <!nocheck>,
                librust-proxmox-apt-0.11+default-dev (>= 0.11.5-~~),
                librust-proxmox-apt-api-types-1+default-dev,
                librust-proxmox-config-digest-0.1+default-dev,
+               librust-proxmox-frr-0.1+default-dev,
                librust-proxmox-http-0.9+client-sync-dev,
                librust-proxmox-http-0.9+client-trait-dev,
                librust-proxmox-http-0.9+default-dev,
@@ -34,6 +35,7 @@ Build-Depends: cargo:native <!nocheck>,
                librust-proxmox-tfa-5+default-dev,
                librust-proxmox-time-2+default-dev,
                librust-proxmox-ve-config-dev (>= 0.2.1-~~),
+               librust-proxmox-ve-config+frr-dev (>= 0.2.2-~~),
                librust-serde-1+default-dev,
                librust-serde-bytes-0.11+default-dev,
                librust-serde-json-1+default-dev,
diff --git a/pve-rs/src/bindings/mod.rs b/pve-rs/src/bindings/mod.rs
index a6a7c6f..79a9917 100644
--- a/pve-rs/src/bindings/mod.rs
+++ b/pve-rs/src/bindings/mod.rs
@@ -8,6 +8,9 @@ pub use 
resource_scheduling_static::pve_rs_resource_scheduling_static;
 mod tfa;
 pub use tfa::pve_rs_tfa;
 
+mod sdn;
+pub use sdn::fabrics::pve_rs_sdn_fabrics;
+
 #[allow(unused_imports)]
 pub use crate::common::bindings::*;
 
diff --git a/pve-rs/src/bindings/sdn/fabrics.rs 
b/pve-rs/src/bindings/sdn/fabrics.rs
new file mode 100644
index 0000000..fac5602
--- /dev/null
+++ b/pve-rs/src/bindings/sdn/fabrics.rs
@@ -0,0 +1,95 @@
+#[perlmod::package(name = "PVE::RS::SDN::Fabrics", lib = "pve_rs")]
+pub mod pve_rs_sdn_fabrics {
+    //! The `PVE::RS::SDN::Fabrics` package.
+    //!
+    //! This provides the configuration for the SDN fabrics, as well as helper 
methods for reading
+    //! / writing the configuration, as well as for generating ifupdown2 and 
FRR configuration.
+
+    use std::collections::BTreeMap;
+    use std::ops::Deref;
+    use std::sync::Mutex;
+
+    use anyhow::Error;
+    use openssl::hash::{hash, MessageDigest};
+    use serde::{Deserialize, Serialize};
+
+    use perlmod::Value;
+    use proxmox_section_config::typed::SectionConfigData;
+    use proxmox_ve_config::common::valid::Validatable;
+
+    use proxmox_ve_config::sdn::fabric::{section_config::Section, 
FabricConfig};
+
+    /// A SDN Fabric config instance.
+    #[derive(Serialize, Deserialize)]
+    pub struct PerlFabricConfig {
+        /// The fabric config instance
+        pub fabric_config: Mutex<FabricConfig>,
+    }
+
+    perlmod::declare_magic!(Box<PerlFabricConfig> : &PerlFabricConfig as 
"PVE::RS::SDN::Fabrics::Config");
+
+    /// Parse the raw configuration from `/etc/pve/sdn/fabrics.cfg`.
+    #[export]
+    fn config(#[raw] class: Value, raw_config: &[u8]) -> 
Result<perlmod::Value, Error> {
+        let raw_config = std::str::from_utf8(raw_config)?;
+        let config = FabricConfig::parse_section_config(raw_config)?;
+
+        Ok(
+            perlmod::instantiate_magic!(&class, MAGIC => 
Box::new(PerlFabricConfig {
+                fabric_config: Mutex::new(config.into_inner()),
+            })),
+        )
+    }
+
+    /// Parse the configuration from `/etc/pve/sdn/.running_config`.
+    #[export]
+    fn running_config(
+        #[raw] class: Value,
+        fabrics: BTreeMap<String, Section>,
+    ) -> Result<perlmod::Value, Error> {
+        let fabrics = SectionConfigData::from_iter(fabrics);
+        let config = FabricConfig::from_section_config(fabrics)?;
+
+        Ok(
+            perlmod::instantiate_magic!(&class, MAGIC => 
Box::new(PerlFabricConfig {
+                fabric_config: Mutex::new(config.into_inner()),
+            })),
+        )
+    }
+
+    /// Class method: Convert the configuration into the section config 
sections.
+    ///
+    /// Used for writing the running configuration.
+    #[export]
+    fn to_sections(
+        #[try_from_ref] this: &PerlFabricConfig,
+    ) -> Result<BTreeMap<String, Section>, Error> {
+        let config = this
+            .fabric_config
+            .lock()
+            .unwrap()
+            .clone()
+            .into_valid()?
+            .into_section_config();
+
+        Ok(BTreeMap::from_iter(config.clone()))
+    }
+
+    /// Class method: Convert the configuration into the section config string.
+    ///
+    /// Used for writing `/etc/pve/sdn/fabrics.cfg`
+    #[export]
+    fn to_raw(#[try_from_ref] this: &PerlFabricConfig) -> Result<String, 
Error> {
+        this.fabric_config.lock().unwrap().write_section_config()
+    }
+
+    /// Class method: Generate a digest for the whole configuration
+    #[export]
+    fn digest(#[try_from_ref] this: &PerlFabricConfig) -> Result<String, 
Error> {
+        let config = this.fabric_config.lock().unwrap();
+        let data = serde_json::to_vec(config.deref())?;
+        let hash = hash(MessageDigest::sha256(), &data)?;
+
+        Ok(hex::encode(hash))
+    }
+}
diff --git a/pve-rs/src/bindings/sdn/mod.rs b/pve-rs/src/bindings/sdn/mod.rs
new file mode 100644
index 0000000..0ec7009
--- /dev/null
+++ b/pve-rs/src/bindings/sdn/mod.rs
@@ -0,0 +1 @@
+pub(crate) mod fabrics;
-- 
2.39.5


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

Reply via email to