WIP building REST structures

This commit is contained in:
Pierre HUBERT 2023-12-28 19:41:20 +01:00
parent 9d4f19822d
commit 246f5ef842
2 changed files with 69 additions and 2 deletions

View File

@ -9,7 +9,6 @@ enum LibVirtStructError {
StructureExtraction(&'static str),
#[error("DomainExtractionError: {0}")]
DomainExtraction(String),
#[error("ParseFilteringChain: {0}")]
ParseFilteringChain(String),
}

View File

@ -96,6 +96,20 @@ pub struct NetworkFilter {
impl NetworkFilter {
pub fn from_xml(xml: NetworkFilterXML) -> anyhow::Result<Self> {
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<Self> {
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<Self> {
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,