Can get the list of groups
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pierre HUBERT 2024-11-02 17:44:10 +01:00
parent 55b49699eb
commit c908d00c62
5 changed files with 47 additions and 6 deletions

View File

@ -0,0 +1,16 @@
use crate::controllers::{HttpResult, LibVirtReq};
use actix_web::HttpResponse;
/// Get the list of groups
pub async fn list(client: LibVirtReq) -> HttpResult {
let groups = match client.get_full_groups_list().await {
Err(e) => {
log::error!("Failed to get the list of groups! {e}");
return Ok(HttpResponse::InternalServerError()
.json(format!("Failed to get the list of groups! {e}")));
}
Ok(l) => l,
};
Ok(HttpResponse::Ok().json(groups))
}

View File

@ -8,6 +8,7 @@ use std::io::ErrorKind;
pub mod api_tokens_controller; pub mod api_tokens_controller;
pub mod auth_controller; pub mod auth_controller;
pub mod groups_controller;
pub mod iso_controller; pub mod iso_controller;
pub mod network_controller; pub mod network_controller;
pub mod nwfilter_controller; pub mod nwfilter_controller;

View File

@ -7,8 +7,9 @@ use crate::libvirt_lib_structures::XMLUuid;
use crate::libvirt_rest_structures::hypervisor::HypervisorInfo; use crate::libvirt_rest_structures::hypervisor::HypervisorInfo;
use crate::libvirt_rest_structures::net::NetworkInfo; use crate::libvirt_rest_structures::net::NetworkInfo;
use crate::libvirt_rest_structures::nw_filter::NetworkFilter; use crate::libvirt_rest_structures::nw_filter::NetworkFilter;
use crate::libvirt_rest_structures::vm::VMInfo; use crate::libvirt_rest_structures::vm::{VMGroupId, VMInfo};
use actix::Addr; use actix::Addr;
use std::collections::HashSet;
#[derive(Clone)] #[derive(Clone)]
pub struct LibVirtClient(pub Addr<LibVirtActor>); pub struct LibVirtClient(pub Addr<LibVirtActor>);
@ -107,6 +108,18 @@ impl LibVirtClient {
.await? .await?
} }
/// Get the full list of groups
pub async fn get_full_groups_list(&self) -> anyhow::Result<Vec<VMGroupId>> {
let domains = self.get_full_domains_list().await?;
let mut out = HashSet::new();
for d in domains {
if let Some(g) = VMInfo::from_domain(d)?.group {
out.insert(g);
}
}
Ok(out.into_iter().collect())
}
/// Update a network configuration /// Update a network configuration
pub async fn update_network( pub async fn update_network(
&self, &self,

View File

@ -10,6 +10,9 @@ use crate::utils::files_utils::convert_size_unit_to_mb;
use lazy_regex::regex; use lazy_regex::regex;
use num::Integer; use num::Integer;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq, Hash)]
pub struct VMGroupId(pub String);
#[derive(serde::Serialize, serde::Deserialize)] #[derive(serde::Serialize, serde::Deserialize)]
pub enum BootType { pub enum BootType {
UEFI, UEFI,
@ -61,7 +64,7 @@ pub struct VMInfo {
pub description: Option<String>, pub description: Option<String>,
/// Group associated with the VM (VirtWeb specific field) /// Group associated with the VM (VirtWeb specific field)
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub group: Option<String>, pub group: Option<VMGroupId>,
pub boot_type: BootType, pub boot_type: BootType,
pub architecture: VMArchitecture, pub architecture: VMArchitecture,
/// VM allocated memory, in megabytes /// VM allocated memory, in megabytes
@ -109,7 +112,7 @@ impl VMInfo {
} }
if let Some(group) = &self.group { if let Some(group) = &self.group {
if !regex!("^[a-zA-Z0-9]+$").is_match(group) { if !regex!("^[a-zA-Z0-9]+$").is_match(&group.0) {
return Err(StructureExtraction("VM group name is invalid!").into()); return Err(StructureExtraction("VM group name is invalid!").into());
} }
} }
@ -294,7 +297,7 @@ impl VMInfo {
metadata: Some(DomainMetadataXML { metadata: Some(DomainMetadataXML {
virtweb: DomainMetadataVirtWebXML { virtweb: DomainMetadataVirtWebXML {
ns: "https://virtweb.communiquons.org".to_string(), ns: "https://virtweb.communiquons.org".to_string(),
group: self.group.clone(), group: self.group.clone().map(|g| g.0),
}, },
}), }),
os: OSXML { os: OSXML {
@ -384,7 +387,13 @@ impl VMInfo {
genid: domain.genid.map(XMLUuid), genid: domain.genid.map(XMLUuid),
title: domain.title, title: domain.title,
description: domain.description, description: domain.description,
group: domain.metadata.clone().unwrap_or_default().virtweb.group, group: domain
.metadata
.clone()
.unwrap_or_default()
.virtweb
.group
.map(VMGroupId),
boot_type: match domain.os.loader { boot_type: match domain.os.loader {
None => BootType::UEFI, None => BootType::UEFI,
Some(l) => match l.secure.as_str() { Some(l) => match l.secure.as_str() {

View File

@ -22,7 +22,7 @@ use virtweb_backend::constants::{
MAX_INACTIVITY_DURATION, MAX_SESSION_DURATION, SESSION_COOKIE_NAME, MAX_INACTIVITY_DURATION, MAX_SESSION_DURATION, SESSION_COOKIE_NAME,
}; };
use virtweb_backend::controllers::{ use virtweb_backend::controllers::{
api_tokens_controller, auth_controller, iso_controller, network_controller, api_tokens_controller, auth_controller, groups_controller, iso_controller, network_controller,
nwfilter_controller, server_controller, static_controller, vm_controller, nwfilter_controller, server_controller, static_controller, vm_controller,
}; };
use virtweb_backend::libvirt_client::LibVirtClient; use virtweb_backend::libvirt_client::LibVirtClient;
@ -210,6 +210,8 @@ async fn main() -> std::io::Result<()> {
web::get().to(vm_controller::vnc_token), web::get().to(vm_controller::vnc_token),
) )
.route("/api/vnc", web::get().to(vm_controller::vnc)) .route("/api/vnc", web::get().to(vm_controller::vnc))
// Groups controller
.route("/api/groups/list", web::get().to(groups_controller::list))
// Network controller // Network controller
.route( .route(
"/api/network/create", "/api/network/create",