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"))
}

View File

@ -29,11 +29,13 @@ struct LenConstraints {
#[derive(serde::Serialize)]
struct ServerConstraints {
iso_max_size: usize,
name_size: LenConstraints,
title_size: LenConstraints,
vm_name_size: LenConstraints,
vm_title_size: LenConstraints,
memory_size: LenConstraints,
disk_name_size: LenConstraints,
disk_size: LenConstraints,
net_name_size: LenConstraints,
net_title_size: LenConstraints,
}
pub async fn static_config(local_auth: LocalAuthEnabled) -> impl Responder {
@ -45,8 +47,8 @@ pub async fn static_config(local_auth: LocalAuthEnabled) -> impl Responder {
constraints: ServerConstraints {
iso_max_size: constants::ISO_MAX_SIZE,
name_size: LenConstraints { min: 2, max: 50 },
title_size: LenConstraints { min: 0, max: 50 },
vm_name_size: LenConstraints { min: 2, max: 50 },
vm_title_size: LenConstraints { min: 0, max: 50 },
memory_size: LenConstraints {
min: constants::MIN_VM_MEMORY,
max: constants::MAX_VM_MEMORY,
@ -59,6 +61,9 @@ pub async fn static_config(local_auth: LocalAuthEnabled) -> impl Responder {
min: DISK_SIZE_MIN,
max: DISK_SIZE_MAX,
},
net_name_size: LenConstraints { min: 2, max: 50 },
net_title_size: LenConstraints { min: 0, max: 50 },
},
})
}