From b4f65a6703cfb55cbd4737418f8a1dd72e580368 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Tue, 2 Jan 2024 15:45:31 +0100 Subject: [PATCH] Can delete network filter --- virtweb_backend/src/actors/libvirt_actor.rs | 26 ++++++++++++++++++- .../src/controllers/nwfilter_controller.rs | 7 +++++ virtweb_backend/src/libvirt_client.rs | 5 ++++ virtweb_backend/src/main.rs | 4 +++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/virtweb_backend/src/actors/libvirt_actor.rs b/virtweb_backend/src/actors/libvirt_actor.rs index d77ff46..99d30ce 100644 --- a/virtweb_backend/src/actors/libvirt_actor.rs +++ b/virtweb_backend/src/actors/libvirt_actor.rs @@ -5,7 +5,7 @@ use crate::libvirt_lib_structures::nwfilter::*; use crate::libvirt_lib_structures::*; use crate::libvirt_rest_structures::hypervisor::*; use crate::libvirt_rest_structures::net::*; -use crate::libvirt_rest_structures::nw_filter::NetworkFilter; +use crate::libvirt_rest_structures::nw_filter::{NetworkFilter, NetworkFilterName}; use crate::libvirt_rest_structures::vm::*; use actix::{Actor, Context, Handler, Message}; use image::ImageOutputFormat; @@ -618,3 +618,27 @@ impl Handler for LibVirtActor { Ok(uuid) } } + +#[derive(Message)] +#[rtype(result = "anyhow::Result<()>")] +pub struct DeleteNetworkFilter(pub XMLUuid); + +impl Handler for LibVirtActor { + type Result = anyhow::Result<()>; + + fn handle(&mut self, msg: DeleteNetworkFilter, _ctx: &mut Self::Context) -> Self::Result { + log::debug!("Delete network filter: {}\n", msg.0.as_string()); + let nw_filter = NWFilter::lookup_by_uuid_string(&self.m, &msg.0.as_string())?; + let nw_filter_name = nw_filter.get_name()?; + nw_filter.undefine()?; + + // Remove backup definition + let backup_definition = + AppConfig::get().net_filter_definition_path(&NetworkFilterName(nw_filter_name)); + if backup_definition.exists() { + std::fs::remove_file(backup_definition)?; + } + + Ok(()) + } +} diff --git a/virtweb_backend/src/controllers/nwfilter_controller.rs b/virtweb_backend/src/controllers/nwfilter_controller.rs index 25c7aa4..39a51f0 100644 --- a/virtweb_backend/src/controllers/nwfilter_controller.rs +++ b/virtweb_backend/src/controllers/nwfilter_controller.rs @@ -91,3 +91,10 @@ pub async fn update( Ok(HttpResponse::Ok().json("Network filter updated")) } + +/// Delete a network filter +pub async fn delete(client: LibVirtReq, path: web::Path) -> HttpResult { + client.delete_network_filter(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 96043fc..ea10a9a 100644 --- a/virtweb_backend/src/libvirt_client.rs +++ b/virtweb_backend/src/libvirt_client.rs @@ -197,4 +197,9 @@ impl LibVirtClient { .send(libvirt_actor::DefineNWFilterReq(nwf_def, xml)) .await? } + + /// Delete a network filter + pub async fn delete_network_filter(&self, id: XMLUuid) -> anyhow::Result<()> { + self.0.send(libvirt_actor::DeleteNetworkFilter(id)).await? + } } diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index e73ed34..52b427c 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -256,6 +256,10 @@ async fn main() -> std::io::Result<()> { "/api/nwfilter/{uid}", web::put().to(nwfilter_controller::update), ) + .route( + "/api/nwfilter/{uid}", + web::delete().to(nwfilter_controller::delete), + ) // Static assets .route("/", web::get().to(static_controller::root_index)) .route(