Ready to implement network routes contents

This commit is contained in:
2023-12-04 20:16:32 +01:00
parent 0e3945089c
commit e579a3aadd
16 changed files with 523 additions and 46 deletions

View File

@ -17,6 +17,7 @@ pub async fn create(client: LibVirtReq, req: web::Json<NetworkInfo>) -> HttpResu
return Ok(HttpResponse::BadRequest().body(e.to_string()));
}
};
let uid = client.update_network(network).await?;
Ok(HttpResponse::Ok().json(NetworkID { uid }))
@ -60,3 +61,62 @@ pub async fn delete(client: LibVirtReq, path: web::Path<NetworkID>) -> HttpResul
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 {
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"))
}