2024-11-02 16:44:10 +00:00
|
|
|
use crate::controllers::{HttpResult, LibVirtReq};
|
2024-11-28 18:06:20 +00:00
|
|
|
use crate::extractors::group_vm_id_extractor::GroupVmIdExtractor;
|
|
|
|
use crate::libvirt_rest_structures::vm::VMInfo;
|
2024-11-02 16:44:10 +00:00
|
|
|
use actix_web::HttpResponse;
|
2024-11-28 18:06:20 +00:00
|
|
|
use std::collections::HashMap;
|
2024-11-02 16:44:10 +00:00
|
|
|
|
|
|
|
/// Get the list of groups
|
|
|
|
pub async fn list(client: LibVirtReq) -> HttpResult {
|
|
|
|
let groups = match client.get_full_groups_list().await {
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("Failed to get the list of groups! {e}");
|
|
|
|
return Ok(HttpResponse::InternalServerError()
|
|
|
|
.json(format!("Failed to get the list of groups! {e}")));
|
|
|
|
}
|
|
|
|
Ok(l) => l,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(groups))
|
|
|
|
}
|
2024-11-28 18:06:20 +00:00
|
|
|
|
|
|
|
/// Get information about the VMs of a group
|
|
|
|
pub async fn vm_info(vms_xml: GroupVmIdExtractor) -> HttpResult {
|
|
|
|
let mut vms = Vec::new();
|
|
|
|
for vm in vms_xml.0 {
|
|
|
|
vms.push(VMInfo::from_domain(vm)?)
|
|
|
|
}
|
|
|
|
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: GroupVmIdExtractor) -> HttpResult {
|
|
|
|
let mut res = TreatmentResult::default();
|
|
|
|
for vm in vms.0 {
|
|
|
|
if let Some(uuid) = vm.uuid {
|
|
|
|
match client.start_domain(uuid).await {
|
|
|
|
Ok(_) => res.ok += 1,
|
|
|
|
Err(_) => res.failed += 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(HttpResponse::Ok().json(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shutdown the VMs of a group
|
|
|
|
pub async fn vm_shutdown(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult {
|
|
|
|
let mut res = TreatmentResult::default();
|
|
|
|
for vm in vms.0 {
|
|
|
|
if let Some(uuid) = vm.uuid {
|
|
|
|
match client.shutdown_domain(uuid).await {
|
|
|
|
Ok(_) => res.ok += 1,
|
|
|
|
Err(_) => res.failed += 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(HttpResponse::Ok().json(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Suspend the VMs of a group
|
|
|
|
pub async fn vm_suspend(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult {
|
|
|
|
let mut res = TreatmentResult::default();
|
|
|
|
for vm in vms.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: GroupVmIdExtractor) -> HttpResult {
|
|
|
|
let mut res = TreatmentResult::default();
|
|
|
|
for vm in vms.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: GroupVmIdExtractor) -> HttpResult {
|
|
|
|
let mut res = TreatmentResult::default();
|
|
|
|
for vm in vms.0 {
|
|
|
|
if let Some(uuid) = vm.uuid {
|
|
|
|
match client.kill_domain(uuid).await {
|
|
|
|
Ok(_) => res.ok += 1,
|
|
|
|
Err(_) => res.failed += 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(HttpResponse::Ok().json(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reset the VMs of a group
|
|
|
|
pub async fn vm_reset(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult {
|
|
|
|
let mut res = TreatmentResult::default();
|
|
|
|
for vm in vms.0 {
|
|
|
|
if let Some(uuid) = vm.uuid {
|
|
|
|
match client.reset_domain(uuid).await {
|
|
|
|
Ok(_) => res.ok += 1,
|
|
|
|
Err(_) => res.failed += 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(HttpResponse::Ok().json(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the screenshot of the VMs of a group
|
|
|
|
pub async fn vm_screenshot(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult {
|
|
|
|
if vms.0.is_empty() {
|
|
|
|
return Ok(HttpResponse::NoContent().finish());
|
|
|
|
}
|
|
|
|
|
|
|
|
let image = if vms.0.len() == 1 {
|
|
|
|
client.screenshot_domain(vms.0[0].uuid.unwrap()).await?
|
|
|
|
} else {
|
|
|
|
return Ok(
|
|
|
|
HttpResponse::UnprocessableEntity().json("Cannot return multiple VM screenshots!!")
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().content_type("image/png").body(image))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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, client.get_domain_state(uuid).await?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(states))
|
|
|
|
}
|