diff --git a/virtweb_backend/src/actors/libvirt_actor.rs b/virtweb_backend/src/actors/libvirt_actor.rs index d5e668a..aac4de1 100644 --- a/virtweb_backend/src/actors/libvirt_actor.rs +++ b/virtweb_backend/src/actors/libvirt_actor.rs @@ -120,32 +120,8 @@ pub struct DefineDomainReq(pub DomainXML); impl Handler for LibVirtActor { type Result = anyhow::Result; - fn handle(&mut self, mut msg: DefineDomainReq, _ctx: &mut Self::Context) -> Self::Result { - // A issue with the disks & network interface definition serialization needs them to be serialized aside - 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("", &format!("{disks_xml}"), 1); + fn handle(&mut self, msg: DefineDomainReq, _ctx: &mut Self::Context) -> Self::Result { + let xml = msg.0.into_xml()?; log::debug!("Define domain:\n{}", xml); let domain = Domain::define_xml(&self.m, &xml)?; @@ -389,26 +365,11 @@ pub struct DefineNetwork(pub NetworkXML); impl Handler for LibVirtActor { type Result = anyhow::Result; - 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); - // 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("", &format!("{ips_xml}"), 1); - log::debug!("Source network structure: {:#?}", msg.0); + let network_xml = msg.0.into_xml()?; log::debug!("Define network XML: {network_xml}"); let network = Network::define_xml(&self.m, &network_xml)?; diff --git a/virtweb_backend/src/libvirt_lib_structures.rs b/virtweb_backend/src/libvirt_lib_structures.rs index addadab..f348603 100644 --- a/virtweb_backend/src/libvirt_lib_structures.rs +++ b/virtweb_backend/src/libvirt_lib_structures.rs @@ -318,6 +318,38 @@ pub struct DomainXML { pub on_crash: String, } +impl DomainXML { + /// Turn this domain into its XML definition + pub fn into_xml(mut self) -> anyhow::Result { + // 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("", &format!("{disks_xml}"), 1); + Ok(xml) + } +} + /// Domain state #[derive(serde::Serialize, Debug, Copy, Clone)] pub enum DomainState { @@ -435,3 +467,24 @@ pub struct NetworkXML { #[serde(default, rename = "ip")] pub ips: Vec, } + +impl NetworkXML { + pub fn into_xml(mut self) -> anyhow::Result { + // 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("", &format!("{ips_xml}"), 1); + Ok(network_xml) + } +}