Add routes to manipulate VM

This commit is contained in:
2024-05-03 21:07:30 +02:00
parent 50600e4e56
commit 14c95ac4f7
3 changed files with 138 additions and 9 deletions

View File

@ -3,7 +3,7 @@
use crate::controllers::HttpResult;
use crate::virtweb_client;
use crate::virtweb_client::VMUuid;
use actix_web::HttpResponse;
use actix_web::{web, HttpResponse};
#[derive(Debug, serde::Serialize)]
pub struct VMInfoAndCaps {
@ -27,7 +27,7 @@ pub async fn list() -> HttpResult {
let mut res = vec![];
for v in rights.list_vm() {
let vm_info = virtweb_client::get_vm_info(v).await?;
let vm_info = virtweb_client::vm_info(v).await?;
res.push(VMInfoAndCaps {
uiid: vm_info.uuid,
@ -46,3 +46,57 @@ pub async fn list() -> HttpResult {
Ok(HttpResponse::Ok().json(res))
}
#[derive(serde::Deserialize)]
pub struct ReqPath {
uid: VMUuid,
}
/// Get the state of a VM
pub async fn state(path: web::Path<ReqPath>) -> HttpResult {
Ok(HttpResponse::Ok().json(virtweb_client::vm_state(path.uid).await?))
}
/// Start a VM
pub async fn start(path: web::Path<ReqPath>) -> HttpResult {
virtweb_client::vm_start(path.uid).await?;
Ok(HttpResponse::Ok().finish())
}
/// Shutdown a VM
pub async fn shutdown(path: web::Path<ReqPath>) -> HttpResult {
virtweb_client::vm_shutdown(path.uid).await?;
Ok(HttpResponse::Ok().finish())
}
/// Kill a VM
pub async fn kill(path: web::Path<ReqPath>) -> HttpResult {
virtweb_client::vm_kill(path.uid).await?;
Ok(HttpResponse::Ok().finish())
}
/// Reset a VM
pub async fn reset(path: web::Path<ReqPath>) -> HttpResult {
virtweb_client::vm_reset(path.uid).await?;
Ok(HttpResponse::Ok().finish())
}
/// Suspend a VM
pub async fn suspend(path: web::Path<ReqPath>) -> HttpResult {
virtweb_client::vm_suspend(path.uid).await?;
Ok(HttpResponse::Ok().finish())
}
/// Resume a VM
pub async fn resume(path: web::Path<ReqPath>) -> HttpResult {
virtweb_client::vm_resume(path.uid).await?;
Ok(HttpResponse::Ok().finish())
}
/// Take the screenshot of a VM
pub async fn screenshot(path: web::Path<ReqPath>) -> HttpResult {
let screenshot = virtweb_client::vm_screenshot(path.uid).await?;
Ok(HttpResponse::Ok()
.insert_header(("content-type", "image/png"))
.body(screenshot))
}