From 9a99e0b54e4b2a3234b74efdab93fef182a16904 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Tue, 31 Oct 2023 12:03:37 +0100 Subject: [PATCH] Can update and delete networks --- virtweb_backend/src/actors/libvirt_actor.rs | 15 ++++++++++++++ .../src/controllers/network_controller.rs | 20 +++++++++++++++++++ virtweb_backend/src/libvirt_client.rs | 5 +++++ virtweb_backend/src/main.rs | 8 ++++++++ 4 files changed, 48 insertions(+) diff --git a/virtweb_backend/src/actors/libvirt_actor.rs b/virtweb_backend/src/actors/libvirt_actor.rs index 7a9380f..45f90d9 100644 --- a/virtweb_backend/src/actors/libvirt_actor.rs +++ b/virtweb_backend/src/actors/libvirt_actor.rs @@ -413,3 +413,18 @@ impl Handler for LibVirtActor { Ok(serde_xml_rs::from_str(&xml)?) } } + +#[derive(Message)] +#[rtype(result = "anyhow::Result<()>")] +pub struct DeleteNetwork(pub XMLUuid); + +impl Handler for LibVirtActor { + type Result = anyhow::Result<()>; + + fn handle(&mut self, msg: DeleteNetwork, _ctx: &mut Self::Context) -> Self::Result { + log::debug!("Delete network: {}\n", msg.0.as_string()); + let network = Network::lookup_by_uuid_string(&self.m, &msg.0.as_string())?; + network.undefine()?; + Ok(()) + } +} diff --git a/virtweb_backend/src/controllers/network_controller.rs b/virtweb_backend/src/controllers/network_controller.rs index 240e021..7c7dbc3 100644 --- a/virtweb_backend/src/controllers/network_controller.rs +++ b/virtweb_backend/src/controllers/network_controller.rs @@ -40,3 +40,23 @@ pub async fn get_single(client: LibVirtReq, req: web::Path) -> HttpRe Ok(HttpResponse::Ok().json(network)) } + +/// Update the information about a single network +pub async fn update( + client: LibVirtReq, + path: web::Path, + body: web::Json, +) -> HttpResult { + let mut network = body.0.to_virt_network()?; + network.uuid = Some(path.uid); + client.update_network(network).await?; + + Ok(HttpResponse::Ok().json("Network updated")) +} + +/// Delete a network +pub async fn delete(client: LibVirtReq, path: web::Path) -> HttpResult { + client.delete_network(path.uid).await?; + + Ok(HttpResponse::Ok().json("Network deleted")) +} diff --git a/virtweb_backend/src/libvirt_client.rs b/virtweb_backend/src/libvirt_client.rs index 6a3561e..29234ef 100644 --- a/virtweb_backend/src/libvirt_client.rs +++ b/virtweb_backend/src/libvirt_client.rs @@ -111,4 +111,9 @@ impl LibVirtClient { pub async fn get_single_network(&self, id: XMLUuid) -> anyhow::Result { self.0.send(libvirt_actor::GetNetworkXMLReq(id)).await? } + + /// Delete a network + pub async fn delete_network(&self, id: XMLUuid) -> anyhow::Result<()> { + self.0.send(libvirt_actor::DeleteNetwork(id)).await? + } } diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index 70fa907..1e8272f 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -186,6 +186,14 @@ async fn main() -> std::io::Result<()> { "/api/network/{uid}", web::get().to(network_controller::get_single), ) + .route( + "/api/network/{uid}", + web::put().to(network_controller::update), + ) + .route( + "/api/network/{uid}", + web::delete().to(network_controller::delete), + ) }) .bind(&AppConfig::get().listen_address)? .run()