Implements VM groups API (#206)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #206
This commit is contained in:
66
virtweb_backend/src/extractors/group_vm_id_extractor.rs
Normal file
66
virtweb_backend/src/extractors/group_vm_id_extractor.rs
Normal file
@ -0,0 +1,66 @@
|
||||
use crate::controllers::LibVirtReq;
|
||||
use crate::libvirt_lib_structures::domain::DomainXML;
|
||||
use crate::libvirt_lib_structures::XMLUuid;
|
||||
use crate::libvirt_rest_structures::vm::VMGroupId;
|
||||
use actix_http::Payload;
|
||||
use actix_web::error::ErrorBadRequest;
|
||||
use actix_web::web::Query;
|
||||
use actix_web::{web, Error, FromRequest, HttpRequest};
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
pub struct GroupVmIdExtractor(pub Vec<DomainXML>);
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct GroupIDInPath {
|
||||
gid: VMGroupId,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct FilterVM {
|
||||
vm_id: Option<XMLUuid>,
|
||||
}
|
||||
|
||||
impl FromRequest for GroupVmIdExtractor {
|
||||
type Error = Error;
|
||||
type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>>>>;
|
||||
|
||||
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
|
||||
let req = req.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
let Ok(group_id) =
|
||||
web::Path::<GroupIDInPath>::from_request(&req, &mut Payload::None).await
|
||||
else {
|
||||
return Err(ErrorBadRequest("Group ID not specified in path!"));
|
||||
};
|
||||
let group_id = group_id.into_inner().gid;
|
||||
|
||||
let filter_vm = match Query::<FilterVM>::from_request(&req, &mut Payload::None).await {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
log::error!("Failed to extract VM id from request! {e}");
|
||||
return Err(ErrorBadRequest("Failed to extract VM id from request!"));
|
||||
}
|
||||
};
|
||||
|
||||
let Ok(client) = LibVirtReq::from_request(&req, &mut Payload::None).await else {
|
||||
return Err(ErrorBadRequest("Failed to extract client handle!"));
|
||||
};
|
||||
|
||||
let vms = match client.get_full_group_vm_list(&group_id).await {
|
||||
Ok(vms) => vms,
|
||||
Err(e) => {
|
||||
log::error!("Failed to get the VMs of the group {group_id:?}: {e}");
|
||||
return Err(ErrorBadRequest("Failed to get the VMs of the group!"));
|
||||
}
|
||||
};
|
||||
|
||||
// Filter (if requested by the user)
|
||||
Ok(GroupVmIdExtractor(match filter_vm.vm_id {
|
||||
None => vms,
|
||||
Some(id) => vms.into_iter().filter(|vms| vms.uuid == Some(id)).collect(),
|
||||
}))
|
||||
})
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
pub mod api_auth_extractor;
|
||||
pub mod auth_extractor;
|
||||
pub mod group_vm_id_extractor;
|
||||
pub mod local_auth_extractor;
|
||||
|
Reference in New Issue
Block a user