Can suspend and resume VMs

This commit is contained in:
Pierre HUBERT 2024-11-25 21:11:33 +01:00
parent 4b97fc498f
commit 200ccd764d
2 changed files with 66 additions and 11 deletions

View File

@ -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))
}

View File

@ -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