2023-10-31 09:26:42 +01:00
|
|
|
use crate::controllers::{HttpResult, LibVirtReq};
|
|
|
|
use crate::libvirt_lib_structures::XMLUuid;
|
|
|
|
use crate::libvirt_rest_structures::NetworkInfo;
|
|
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
|
|
|
|
#[derive(serde::Serialize, serde::Deserialize)]
|
2023-10-31 11:08:05 +01:00
|
|
|
pub struct NetworkID {
|
|
|
|
uid: XMLUuid,
|
2023-10-31 09:26:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new network
|
|
|
|
pub async fn create(client: LibVirtReq, req: web::Json<NetworkInfo>) -> HttpResult {
|
|
|
|
let network = match req.0.to_virt_network() {
|
|
|
|
Ok(d) => d,
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("Failed to extract network info! {e}");
|
|
|
|
return Ok(HttpResponse::BadRequest().body(e.to_string()));
|
|
|
|
}
|
|
|
|
};
|
2023-12-04 20:16:32 +01:00
|
|
|
|
2023-10-31 11:08:05 +01:00
|
|
|
let uid = client.update_network(network).await?;
|
2023-10-31 09:26:42 +01:00
|
|
|
|
2023-10-31 11:08:05 +01:00
|
|
|
Ok(HttpResponse::Ok().json(NetworkID { uid }))
|
2023-10-31 09:26:42 +01:00
|
|
|
}
|
2023-10-31 10:51:13 +01:00
|
|
|
|
|
|
|
/// Get the list of networks
|
|
|
|
pub async fn list(client: LibVirtReq) -> HttpResult {
|
|
|
|
let networks = client
|
|
|
|
.get_full_networks_list()
|
|
|
|
.await?
|
|
|
|
.into_iter()
|
|
|
|
.map(|n| NetworkInfo::from_xml(n).unwrap())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(networks))
|
|
|
|
}
|
2023-10-31 11:08:05 +01:00
|
|
|
|
|
|
|
/// 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))
|
|
|
|
}
|
2023-10-31 12:03:37 +01:00
|
|
|
|
2023-12-11 18:41:59 +01:00
|
|
|
/// 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))
|
|
|
|
}
|
|
|
|
|
2023-10-31 12:03:37 +01:00
|
|
|
/// 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 = body.0.to_virt_network()?;
|
|
|
|
network.uuid = Some(path.uid);
|
|
|
|
client.update_network(network).await?;
|
|
|
|
|
|
|
|
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"))
|
|
|
|
}
|
2023-12-04 20:16:32 +01:00
|
|
|
|
|
|
|
#[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 {
|
|
|
|
client.start_network(id.uid).await?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json("Network started"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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"))
|
|
|
|
}
|