From 52d7e35d10a6d584d05c260260580ca22310e87e Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 13 Oct 2023 15:30:32 +0200 Subject: [PATCH] Can get domain state through a dedicated route --- virtweb_backend/src/controllers/vm_controller.rs | 16 ++++++++++++++++ virtweb_backend/src/main.rs | 1 + 2 files changed, 17 insertions(+) diff --git a/virtweb_backend/src/controllers/vm_controller.rs b/virtweb_backend/src/controllers/vm_controller.rs index 3f203c1..8b72424 100644 --- a/virtweb_backend/src/controllers/vm_controller.rs +++ b/virtweb_backend/src/controllers/vm_controller.rs @@ -131,6 +131,22 @@ pub async fn resume(client: LibVirtReq, id: web::Path) -> HttpR }) } +#[derive(serde::Serialize)] +struct DomainStateRes { + state: DomainState, +} + +/// Get the state of a VM +pub async fn state(client: LibVirtReq, id: web::Path) -> HttpResult { + Ok(match client.get_domain_state(id.uid).await { + Ok(s) => HttpResponse::Ok().json(DomainStateRes { state: s }), + Err(e) => { + log::error!("Failed to get domain state {:?} ! {e}", id.uid); + HttpResponse::InternalServerError().json("Failed to get domain state!") + } + }) +} + /// Take a screenshot of a VM pub async fn screenshot(client: LibVirtReq, id: web::Path) -> HttpResult { Ok(match client.screenshot_domain(id.uid).await { diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index c2867ae..ee8698b 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -150,6 +150,7 @@ async fn main() -> std::io::Result<()> { web::get().to(vm_controller::suspend), ) .route("/api/vm/{uid}/resume", web::get().to(vm_controller::resume)) + .route("/api/vm/{uid}/state", web::get().to(vm_controller::state)) .route( "/api/vm/{uid}/screenshot", web::get().to(vm_controller::screenshot),