From 246f5ef842b6dae99ef62b12db5a3b23343139ad Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Thu, 28 Dec 2023 19:41:20 +0100 Subject: [PATCH] WIP building REST structures --- .../src/libvirt_rest_structures/mod.rs | 1 - .../src/libvirt_rest_structures/nw_filter.rs | 70 ++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/virtweb_backend/src/libvirt_rest_structures/mod.rs b/virtweb_backend/src/libvirt_rest_structures/mod.rs index cb17c8c..d97dea0 100644 --- a/virtweb_backend/src/libvirt_rest_structures/mod.rs +++ b/virtweb_backend/src/libvirt_rest_structures/mod.rs @@ -9,7 +9,6 @@ enum LibVirtStructError { StructureExtraction(&'static str), #[error("DomainExtractionError: {0}")] DomainExtraction(String), - #[error("ParseFilteringChain: {0}")] ParseFilteringChain(String), } diff --git a/virtweb_backend/src/libvirt_rest_structures/nw_filter.rs b/virtweb_backend/src/libvirt_rest_structures/nw_filter.rs index 5cc494c..1132600 100644 --- a/virtweb_backend/src/libvirt_rest_structures/nw_filter.rs +++ b/virtweb_backend/src/libvirt_rest_structures/nw_filter.rs @@ -96,6 +96,20 @@ pub struct NetworkFilter { impl NetworkFilter { pub fn from_xml(xml: NetworkFilterXML) -> anyhow::Result { + let mut rules = Vec::with_capacity(xml.rules.len()); + for rule in &xml.rules { + let mut selectors = Vec::new(); + + // TODO : add other selectors + + rules.push(NetworkFilterRule { + action: NetworkFilterAction::from_xml(&rule.action)?, + direction: NetworkFilterDirection::from_xml(&rule.direction)?, + priority: rule.priority, + selectors, + }) + } + Ok(Self { name: xml.name, uuid: xml.uuid, @@ -106,7 +120,7 @@ impl NetworkFilter { .iter() .map(|i| i.filter.to_string()) .collect(), - rules: vec![], // TODO ! + rules, }) } } @@ -126,6 +140,35 @@ pub enum NetworkFilterAction { Continue, } +impl NetworkFilterAction { + pub fn from_xml(xml: &str) -> anyhow::Result { + Ok(match xml { + "drop" => Self::Drop, + "reject" => Self::Reject, + "accept" => Self::Accept, + "return" => Self::Return, + "continue" => Self::Continue, + s => { + return Err(LibVirtStructError::ParseFilteringChain(format!( + "Unkown filter action {s}!" + )) + .into()) + } + }) + } + + pub fn to_xml(&self) -> String { + match self { + Self::Drop => "drop", + Self::Reject => "reject", + Self::Accept => "accept", + Self::Return => "return", + Self::Continue => "continue", + } + .to_string() + } +} + #[derive(serde::Serialize, serde::Deserialize, Debug, Clone)] pub enum NetworkFilterDirection { In, @@ -133,6 +176,31 @@ pub enum NetworkFilterDirection { InOut, } +impl NetworkFilterDirection { + pub fn from_xml(xml: &str) -> anyhow::Result { + Ok(match xml { + "in" => Self::In, + "out" => Self::Out, + "inout" => Self::InOut, + s => { + return Err(LibVirtStructError::ParseFilteringChain(format!( + "Unkown filter direction {s}!" + )) + .into()) + } + }) + } + + pub fn to_xml(&self) -> String { + match self { + NetworkFilterDirection::In => "in", + NetworkFilterDirection::Out => "out", + NetworkFilterDirection::InOut => "inout", + } + .to_string() + } +} + #[derive(serde::Serialize, serde::Deserialize, Debug, Clone)] pub enum Layer4State { NEW,