In PVE we use the GET /nodes/{node}/network API endpoint to return all
currently configured network interfaces on a specific node. In order
to be able to use SDN fabrics in Ceph and the migration settings, we
add a helper method that returns all fabrics formatted in the same
format as the pre-existing PVE API call. This enables us to return the
SDN fabrics in the endpoint so users can select the fabrics from the
UI, integrating the fabrics with the existing UI components.

Co-authored-by: Gabriel Goller <g.gol...@proxmox.com>
Signed-off-by: Stefan Hanreich <s.hanre...@proxmox.com>
---
 pve-rs/src/bindings/sdn/fabrics.rs | 54 ++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)

diff --git a/pve-rs/src/bindings/sdn/fabrics.rs 
b/pve-rs/src/bindings/sdn/fabrics.rs
index f55e9f98a5c2..fd81c7710834 100644
--- a/pve-rs/src/bindings/sdn/fabrics.rs
+++ b/pve-rs/src/bindings/sdn/fabrics.rs
@@ -17,13 +17,13 @@ pub mod pve_rs_sdn_fabrics {
 
     use perlmod::Value;
     use proxmox_frr::serializer::to_raw_config;
-    use proxmox_network_types::ip_address::Cidr;
+    use proxmox_network_types::ip_address::{Cidr, Ipv4Cidr, Ipv6Cidr};
     use proxmox_section_config::typed::SectionConfigData;
     use proxmox_ve_config::common::valid::Validatable;
 
     use proxmox_ve_config::sdn::fabric::section_config::Section;
     use proxmox_ve_config::sdn::fabric::section_config::fabric::{
-        FabricId,
+        Fabric as ConfigFabric, FabricId,
         api::{Fabric, FabricUpdater},
     };
     use proxmox_ve_config::sdn::fabric::section_config::node::{
@@ -42,6 +42,34 @@ pub mod pve_rs_sdn_fabrics {
 
     perlmod::declare_magic!(Box<PerlFabricConfig> : &PerlFabricConfig as 
"PVE::RS::SDN::Fabrics::Config");
 
+    /// Represents an interface as returned by the `GET /nodes/{node}/network` 
endpoint in PVE.
+    ///
+    /// This is used for returning fabrics in the endpoint, so they can be 
used from various places
+    /// in the PVE UI (e.g. migration network settings).
+    #[derive(Serialize)]
+    pub struct PveInterface {
+        iface: String,
+        #[serde(rename = "type")]
+        ty: String,
+        active: bool,
+        #[serde(skip_serializing_if = "Option::is_none")]
+        cidr: Option<Ipv4Cidr>,
+        #[serde(skip_serializing_if = "Option::is_none")]
+        cidr6: Option<Ipv6Cidr>,
+    }
+
+    impl From<ConfigFabric> for PveInterface {
+        fn from(fabric: ConfigFabric) -> Self {
+            Self {
+                iface: fabric.id().to_string(),
+                ty: "fabric".to_string(),
+                active: true,
+                cidr: fabric.ip_prefix(),
+                cidr6: fabric.ip6_prefix(),
+            }
+        }
+    }
+
     /// Class method: Parse the raw configuration from 
`/etc/pve/sdn/fabrics.cfg`.
     #[export]
     pub fn config(#[raw] class: Value, raw_config: &[u8]) -> 
Result<perlmod::Value, Error> {
@@ -312,6 +340,28 @@ pub mod pve_rs_sdn_fabrics {
         Ok(hex::encode(hash))
     }
 
+    /// Method: Return all interfaces of a node, that are part of a fabric.
+    #[export]
+    pub fn get_interfaces_for_node(
+        #[try_from_ref] this: &PerlFabricConfig,
+        node_id: NodeId,
+    ) -> BTreeMap<String, PveInterface> {
+        let config = this.fabric_config.lock().unwrap();
+
+        let mut ifaces = BTreeMap::new();
+
+        for entry in config.values() {
+            if entry.get_node(&node_id).is_ok() {
+                ifaces.insert(
+                    entry.fabric().id().to_string(),
+                    entry.fabric().clone().into(),
+                );
+            }
+        }
+
+        ifaces
+    }
+
     /// Method: Return all FRR daemons that need to be enabled for this fabric 
configuration
     /// instance.
     ///
-- 
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