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

View File

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

View File

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