Can update and delete networks

This commit is contained in:
Pierre HUBERT 2023-10-31 12:03:37 +01:00
parent 2025416607
commit 9a99e0b54e
4 changed files with 48 additions and 0 deletions

View File

@ -413,3 +413,18 @@ impl Handler<GetNetworkXMLReq> for LibVirtActor {
Ok(serde_xml_rs::from_str(&xml)?)
}
}
#[derive(Message)]
#[rtype(result = "anyhow::Result<()>")]
pub struct DeleteNetwork(pub XMLUuid);
impl Handler<DeleteNetwork> for LibVirtActor {
type Result = anyhow::Result<()>;
fn handle(&mut self, msg: DeleteNetwork, _ctx: &mut Self::Context) -> Self::Result {
log::debug!("Delete network: {}\n", msg.0.as_string());
let network = Network::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
network.undefine()?;
Ok(())
}
}

View File

@ -40,3 +40,23 @@ pub async fn get_single(client: LibVirtReq, req: web::Path<NetworkID>) -> HttpRe
Ok(HttpResponse::Ok().json(network))
}
/// 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"))
}

View File

@ -111,4 +111,9 @@ impl LibVirtClient {
pub async fn get_single_network(&self, id: XMLUuid) -> anyhow::Result<NetworkXML> {
self.0.send(libvirt_actor::GetNetworkXMLReq(id)).await?
}
/// Delete a network
pub async fn delete_network(&self, id: XMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::DeleteNetwork(id)).await?
}
}

View File

@ -186,6 +186,14 @@ async fn main() -> std::io::Result<()> {
"/api/network/{uid}",
web::get().to(network_controller::get_single),
)
.route(
"/api/network/{uid}",
web::put().to(network_controller::update),
)
.route(
"/api/network/{uid}",
web::delete().to(network_controller::delete),
)
})
.bind(&AppConfig::get().listen_address)?
.run()