From 7eced6b8b5dc52420c4701eac19f01b407bac43e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 25 Nov 2024 21:26:28 +0100 Subject: [PATCH] Can get the state of VMs of a group --- .../src/controllers/groups_controller.rs | 42 ++++++++++++------- .../src/libvirt_lib_structures/mod.rs | 2 +- virtweb_backend/src/main.rs | 5 ++- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/virtweb_backend/src/controllers/groups_controller.rs b/virtweb_backend/src/controllers/groups_controller.rs index ae096f5..bab92cc 100644 --- a/virtweb_backend/src/controllers/groups_controller.rs +++ b/virtweb_backend/src/controllers/groups_controller.rs @@ -2,6 +2,7 @@ use crate::controllers::{HttpResult, LibVirtReq}; use crate::extractors::group_vm_id_extractor::GroupVmIdExtractor; use crate::libvirt_rest_structures::vm::VMInfo; use actix_web::HttpResponse; +use std::collections::HashMap; /// Get the list of groups pub async fn list(client: LibVirtReq) -> HttpResult { @@ -18,9 +19,9 @@ pub async fn list(client: LibVirtReq) -> HttpResult { } /// Get information about the VMs of a group -pub async fn vm_info(vms_ids: GroupVmIdExtractor) -> HttpResult { +pub async fn vm_info(vms_xml: GroupVmIdExtractor) -> HttpResult { let mut vms = Vec::new(); - for vm in vms_ids.0 { + for vm in vms_xml.0 { vms.push(VMInfo::from_domain(vm)?) } Ok(HttpResponse::Ok().json(vms)) @@ -33,9 +34,9 @@ pub struct TreatmentResult { } /// Start the VMs of a group -pub async fn vm_start(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { +pub async fn vm_start(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult { let mut res = TreatmentResult::default(); - for vm in vms_ids.0 { + for vm in vms.0 { if let Some(uuid) = vm.uuid { match client.start_domain(uuid).await { Ok(_) => res.ok += 1, @@ -47,9 +48,9 @@ pub async fn vm_start(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpRe } /// Shutdown the VMs of a group -pub async fn vm_shutdown(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { +pub async fn vm_shutdown(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult { let mut res = TreatmentResult::default(); - for vm in vms_ids.0 { + for vm in vms.0 { if let Some(uuid) = vm.uuid { match client.shutdown_domain(uuid).await { Ok(_) => res.ok += 1, @@ -61,9 +62,9 @@ pub async fn vm_shutdown(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> Htt } /// Suspend the VMs of a group -pub async fn vm_suspend(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { +pub async fn vm_suspend(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult { let mut res = TreatmentResult::default(); - for vm in vms_ids.0 { + for vm in vms.0 { if let Some(uuid) = vm.uuid { match client.suspend_domain(uuid).await { Ok(_) => res.ok += 1, @@ -75,9 +76,9 @@ pub async fn vm_suspend(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> Http } /// Resume the VMs of a group -pub async fn vm_resume(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { +pub async fn vm_resume(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult { let mut res = TreatmentResult::default(); - for vm in vms_ids.0 { + for vm in vms.0 { if let Some(uuid) = vm.uuid { match client.resume_domain(uuid).await { Ok(_) => res.ok += 1, @@ -89,9 +90,9 @@ pub async fn vm_resume(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpR } /// Kill the VMs of a group -pub async fn vm_kill(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { +pub async fn vm_kill(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult { let mut res = TreatmentResult::default(); - for vm in vms_ids.0 { + for vm in vms.0 { if let Some(uuid) = vm.uuid { match client.kill_domain(uuid).await { Ok(_) => res.ok += 1, @@ -103,9 +104,9 @@ pub async fn vm_kill(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpRes } /// Reset the VMs of a group -pub async fn vm_reset(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { +pub async fn vm_reset(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult { let mut res = TreatmentResult::default(); - for vm in vms_ids.0 { + for vm in vms.0 { if let Some(uuid) = vm.uuid { match client.reset_domain(uuid).await { Ok(_) => res.ok += 1, @@ -115,3 +116,16 @@ pub async fn vm_reset(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpRe } Ok(HttpResponse::Ok().json(res)) } + +/// Get the state of the VMs +pub async fn vm_state(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult { + let mut states = HashMap::new(); + + for vm in vms.0 { + if let Some(uuid) = vm.uuid { + states.insert(uuid.clone(), client.get_domain_state(uuid).await?); + } + } + + Ok(HttpResponse::Ok().json(states)) +} diff --git a/virtweb_backend/src/libvirt_lib_structures/mod.rs b/virtweb_backend/src/libvirt_lib_structures/mod.rs index 65e7491..8fbca27 100644 --- a/virtweb_backend/src/libvirt_lib_structures/mod.rs +++ b/virtweb_backend/src/libvirt_lib_structures/mod.rs @@ -1,4 +1,4 @@ -#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, Debug, Eq, PartialEq)] +#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, Debug, Eq, PartialEq, Hash)] pub struct XMLUuid(pub uuid::Uuid); impl XMLUuid { diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index 7a4c13d..a8e18f2 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -241,7 +241,10 @@ async fn main() -> std::io::Result<()> { web::get().to(groups_controller::vm_reset), ) // TODO screenshot VM - // TODO state of VM + .route( + "/api/group/{gid}/vm/state", + web::get().to(groups_controller::vm_state), + ) // Network controller .route( "/api/network/create",