WIP REST routes to create / update Network filters

This commit is contained in:
2023-12-29 20:11:21 +01:00
parent 61c567846d
commit 7b74e7b75a
6 changed files with 331 additions and 83 deletions

View File

@ -1,3 +1,4 @@
use crate::constants;
use crate::controllers::{HttpResult, LibVirtReq};
use crate::libvirt_lib_structures::XMLUuid;
use crate::libvirt_rest_structures::nw_filter::NetworkFilter;
@ -8,6 +9,37 @@ pub struct NetworkFilterID {
uid: XMLUuid,
}
/// Create a new nw filter
pub async fn create(client: LibVirtReq, req: web::Json<NetworkFilter>) -> HttpResult {
let network = match req.0.rest2lib() {
Ok(d) => d,
Err(e) => {
log::error!("Failed to extract network filter info! {e}");
return Ok(HttpResponse::BadRequest()
.json(format!("Failed to extract network filter info! {e}")));
}
};
if constants::BUILTIN_NETWORK_FILTER_RULES.contains(&network.name.as_str()) {
return Ok(HttpResponse::ExpectationFailed()
.json("Builtin network filter rules shall not be modified!"));
}
// TODO : remove
return Ok(HttpResponse::Ok().json(network));
let uid = match client.update_network_filter(req.0, network).await {
Ok(u) => u,
Err(e) => {
log::error!("Failed to update network filter! {e}");
return Ok(HttpResponse::InternalServerError()
.json(format!("Failed to update network filter! {e}")));
}
};
Ok(HttpResponse::Ok().json(NetworkFilterID { uid }))
}
/// Get the list of network filters
pub async fn list(client: LibVirtReq) -> HttpResult {
let networks = match client.get_full_network_filters_list().await {
@ -21,7 +53,7 @@ pub async fn list(client: LibVirtReq) -> HttpResult {
let networks = networks
.into_iter()
.map(|n| NetworkFilter::from_xml(n).unwrap())
.map(|n| NetworkFilter::lib2rest(n).unwrap())
.collect::<Vec<_>>();
Ok(HttpResponse::Ok().json(networks))
@ -29,6 +61,36 @@ pub async fn list(client: LibVirtReq) -> HttpResult {
/// Get the information about a single network filter
pub async fn get_single(client: LibVirtReq, req: web::Path<NetworkFilterID>) -> HttpResult {
let nwfilter = NetworkFilter::from_xml(client.get_single_network_filter(req.uid).await?)?;
let nwfilter = NetworkFilter::lib2rest(client.get_single_network_filter(req.uid).await?)?;
Ok(HttpResponse::Ok().json(nwfilter))
}
/// Update the information about a single network filter
pub async fn update(
client: LibVirtReq,
path: web::Path<NetworkFilterID>,
body: web::Json<NetworkFilter>,
) -> HttpResult {
let mut network = match body.0.rest2lib() {
Ok(n) => n,
Err(e) => {
log::error!("Failed to extract network filter info! {e}");
return Ok(HttpResponse::BadRequest()
.json(format!("Failed to extract network filter info!\n${e}")));
}
};
network.uuid = Some(path.uid);
if constants::BUILTIN_NETWORK_FILTER_RULES.contains(&network.name.as_str()) {
return Ok(HttpResponse::ExpectationFailed()
.json("Builtin network filter rules shall not be modified!"));
}
if let Err(e) = client.update_network_filter(body.0, network).await {
log::error!("Failed to update network filter! {e}");
return Ok(HttpResponse::InternalServerError()
.json(format!("Failed to update network filter!\n${e}")));
}
Ok(HttpResponse::Ok().json("Network filter updated"))
}