Get information about the VM of a group
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Pierre HUBERT 2024-11-30 11:22:32 +01:00
parent 09f54bf3c1
commit a8a75328a9
2 changed files with 43 additions and 2 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::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<VMInfo>,
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?;

View File

@ -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<GroupID> {
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::<Vec<_>>()
}
/// List the virtual machines with access
pub fn list_vm(&self) -> Vec<VMUuid> {
self.rights
@ -260,6 +279,11 @@ pub async fn vm_screenshot(id: VMUuid) -> anyhow::Result<Vec<u8>> {
.to_vec())
}
/// Get the VM of a group
pub async fn group_vm_info(id: &GroupID) -> anyhow::Result<Vec<VMInfo>> {
json_request(id.route_vm_info()).await
}
/// Get current server information
pub async fn get_server_info() -> anyhow::Result<SystemInfo> {
json_request("/api/server/info").await