Refactorize VM information management
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Pierre HUBERT 2024-11-30 10:59:38 +01:00
parent 184a106542
commit 09f54bf3c1
3 changed files with 30 additions and 23 deletions

View File

@ -2,7 +2,7 @@ use crate::app_config::AppConfig;
use crate::controllers::HttpResult;
use crate::extractors::auth_extractor::AuthExtractor;
use crate::virtweb_client;
use crate::virtweb_client::VMUuid;
use crate::virtweb_client::VMInfo;
use actix_web::HttpResponse;
#[derive(serde::Serialize)]
@ -20,18 +20,29 @@ pub async fn config(auth: AuthExtractor) -> HttpResult {
#[derive(Default, Debug, serde::Serialize)]
pub struct Rights {
groups: Vec<GroupInfo>,
vms: Vec<VMInfoAndCaps>,
sys_info: bool,
}
#[derive(Debug, serde::Serialize)]
pub struct VMInfoAndCaps {
uiid: VMUuid,
pub struct GroupInfo {
name: String,
description: Option<String>,
architecture: String,
memory: usize,
number_vcpu: usize,
vms: Vec<VMInfo>,
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,
}
#[derive(Debug, serde::Serialize)]
pub struct VMInfoAndCaps {
#[serde(flatten)]
info: VMInfo,
can_get_state: bool,
can_start: bool,
can_shutdown: bool,
@ -46,6 +57,7 @@ pub async fn rights() -> HttpResult {
let rights = virtweb_client::get_token_info().await?;
let mut res = Rights {
groups: vec![],
vms: vec![],
sys_info: rights.can_retrieve_system_info(),
};
@ -54,12 +66,7 @@ pub async fn rights() -> HttpResult {
let vm_info = virtweb_client::vm_info(v).await?;
res.vms.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,
info: vm_info,
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()),

View File

@ -69,7 +69,7 @@ pub struct TokenClaims {
pub nonce: String,
}
#[derive(serde::Deserialize, Debug)]
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct VMInfo {
pub uuid: VMUuid,
pub name: String,

View File

@ -1,7 +1,7 @@
import { APIClient } from "./ApiClient";
export interface VMInfo {
uiid: string;
uuid: string;
name: string;
description?: string;
architecture: string;
@ -34,7 +34,7 @@ export class VMApi {
*/
static async State(vm: VMInfo): Promise<VMState> {
return (
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uiid}/state` })
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uuid}/state` })
).data.state;
}
@ -42,42 +42,42 @@ export class VMApi {
* Request to start VM
*/
static async StartVM(vm: VMInfo): Promise<void> {
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uiid}/start` });
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uuid}/start` });
}
/**
* Request to suspend VM
*/
static async SuspendVM(vm: VMInfo): Promise<void> {
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uiid}/suspend` });
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uuid}/suspend` });
}
/**
* Request to resume VM
*/
static async ResumeVM(vm: VMInfo): Promise<void> {
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uiid}/resume` });
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uuid}/resume` });
}
/**
* Request to shutdown VM
*/
static async ShutdownVM(vm: VMInfo): Promise<void> {
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uiid}/shutdown` });
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uuid}/shutdown` });
}
/**
* Request to kill VM
*/
static async KillVM(vm: VMInfo): Promise<void> {
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uiid}/kill` });
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uuid}/kill` });
}
/**
* Request to reset VM
*/
static async ResetVM(vm: VMInfo): Promise<void> {
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uiid}/reset` });
await APIClient.exec({ method: "GET", uri: `/vm/${vm.uuid}/reset` });
}
/**
@ -86,7 +86,7 @@ export class VMApi {
static async Screenshot(vm: VMInfo): Promise<Blob> {
return (
await APIClient.exec({
uri: `/vm/${vm.uiid}/screenshot`,
uri: `/vm/${vm.uuid}/screenshot`,
method: "GET",
})
).data;