Ready to implement network routes contents
This commit is contained in:
@ -367,10 +367,12 @@ impl Handler<DefineNetwork> for LibVirtActor {
|
||||
msg.0.ips = vec![];
|
||||
|
||||
let mut network_xml = serde_xml_rs::to_string(&msg.0)?;
|
||||
log::trace!("Serialize network XML start: {network_xml}");
|
||||
|
||||
let ips_xml = ips_xml.join("\n");
|
||||
network_xml = network_xml.replacen("</network>", &format!("{ips_xml}</network>"), 1);
|
||||
|
||||
log::debug!("Source network structure: {:#?}", msg.0);
|
||||
log::debug!("Define network XML: {network_xml}");
|
||||
|
||||
let network = Network::define_xml(&self.m, &network_xml)?;
|
||||
@ -428,3 +430,83 @@ impl Handler<DeleteNetwork> for LibVirtActor {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "anyhow::Result<bool>")]
|
||||
pub struct IsNetworkAutostart(pub XMLUuid);
|
||||
|
||||
impl Handler<IsNetworkAutostart> for LibVirtActor {
|
||||
type Result = anyhow::Result<bool>;
|
||||
|
||||
fn handle(&mut self, msg: IsNetworkAutostart, _ctx: &mut Self::Context) -> Self::Result {
|
||||
log::debug!(
|
||||
"Check if autostart is enabled for a network: {}",
|
||||
msg.0.as_string()
|
||||
);
|
||||
let network = Network::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
|
||||
Ok(network.get_autostart()?)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "anyhow::Result<()>")]
|
||||
pub struct SetNetworkAutostart(pub XMLUuid, pub bool);
|
||||
|
||||
impl Handler<SetNetworkAutostart> for LibVirtActor {
|
||||
type Result = anyhow::Result<()>;
|
||||
|
||||
fn handle(&mut self, msg: SetNetworkAutostart, _ctx: &mut Self::Context) -> Self::Result {
|
||||
log::debug!(
|
||||
"Set autostart enabled={} for a network: {}",
|
||||
msg.1,
|
||||
msg.0.as_string()
|
||||
);
|
||||
let network = Network::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
|
||||
network.set_autostart(msg.1)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "anyhow::Result<bool>")]
|
||||
pub struct IsNetworkStarted(pub XMLUuid);
|
||||
|
||||
impl Handler<IsNetworkStarted> for LibVirtActor {
|
||||
type Result = anyhow::Result<bool>;
|
||||
|
||||
fn handle(&mut self, msg: IsNetworkStarted, _ctx: &mut Self::Context) -> Self::Result {
|
||||
log::debug!("Check if a network is started: {}", msg.0.as_string());
|
||||
let network = Network::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
|
||||
Ok(network.is_active()?)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "anyhow::Result<()>")]
|
||||
pub struct StartNetwork(pub XMLUuid);
|
||||
|
||||
impl Handler<StartNetwork> for LibVirtActor {
|
||||
type Result = anyhow::Result<()>;
|
||||
|
||||
fn handle(&mut self, msg: StartNetwork, _ctx: &mut Self::Context) -> Self::Result {
|
||||
log::debug!("Start a network: {}", msg.0.as_string());
|
||||
let network = Network::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
|
||||
network.create()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "anyhow::Result<()>")]
|
||||
pub struct StopNetwork(pub XMLUuid);
|
||||
|
||||
impl Handler<StopNetwork> for LibVirtActor {
|
||||
type Result = anyhow::Result<()>;
|
||||
|
||||
fn handle(&mut self, msg: StopNetwork, _ctx: &mut Self::Context) -> Self::Result {
|
||||
log::debug!("Stop a network: {}", msg.0.as_string());
|
||||
let network = Network::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
|
||||
network.destroy()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -149,7 +149,14 @@ impl AppConfig {
|
||||
|
||||
/// Get root storage directory
|
||||
pub fn storage_path(&self) -> PathBuf {
|
||||
Path::new(&self.storage).canonicalize().unwrap()
|
||||
let storage_path = Path::new(&self.storage);
|
||||
if !storage_path.is_dir() {
|
||||
panic!(
|
||||
"Specified storage path ({}) is not a directory!",
|
||||
self.storage
|
||||
);
|
||||
}
|
||||
storage_path.canonicalize().unwrap()
|
||||
}
|
||||
|
||||
/// Get iso storage directory
|
||||
|
@ -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"))
|
||||
}
|
||||
|
@ -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 },
|
||||
},
|
||||
})
|
||||
}
|
||||
|
@ -116,4 +116,31 @@ impl LibVirtClient {
|
||||
pub async fn delete_network(&self, id: XMLUuid) -> anyhow::Result<()> {
|
||||
self.0.send(libvirt_actor::DeleteNetwork(id)).await?
|
||||
}
|
||||
|
||||
/// Get auto-start status of a network
|
||||
pub async fn is_network_autostart(&self, id: XMLUuid) -> anyhow::Result<bool> {
|
||||
self.0.send(libvirt_actor::IsNetworkAutostart(id)).await?
|
||||
}
|
||||
|
||||
/// Update autostart value of a network
|
||||
pub async fn set_network_autostart(&self, id: XMLUuid, autostart: bool) -> anyhow::Result<()> {
|
||||
self.0
|
||||
.send(libvirt_actor::SetNetworkAutostart(id, autostart))
|
||||
.await?
|
||||
}
|
||||
|
||||
/// Check out whether a network is started or not
|
||||
pub async fn is_network_started(&self, id: XMLUuid) -> anyhow::Result<bool> {
|
||||
self.0.send(libvirt_actor::IsNetworkStarted(id)).await?
|
||||
}
|
||||
|
||||
/// Start a network
|
||||
pub async fn start_network(&self, id: XMLUuid) -> anyhow::Result<()> {
|
||||
self.0.send(libvirt_actor::StartNetwork(id)).await?
|
||||
}
|
||||
|
||||
/// Stop a network
|
||||
pub async fn stop_network(&self, id: XMLUuid) -> anyhow::Result<()> {
|
||||
self.0.send(libvirt_actor::StopNetwork(id)).await?
|
||||
}
|
||||
}
|
||||
|
@ -194,6 +194,26 @@ async fn main() -> std::io::Result<()> {
|
||||
"/api/network/{uid}",
|
||||
web::delete().to(network_controller::delete),
|
||||
)
|
||||
.route(
|
||||
"/api/network/{uid}/autostart",
|
||||
web::get().to(network_controller::get_autostart),
|
||||
)
|
||||
.route(
|
||||
"/api/network/{uid}/autostart",
|
||||
web::put().to(network_controller::set_autostart),
|
||||
)
|
||||
.route(
|
||||
"/api/network/{uid}/status",
|
||||
web::get().to(network_controller::status),
|
||||
)
|
||||
.route(
|
||||
"/api/network/{uid}/start",
|
||||
web::get().to(network_controller::start),
|
||||
)
|
||||
.route(
|
||||
"/api/network/{uid}/stop",
|
||||
web::get().to(network_controller::stop),
|
||||
)
|
||||
})
|
||||
.bind(&AppConfig::get().listen_address)?
|
||||
.run()
|
||||
|
Reference in New Issue
Block a user