From 200ccd764deb6e753a51ccdb22a060dcbf874d84 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 25 Nov 2024 21:11:33 +0100 Subject: [PATCH] Can suspend and resume VMs --- .../src/controllers/groups_controller.rs | 66 ++++++++++++++++--- virtweb_backend/src/main.rs | 11 +++- 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/virtweb_backend/src/controllers/groups_controller.rs b/virtweb_backend/src/controllers/groups_controller.rs index b041454..ae096f5 100644 --- a/virtweb_backend/src/controllers/groups_controller.rs +++ b/virtweb_backend/src/controllers/groups_controller.rs @@ -26,42 +26,92 @@ pub async fn vm_info(vms_ids: GroupVmIdExtractor) -> HttpResult { Ok(HttpResponse::Ok().json(vms)) } +#[derive(Default, serde::Serialize)] +pub struct TreatmentResult { + ok: usize, + failed: usize, +} + /// Start the VMs of a group pub async fn vm_start(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { + let mut res = TreatmentResult::default(); for vm in vms_ids.0 { if let Some(uuid) = vm.uuid { - client.start_domain(uuid).await?; + match client.start_domain(uuid).await { + Ok(_) => res.ok += 1, + Err(_) => res.failed += 1, + } } } - Ok(HttpResponse::Ok().finish()) + Ok(HttpResponse::Ok().json(res)) } /// Shutdown the VMs of a group pub async fn vm_shutdown(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { + let mut res = TreatmentResult::default(); for vm in vms_ids.0 { if let Some(uuid) = vm.uuid { - client.shutdown_domain(uuid).await?; + match client.shutdown_domain(uuid).await { + Ok(_) => res.ok += 1, + Err(_) => res.failed += 1, + } } } - Ok(HttpResponse::Ok().finish()) + Ok(HttpResponse::Ok().json(res)) +} + +/// Suspend the VMs of a group +pub async fn vm_suspend(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { + let mut res = TreatmentResult::default(); + for vm in vms_ids.0 { + if let Some(uuid) = vm.uuid { + match client.suspend_domain(uuid).await { + Ok(_) => res.ok += 1, + Err(_) => res.failed += 1, + } + } + } + Ok(HttpResponse::Ok().json(res)) +} + +/// Resume the VMs of a group +pub async fn vm_resume(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { + let mut res = TreatmentResult::default(); + for vm in vms_ids.0 { + if let Some(uuid) = vm.uuid { + match client.resume_domain(uuid).await { + Ok(_) => res.ok += 1, + Err(_) => res.failed += 1, + } + } + } + Ok(HttpResponse::Ok().json(res)) } /// Kill the VMs of a group pub async fn vm_kill(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { + let mut res = TreatmentResult::default(); for vm in vms_ids.0 { if let Some(uuid) = vm.uuid { - client.kill_domain(uuid).await?; + match client.kill_domain(uuid).await { + Ok(_) => res.ok += 1, + Err(_) => res.failed += 1, + } } } - Ok(HttpResponse::Ok().finish()) + Ok(HttpResponse::Ok().json(res)) } /// Reset the VMs of a group pub async fn vm_reset(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { + let mut res = TreatmentResult::default(); for vm in vms_ids.0 { if let Some(uuid) = vm.uuid { - client.reset_domain(uuid).await?; + match client.reset_domain(uuid).await { + Ok(_) => res.ok += 1, + Err(_) => res.failed += 1, + } } } - Ok(HttpResponse::Ok().finish()) + Ok(HttpResponse::Ok().json(res)) } diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index 9f9a76d..7a4c13d 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -224,8 +224,14 @@ async fn main() -> std::io::Result<()> { "/api/group/{gid}/vm/shutdown", web::get().to(groups_controller::vm_shutdown), ) - // TODO suspend VM - // TODO resume VM + .route( + "/api/group/{gid}/vm/suspend", + web::get().to(groups_controller::vm_suspend), + ) + .route( + "/api/group/{gid}/vm/resume", + web::get().to(groups_controller::vm_resume), + ) .route( "/api/group/{gid}/vm/kill", web::get().to(groups_controller::vm_kill), @@ -234,7 +240,6 @@ async fn main() -> std::io::Result<()> { "/api/group/{gid}/vm/reset", web::get().to(groups_controller::vm_reset), ) - // TODO reset VM // TODO screenshot VM // TODO state of VM // Network controller