Can get the state of VMs of a group

This commit is contained in:
Pierre HUBERT 2024-11-25 21:26:28 +01:00
parent 200ccd764d
commit 7eced6b8b5
3 changed files with 33 additions and 16 deletions

View File

@ -2,6 +2,7 @@ use crate::controllers::{HttpResult, LibVirtReq};
use crate::extractors::group_vm_id_extractor::GroupVmIdExtractor; use crate::extractors::group_vm_id_extractor::GroupVmIdExtractor;
use crate::libvirt_rest_structures::vm::VMInfo; use crate::libvirt_rest_structures::vm::VMInfo;
use actix_web::HttpResponse; use actix_web::HttpResponse;
use std::collections::HashMap;
/// Get the list of groups /// Get the list of groups
pub async fn list(client: LibVirtReq) -> HttpResult { 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 /// 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(); let mut vms = Vec::new();
for vm in vms_ids.0 { for vm in vms_xml.0 {
vms.push(VMInfo::from_domain(vm)?) vms.push(VMInfo::from_domain(vm)?)
} }
Ok(HttpResponse::Ok().json(vms)) Ok(HttpResponse::Ok().json(vms))
@ -33,9 +34,9 @@ pub struct TreatmentResult {
} }
/// Start the VMs of a group /// 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(); let mut res = TreatmentResult::default();
for vm in vms_ids.0 { for vm in vms.0 {
if let Some(uuid) = vm.uuid { if let Some(uuid) = vm.uuid {
match client.start_domain(uuid).await { match client.start_domain(uuid).await {
Ok(_) => res.ok += 1, 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 /// 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(); let mut res = TreatmentResult::default();
for vm in vms_ids.0 { for vm in vms.0 {
if let Some(uuid) = vm.uuid { if let Some(uuid) = vm.uuid {
match client.shutdown_domain(uuid).await { match client.shutdown_domain(uuid).await {
Ok(_) => res.ok += 1, 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 /// 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(); let mut res = TreatmentResult::default();
for vm in vms_ids.0 { for vm in vms.0 {
if let Some(uuid) = vm.uuid { if let Some(uuid) = vm.uuid {
match client.suspend_domain(uuid).await { match client.suspend_domain(uuid).await {
Ok(_) => res.ok += 1, 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 /// 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(); let mut res = TreatmentResult::default();
for vm in vms_ids.0 { for vm in vms.0 {
if let Some(uuid) = vm.uuid { if let Some(uuid) = vm.uuid {
match client.resume_domain(uuid).await { match client.resume_domain(uuid).await {
Ok(_) => res.ok += 1, 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 /// 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(); let mut res = TreatmentResult::default();
for vm in vms_ids.0 { for vm in vms.0 {
if let Some(uuid) = vm.uuid { if let Some(uuid) = vm.uuid {
match client.kill_domain(uuid).await { match client.kill_domain(uuid).await {
Ok(_) => res.ok += 1, 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 /// 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(); let mut res = TreatmentResult::default();
for vm in vms_ids.0 { for vm in vms.0 {
if let Some(uuid) = vm.uuid { if let Some(uuid) = vm.uuid {
match client.reset_domain(uuid).await { match client.reset_domain(uuid).await {
Ok(_) => res.ok += 1, Ok(_) => res.ok += 1,
@ -115,3 +116,16 @@ pub async fn vm_reset(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpRe
} }
Ok(HttpResponse::Ok().json(res)) 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))
}

View File

@ -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); pub struct XMLUuid(pub uuid::Uuid);
impl XMLUuid { impl XMLUuid {

View File

@ -241,7 +241,10 @@ async fn main() -> std::io::Result<()> {
web::get().to(groups_controller::vm_reset), web::get().to(groups_controller::vm_reset),
) )
// TODO screenshot VM // TODO screenshot VM
// TODO state of VM .route(
"/api/group/{gid}/vm/state",
web::get().to(groups_controller::vm_state),
)
// Network controller // Network controller
.route( .route(
"/api/network/create", "/api/network/create",