Refactor XML definitions serialization

This commit is contained in:
Pierre HUBERT 2023-12-19 16:33:47 +01:00
parent 924c972984
commit ed1bd806d7
2 changed files with 57 additions and 43 deletions

View File

@ -120,32 +120,8 @@ pub struct DefineDomainReq(pub DomainXML);
impl Handler<DefineDomainReq> for LibVirtActor { impl Handler<DefineDomainReq> for LibVirtActor {
type Result = anyhow::Result<XMLUuid>; type Result = anyhow::Result<XMLUuid>;
fn handle(&mut self, mut msg: DefineDomainReq, _ctx: &mut Self::Context) -> Self::Result { fn handle(&mut self, msg: DefineDomainReq, _ctx: &mut Self::Context) -> Self::Result {
// A issue with the disks & network interface definition serialization needs them to be serialized aside let xml = msg.0.into_xml()?;
let mut devices_xml = Vec::with_capacity(msg.0.devices.disks.len());
for disk in msg.0.devices.disks {
let disk_xml = serde_xml_rs::to_string(&disk)?;
let start_offset = disk_xml.find("<disk").unwrap();
devices_xml.push(disk_xml[start_offset..].to_string());
}
for network in msg.0.devices.net_interfaces {
let network_xml = serde_xml_rs::to_string(&network)?;
let start_offset = network_xml.find("<interface").unwrap();
devices_xml.push(network_xml[start_offset..].to_string());
}
for input in msg.0.devices.inputs {
let input_xml = serde_xml_rs::to_string(&input)?;
let start_offset = input_xml.find("<input").unwrap();
devices_xml.push(input_xml[start_offset..].to_string());
}
msg.0.devices.disks = vec![];
msg.0.devices.net_interfaces = vec![];
msg.0.devices.inputs = vec![];
let mut xml = serde_xml_rs::to_string(&msg.0)?;
let disks_xml = devices_xml.join("\n");
xml = xml.replacen("<devices>", &format!("<devices>{disks_xml}"), 1);
log::debug!("Define domain:\n{}", xml); log::debug!("Define domain:\n{}", xml);
let domain = Domain::define_xml(&self.m, &xml)?; let domain = Domain::define_xml(&self.m, &xml)?;
@ -389,26 +365,11 @@ pub struct DefineNetwork(pub NetworkXML);
impl Handler<DefineNetwork> for LibVirtActor { impl Handler<DefineNetwork> for LibVirtActor {
type Result = anyhow::Result<XMLUuid>; type Result = anyhow::Result<XMLUuid>;
fn handle(&mut self, mut msg: DefineNetwork, _ctx: &mut Self::Context) -> Self::Result { fn handle(&mut self, msg: DefineNetwork, _ctx: &mut Self::Context) -> Self::Result {
log::debug!("Define network: {:?}", msg.0); log::debug!("Define network: {:?}", msg.0);
// A issue with the IPs definition serialization needs them to be serialized aside
let mut ips_xml = Vec::with_capacity(msg.0.ips.len());
for ip in msg.0.ips {
log::debug!("Serialize {ip:?}");
let ip_xml = serde_xml_rs::to_string(&ip)?;
let start_offset = ip_xml.find("<ip").unwrap();
ips_xml.push(ip_xml[start_offset..].to_string());
}
msg.0.ips = vec![];
let mut network_xml = serde_xml_rs::to_string(&msg.0)?;
log::trace!("Serialize network XML start: {network_xml}");
let ips_xml = ips_xml.join("\n");
network_xml = network_xml.replacen("</network>", &format!("{ips_xml}</network>"), 1);
log::debug!("Source network structure: {:#?}", msg.0); log::debug!("Source network structure: {:#?}", msg.0);
let network_xml = msg.0.into_xml()?;
log::debug!("Define network XML: {network_xml}"); log::debug!("Define network XML: {network_xml}");
let network = Network::define_xml(&self.m, &network_xml)?; let network = Network::define_xml(&self.m, &network_xml)?;

View File

@ -318,6 +318,38 @@ pub struct DomainXML {
pub on_crash: String, pub on_crash: String,
} }
impl DomainXML {
/// Turn this domain into its XML definition
pub fn into_xml(mut self) -> anyhow::Result<String> {
// A issue with the disks & network interface definition serialization needs them to be serialized aside
let mut devices_xml = Vec::with_capacity(self.devices.disks.len());
for disk in self.devices.disks {
let disk_xml = serde_xml_rs::to_string(&disk)?;
let start_offset = disk_xml.find("<disk").unwrap();
devices_xml.push(disk_xml[start_offset..].to_string());
}
for network in self.devices.net_interfaces {
let network_xml = serde_xml_rs::to_string(&network)?;
let start_offset = network_xml.find("<interface").unwrap();
devices_xml.push(network_xml[start_offset..].to_string());
}
for input in self.devices.inputs {
let input_xml = serde_xml_rs::to_string(&input)?;
let start_offset = input_xml.find("<input").unwrap();
devices_xml.push(input_xml[start_offset..].to_string());
}
self.devices.disks = vec![];
self.devices.net_interfaces = vec![];
self.devices.inputs = vec![];
let mut xml = serde_xml_rs::to_string(&self)?;
let disks_xml = devices_xml.join("\n");
xml = xml.replacen("<devices>", &format!("<devices>{disks_xml}"), 1);
Ok(xml)
}
}
/// Domain state /// Domain state
#[derive(serde::Serialize, Debug, Copy, Clone)] #[derive(serde::Serialize, Debug, Copy, Clone)]
pub enum DomainState { pub enum DomainState {
@ -435,3 +467,24 @@ pub struct NetworkXML {
#[serde(default, rename = "ip")] #[serde(default, rename = "ip")]
pub ips: Vec<NetworkIPXML>, pub ips: Vec<NetworkIPXML>,
} }
impl NetworkXML {
pub fn into_xml(mut self) -> anyhow::Result<String> {
// A issue with the IPs definition serialization needs them to be serialized aside
let mut ips_xml = Vec::with_capacity(self.ips.len());
for ip in self.ips {
log::debug!("Serialize {ip:?}");
let ip_xml = serde_xml_rs::to_string(&ip)?;
let start_offset = ip_xml.find("<ip").unwrap();
ips_xml.push(ip_xml[start_offset..].to_string());
}
self.ips = vec![];
let mut network_xml = serde_xml_rs::to_string(&self)?;
log::trace!("Serialize network XML start: {network_xml}");
let ips_xml = ips_xml.join("\n");
network_xml = network_xml.replacen("</network>", &format!("{ips_xml}</network>"), 1);
Ok(network_xml)
}
}