diff --git a/virtweb_backend/src/controllers/groups_controller.rs b/virtweb_backend/src/controllers/groups_controller.rs index b61cc25..b041454 100644 --- a/virtweb_backend/src/controllers/groups_controller.rs +++ b/virtweb_backend/src/controllers/groups_controller.rs @@ -17,7 +17,7 @@ pub async fn list(client: LibVirtReq) -> HttpResult { Ok(HttpResponse::Ok().json(groups)) } -/// Get information about a VM +/// Get information about the VMs of a group pub async fn vm_info(vms_ids: GroupVmIdExtractor) -> HttpResult { let mut vms = Vec::new(); for vm in vms_ids.0 { @@ -25,3 +25,43 @@ pub async fn vm_info(vms_ids: GroupVmIdExtractor) -> HttpResult { } Ok(HttpResponse::Ok().json(vms)) } + +/// Start the VMs of a group +pub async fn vm_start(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { + for vm in vms_ids.0 { + if let Some(uuid) = vm.uuid { + client.start_domain(uuid).await?; + } + } + Ok(HttpResponse::Ok().finish()) +} + +/// Shutdown the VMs of a group +pub async fn vm_shutdown(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { + for vm in vms_ids.0 { + if let Some(uuid) = vm.uuid { + client.shutdown_domain(uuid).await?; + } + } + Ok(HttpResponse::Ok().finish()) +} + +/// Kill the VMs of a group +pub async fn vm_kill(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { + for vm in vms_ids.0 { + if let Some(uuid) = vm.uuid { + client.kill_domain(uuid).await?; + } + } + Ok(HttpResponse::Ok().finish()) +} + +/// Reset the VMs of a group +pub async fn vm_reset(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult { + for vm in vms_ids.0 { + if let Some(uuid) = vm.uuid { + client.reset_domain(uuid).await?; + } + } + Ok(HttpResponse::Ok().finish()) +} diff --git a/virtweb_backend/src/extractors/group_vm_id_extractor.rs b/virtweb_backend/src/extractors/group_vm_id_extractor.rs index ac85cf0..b5b823d 100644 --- a/virtweb_backend/src/extractors/group_vm_id_extractor.rs +++ b/virtweb_backend/src/extractors/group_vm_id_extractor.rs @@ -51,8 +51,8 @@ impl FromRequest for GroupVmIdExtractor { let vms = match client.get_full_group_vm_list(&group_id).await { Ok(vms) => vms, Err(e) => { - log::error!("Failed to get the VM of the group {group_id:?}: {e}"); - return Err(ErrorBadRequest("Failed to get the VM of the group!")); + log::error!("Failed to get the VMs of the group {group_id:?}: {e}"); + return Err(ErrorBadRequest("Failed to get the VMs of the group!")); } }; diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index 0cf3a50..9f9a76d 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -216,13 +216,27 @@ async fn main() -> std::io::Result<()> { "/api/group/{gid}/vm/info", web::get().to(groups_controller::vm_info), ) - // TODO start VM - // TODO stop VM + .route( + "/api/group/{gid}/vm/start", + web::get().to(groups_controller::vm_start), + ) + .route( + "/api/group/{gid}/vm/shutdown", + web::get().to(groups_controller::vm_shutdown), + ) // TODO suspend VM // TODO resume VM - // TODO kill VM + .route( + "/api/group/{gid}/vm/kill", + web::get().to(groups_controller::vm_kill), + ) + .route( + "/api/group/{gid}/vm/reset", + web::get().to(groups_controller::vm_reset), + ) // TODO reset VM // TODO screenshot VM + // TODO state of VM // Network controller .route( "/api/network/create",