109 lines
3.2 KiB
Rust
109 lines
3.2 KiB
Rust
//! # Virtual machines routes controller
|
|
|
|
use crate::controllers::HttpResult;
|
|
use crate::virtweb_client;
|
|
use crate::virtweb_client::VMUuid;
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
#[derive(Debug, serde::Serialize)]
|
|
pub struct VMInfoAndCaps {
|
|
uiid: VMUuid,
|
|
name: String,
|
|
description: Option<String>,
|
|
architecture: String,
|
|
memory: usize,
|
|
number_vcpu: usize,
|
|
can_get_state: bool,
|
|
can_start: bool,
|
|
can_shutdown: bool,
|
|
can_kill: bool,
|
|
can_reset: bool,
|
|
can_suspend: bool,
|
|
can_resume: bool,
|
|
can_screenshot: bool,
|
|
}
|
|
|
|
/// Get the list of VMs that can be controlled by VirtWeb remote
|
|
pub async fn list() -> HttpResult {
|
|
let rights = virtweb_client::get_token_info().await?;
|
|
|
|
let mut res = vec![];
|
|
|
|
for v in rights.list_vm() {
|
|
let vm_info = virtweb_client::vm_info(v).await?;
|
|
|
|
res.push(VMInfoAndCaps {
|
|
uiid: vm_info.uuid,
|
|
name: vm_info.name,
|
|
description: vm_info.description.clone(),
|
|
architecture: vm_info.architecture.to_string(),
|
|
memory: vm_info.memory,
|
|
number_vcpu: vm_info.number_vcpu,
|
|
can_get_state: rights.is_route_allowed("GET", &v.route_state()),
|
|
can_start: rights.is_route_allowed("GET", &v.route_start()),
|
|
can_shutdown: rights.is_route_allowed("GET", &v.route_shutdown()),
|
|
can_kill: rights.is_route_allowed("GET", &v.route_kill()),
|
|
can_reset: rights.is_route_allowed("GET", &v.route_reset()),
|
|
can_suspend: rights.is_route_allowed("GET", &v.route_suspend()),
|
|
can_resume: rights.is_route_allowed("GET", &v.route_resume()),
|
|
can_screenshot: rights.is_route_allowed("GET", &v.route_screenshot()),
|
|
})
|
|
}
|
|
|
|
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))
|
|
}
|