From a8a75328a9b64eebf5ea56e99a603f25753c99ea Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 30 Nov 2024 11:22:32 +0100 Subject: [PATCH] Get information about the VM of a group --- .../src/controllers/server_controller.rs | 21 ++++++++++++++-- remote_backend/src/virtweb_client.rs | 24 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/remote_backend/src/controllers/server_controller.rs b/remote_backend/src/controllers/server_controller.rs index c66e1de..38bbf81 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::VMInfo; +use crate::virtweb_client::{GroupID, VMInfo}; use actix_web::HttpResponse; #[derive(serde::Serialize)] @@ -27,7 +27,7 @@ pub struct Rights { #[derive(Debug, serde::Serialize)] pub struct GroupInfo { - name: String, + id: GroupID, vms: Vec, can_get_state: bool, can_start: bool, @@ -62,6 +62,23 @@ pub async fn rights() -> HttpResult { sys_info: rights.can_retrieve_system_info(), }; + for g in rights.list_groups() { + let group_vms = virtweb_client::group_vm_info(&g).await?; + + res.groups.push(GroupInfo { + id: g, + vms: group_vms, + can_get_state: false, //TODO + can_start: false, + can_shutdown: false, + can_kill: false, + can_reset: false, + can_suspend: false, + can_resume: false, + can_screenshot: false, + }) + } + for v in rights.list_vm() { let vm_info = virtweb_client::vm_info(v).await?; diff --git a/remote_backend/src/virtweb_client.rs b/remote_backend/src/virtweb_client.rs index 1f5e33e..d2e9dd9 100644 --- a/remote_backend/src/virtweb_client.rs +++ b/remote_backend/src/virtweb_client.rs @@ -12,6 +12,15 @@ pub enum VirtWebClientError { InvalidStatusCode(u16), } +#[derive(Eq, PartialEq, Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct GroupID(String); + +impl GroupID { + pub fn route_vm_info(&self) -> String { + format!("/api/group/{}/vm/info", self.0) + } +} + #[derive(Eq, PartialEq, Debug, Copy, Clone, serde::Serialize, serde::Deserialize)] pub struct VMUuid(Uuid); @@ -147,6 +156,16 @@ impl TokenInfo { false } + /// List the groups with access + pub fn list_groups(&self) -> Vec { + self.rights + .iter() + .filter(|r| r.verb == "GET") + .filter(|r| regex!("^/api/group/[^/]+/vm/info$").is_match(&r.path)) + .map(|r| GroupID(r.path.split("/").nth(3).unwrap().to_string())) + .collect::>() + } + /// List the virtual machines with access pub fn list_vm(&self) -> Vec { self.rights @@ -260,6 +279,11 @@ pub async fn vm_screenshot(id: VMUuid) -> anyhow::Result> { .to_vec()) } +/// Get the VM of a group +pub async fn group_vm_info(id: &GroupID) -> anyhow::Result> { + json_request(id.route_vm_info()).await +} + /// Get current server information pub async fn get_server_info() -> anyhow::Result { json_request("/api/server/info").await