VirtWeb/virtweb_backend/src/controllers/network_controller.rs

164 lines
4.9 KiB
Rust

use crate::controllers::{HttpResult, LibVirtReq};
use crate::libvirt_lib_structures::XMLUuid;
use crate::libvirt_rest_structures::net::NetworkInfo;
use actix_web::{web, HttpResponse};
#[derive(serde::Serialize, serde::Deserialize)]
pub struct NetworkID {
uid: XMLUuid,
}
/// Create a new network
pub async fn create(client: LibVirtReq, req: web::Json<NetworkInfo>) -> HttpResult {
let network = match req.0.as_virt_network() {
Ok(d) => d,
Err(e) => {
log::error!("Failed to extract network info! {e}");
return Ok(
HttpResponse::BadRequest().json(format!("Failed to extract network info! {e}"))
);
}
};
let uid = match client.update_network(req.0, network).await {
Ok(u) => u,
Err(e) => {
log::error!("Failed to update network! {e}");
return Ok(
HttpResponse::InternalServerError().json(format!("Failed to update network! {e}"))
);
}
};
Ok(HttpResponse::Ok().json(NetworkID { uid }))
}
/// Get the list of networks
pub async fn list(client: LibVirtReq) -> HttpResult {
let networks = match client.get_full_networks_list().await {
Err(e) => {
log::error!("Failed to get the list of networks! {e}");
return Ok(HttpResponse::InternalServerError()
.json(format!("Failed to get the list of networks! {e}")));
}
Ok(l) => l,
};
let networks = networks
.into_iter()
.map(|n| NetworkInfo::from_xml(n).unwrap())
.collect::<Vec<_>>();
Ok(HttpResponse::Ok().json(networks))
}
/// Get the information about a single network
pub async fn get_single(client: LibVirtReq, req: web::Path<NetworkID>) -> HttpResult {
let network = NetworkInfo::from_xml(client.get_single_network(req.uid).await?)?;
Ok(HttpResponse::Ok().json(network))
}
/// Get the XML source description of a single network
pub async fn single_src(client: LibVirtReq, req: web::Path<NetworkID>) -> HttpResult {
let xml = client.get_single_network_xml(req.uid).await?;
Ok(HttpResponse::Ok().content_type("application/xml").body(xml))
}
/// Update the information about a single network
pub async fn update(
client: LibVirtReq,
path: web::Path<NetworkID>,
body: web::Json<NetworkInfo>,
) -> HttpResult {
let mut network = match body.0.as_virt_network() {
Ok(n) => n,
Err(e) => {
log::error!("Failed to extract network info! {e}");
return Ok(
HttpResponse::BadRequest().json(format!("Failed to extract network info!\n${e}"))
);
}
};
network.uuid = Some(path.uid);
if let Err(e) = client.update_network(body.0, network).await {
log::error!("Failed to update network! {e}");
return Ok(
HttpResponse::InternalServerError().json(format!("Failed to update network!\n${e}"))
);
}
Ok(HttpResponse::Ok().json("Network updated"))
}
/// Delete a network
pub async fn delete(client: LibVirtReq, path: web::Path<NetworkID>) -> HttpResult {
client.delete_network(path.uid).await?;
Ok(HttpResponse::Ok().json("Network deleted"))
}
#[derive(serde::Serialize, serde::Deserialize)]
pub struct NetworkAutostart {
autostart: bool,
}
/// Get autostart value of a network
pub async fn get_autostart(client: LibVirtReq, id: web::Path<NetworkID>) -> HttpResult {
Ok(HttpResponse::Ok().json(NetworkAutostart {
autostart: client.is_network_autostart(id.uid).await?,
}))
}
/// Configure autostart value for a network
pub async fn set_autostart(
client: LibVirtReq,
id: web::Path<NetworkID>,
body: web::Json<NetworkAutostart>,
) -> HttpResult {
client.set_network_autostart(id.uid, body.autostart).await?;
Ok(HttpResponse::Accepted().json("OK"))
}
#[derive(serde::Serialize)]
enum NetworkStatus {
Started,
Stopped,
}
#[derive(serde::Serialize)]
struct NetworkStatusResponse {
status: NetworkStatus,
}
/// Get network status
pub async fn status(client: LibVirtReq, id: web::Path<NetworkID>) -> HttpResult {
let started = client.is_network_started(id.uid).await?;
Ok(HttpResponse::Ok().json(NetworkStatusResponse {
status: match started {
true => NetworkStatus::Started,
false => NetworkStatus::Stopped,
},
}))
}
/// Start a network
pub async fn start(client: LibVirtReq, id: web::Path<NetworkID>) -> HttpResult {
match client.start_network(id.uid).await {
Ok(_) => Ok(HttpResponse::Accepted().json("Network started")),
Err(e) => {
log::error!("Failed to start a network! {e}: {:?}", id.uid);
Ok(HttpResponse::InternalServerError().json(format!("Failed to start network! {e}")))
}
}
}
/// Stop a network
pub async fn stop(client: LibVirtReq, id: web::Path<NetworkID>) -> HttpResult {
client.stop_network(id.uid).await?;
Ok(HttpResponse::Accepted().json("Network stopped"))
}