From 09f54bf3c15fc8ed07a05b71cdc48486ba6aeefc Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 30 Nov 2024 10:59:38 +0100 Subject: [PATCH] Refactorize VM information management --- .../src/controllers/server_controller.rs | 33 +++++++++++-------- remote_backend/src/virtweb_client.rs | 2 +- remote_frontend/src/api/VMApi.ts | 18 +++++----- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/remote_backend/src/controllers/server_controller.rs b/remote_backend/src/controllers/server_controller.rs index 165174b..c66e1de 100644 --- a/remote_backend/src/controllers/server_controller.rs +++ b/remote_backend/src/controllers/server_controller.rs @@ -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, vms: Vec, sys_info: bool, } #[derive(Debug, serde::Serialize)] -pub struct VMInfoAndCaps { - uiid: VMUuid, +pub struct GroupInfo { name: String, - description: Option, - architecture: String, - memory: usize, - number_vcpu: usize, + vms: Vec, + 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()), diff --git a/remote_backend/src/virtweb_client.rs b/remote_backend/src/virtweb_client.rs index 4b5c5ba..1f5e33e 100644 --- a/remote_backend/src/virtweb_client.rs +++ b/remote_backend/src/virtweb_client.rs @@ -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, diff --git a/remote_frontend/src/api/VMApi.ts b/remote_frontend/src/api/VMApi.ts index 03b0c9d..16fcace 100644 --- a/remote_frontend/src/api/VMApi.ts +++ b/remote_frontend/src/api/VMApi.ts @@ -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 { 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { return ( await APIClient.exec({ - uri: `/vm/${vm.uiid}/screenshot`, + uri: `/vm/${vm.uuid}/screenshot`, method: "GET", }) ).data;