Can update or delete domains
This commit is contained in:
		@@ -113,6 +113,34 @@ impl Handler<DefineDomainReq> for LibVirtActor {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Message)]
 | 
			
		||||
#[rtype(result = "anyhow::Result<()>")]
 | 
			
		||||
pub struct DeleteDomainReq {
 | 
			
		||||
    pub id: DomainXMLUuid,
 | 
			
		||||
    pub keep_files: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Handler<DeleteDomainReq> for LibVirtActor {
 | 
			
		||||
    type Result = anyhow::Result<()>;
 | 
			
		||||
 | 
			
		||||
    fn handle(&mut self, msg: DeleteDomainReq, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
        log::debug!(
 | 
			
		||||
            "Delete domain: {:?} (keep files: {})",
 | 
			
		||||
            msg.id,
 | 
			
		||||
            msg.keep_files
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        let domain = Domain::lookup_by_uuid_string(&self.m, &msg.id.as_string())?;
 | 
			
		||||
        domain.undefine_flags(match msg.keep_files {
 | 
			
		||||
            true => sys::VIR_DOMAIN_UNDEFINE_KEEP_NVRAM,
 | 
			
		||||
            false => sys::VIR_DOMAIN_UNDEFINE_NVRAM,
 | 
			
		||||
        })?;
 | 
			
		||||
 | 
			
		||||
        // TODO : delete files, if requested
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Message)]
 | 
			
		||||
#[rtype(result = "anyhow::Result<DomainState>")]
 | 
			
		||||
pub struct GetDomainStateReq(pub DomainXMLUuid);
 | 
			
		||||
@@ -121,7 +149,7 @@ impl Handler<GetDomainStateReq> for LibVirtActor {
 | 
			
		||||
    type Result = anyhow::Result<DomainState>;
 | 
			
		||||
 | 
			
		||||
    fn handle(&mut self, msg: GetDomainStateReq, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
        log::debug!("Get domain state:\n{}", msg.0.as_string());
 | 
			
		||||
        log::debug!("Get domain state: {}", msg.0.as_string());
 | 
			
		||||
        let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
 | 
			
		||||
        let (state, _) = domain.get_state()?;
 | 
			
		||||
        Ok(match state {
 | 
			
		||||
@@ -147,7 +175,7 @@ impl Handler<StartDomainReq> for LibVirtActor {
 | 
			
		||||
    type Result = anyhow::Result<()>;
 | 
			
		||||
 | 
			
		||||
    fn handle(&mut self, msg: StartDomainReq, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
        log::debug!("Start domain:\n{}", msg.0.as_string());
 | 
			
		||||
        log::debug!("Start domain: {}", msg.0.as_string());
 | 
			
		||||
        let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
 | 
			
		||||
        domain.create()?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
@@ -162,7 +190,7 @@ impl Handler<ShutdownDomainReq> for LibVirtActor {
 | 
			
		||||
    type Result = anyhow::Result<()>;
 | 
			
		||||
 | 
			
		||||
    fn handle(&mut self, msg: ShutdownDomainReq, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
        log::debug!("Shutdown domain:\n{}", msg.0.as_string());
 | 
			
		||||
        log::debug!("Shutdown domain: {}", msg.0.as_string());
 | 
			
		||||
        let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
 | 
			
		||||
        domain.shutdown()?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
@@ -177,7 +205,7 @@ impl Handler<KillDomainReq> for LibVirtActor {
 | 
			
		||||
    type Result = anyhow::Result<()>;
 | 
			
		||||
 | 
			
		||||
    fn handle(&mut self, msg: KillDomainReq, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
        log::debug!("Kill domain:\n{}", msg.0.as_string());
 | 
			
		||||
        log::debug!("Kill domain: {}", msg.0.as_string());
 | 
			
		||||
        let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
 | 
			
		||||
        domain.destroy()?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
@@ -192,7 +220,7 @@ impl Handler<ResetDomainReq> for LibVirtActor {
 | 
			
		||||
    type Result = anyhow::Result<()>;
 | 
			
		||||
 | 
			
		||||
    fn handle(&mut self, msg: ResetDomainReq, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
        log::debug!("Reset domain:\n{}", msg.0.as_string());
 | 
			
		||||
        log::debug!("Reset domain: {}", msg.0.as_string());
 | 
			
		||||
        let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
 | 
			
		||||
        domain.reset()?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
@@ -207,7 +235,7 @@ impl Handler<SuspendDomainReq> for LibVirtActor {
 | 
			
		||||
    type Result = anyhow::Result<()>;
 | 
			
		||||
 | 
			
		||||
    fn handle(&mut self, msg: SuspendDomainReq, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
        log::debug!("Suspend domain:\n{}", msg.0.as_string());
 | 
			
		||||
        log::debug!("Suspend domain: {}", msg.0.as_string());
 | 
			
		||||
        let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
 | 
			
		||||
        domain.suspend()?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
@@ -222,7 +250,7 @@ impl Handler<ResumeDomainReq> for LibVirtActor {
 | 
			
		||||
    type Result = anyhow::Result<()>;
 | 
			
		||||
 | 
			
		||||
    fn handle(&mut self, msg: ResumeDomainReq, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
        log::debug!("Resume domain:\n{}", msg.0.as_string());
 | 
			
		||||
        log::debug!("Resume domain: {}", msg.0.as_string());
 | 
			
		||||
        let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
 | 
			
		||||
        domain.resume()?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
@@ -237,7 +265,7 @@ impl Handler<ScreenshotDomainReq> for LibVirtActor {
 | 
			
		||||
    type Result = anyhow::Result<Vec<u8>>;
 | 
			
		||||
 | 
			
		||||
    fn handle(&mut self, msg: ScreenshotDomainReq, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
        log::debug!("Take screenshot of domain:\n{}", msg.0.as_string());
 | 
			
		||||
        log::debug!("Take screenshot of domain: {}", msg.0.as_string());
 | 
			
		||||
        let domain = Domain::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
 | 
			
		||||
 | 
			
		||||
        let stream = Stream::new(&self.m, 0)?;
 | 
			
		||||
 
 | 
			
		||||
@@ -12,13 +12,19 @@ pub mod vm_controller;
 | 
			
		||||
 | 
			
		||||
/// Custom error to ease controller writing
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct HttpErr {
 | 
			
		||||
    err: anyhow::Error,
 | 
			
		||||
pub enum HttpErr {
 | 
			
		||||
    Err(anyhow::Error),
 | 
			
		||||
    HTTPResponse(HttpResponse),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Display for HttpErr {
 | 
			
		||||
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
 | 
			
		||||
        Display::fmt(&self.err, f)
 | 
			
		||||
        match self {
 | 
			
		||||
            HttpErr::Err(err) => Display::fmt(err, f),
 | 
			
		||||
            HttpErr::HTTPResponse(res) => {
 | 
			
		||||
                Display::fmt(&format!("HTTP RESPONSE {}", res.status().as_str()), f)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -31,54 +37,57 @@ impl actix_web::error::ResponseError for HttpErr {
 | 
			
		||||
 | 
			
		||||
impl From<anyhow::Error> for HttpErr {
 | 
			
		||||
    fn from(err: anyhow::Error) -> HttpErr {
 | 
			
		||||
        HttpErr { err }
 | 
			
		||||
        HttpErr::Err(err)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<serde_json::Error> for HttpErr {
 | 
			
		||||
    fn from(value: serde_json::Error) -> Self {
 | 
			
		||||
        HttpErr { err: value.into() }
 | 
			
		||||
        HttpErr::Err(value.into())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<Box<dyn Error>> for HttpErr {
 | 
			
		||||
    fn from(value: Box<dyn Error>) -> Self {
 | 
			
		||||
        HttpErr {
 | 
			
		||||
            err: std::io::Error::new(ErrorKind::Other, value.to_string()).into(),
 | 
			
		||||
        }
 | 
			
		||||
        HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<std::io::Error> for HttpErr {
 | 
			
		||||
    fn from(value: std::io::Error) -> Self {
 | 
			
		||||
        HttpErr { err: value.into() }
 | 
			
		||||
        HttpErr::Err(value.into())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<std::num::ParseIntError> for HttpErr {
 | 
			
		||||
    fn from(value: std::num::ParseIntError) -> Self {
 | 
			
		||||
        HttpErr { err: value.into() }
 | 
			
		||||
        HttpErr::Err(value.into())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<tempfile::PersistError> for HttpErr {
 | 
			
		||||
    fn from(value: tempfile::PersistError) -> Self {
 | 
			
		||||
        HttpErr { err: value.into() }
 | 
			
		||||
        HttpErr::Err(value.into())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<reqwest::Error> for HttpErr {
 | 
			
		||||
    fn from(value: reqwest::Error) -> Self {
 | 
			
		||||
        HttpErr { err: value.into() }
 | 
			
		||||
        HttpErr::Err(value.into())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<reqwest::header::ToStrError> for HttpErr {
 | 
			
		||||
    fn from(value: reqwest::header::ToStrError) -> Self {
 | 
			
		||||
        HttpErr { err: value.into() }
 | 
			
		||||
        HttpErr::Err(value.into())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<HttpResponse> for HttpErr {
 | 
			
		||||
    fn from(value: HttpResponse) -> Self {
 | 
			
		||||
        HttpErr::HTTPResponse(value)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
pub type HttpResult = Result<HttpResponse, HttpErr>;
 | 
			
		||||
 | 
			
		||||
pub type LibVirtReq = web::Data<LibVirtClient>;
 | 
			
		||||
 
 | 
			
		||||
@@ -65,6 +65,49 @@ pub async fn get_single(client: LibVirtReq, id: web::Path<SingleVMUUidReq>) -> H
 | 
			
		||||
    }))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Update a VM information
 | 
			
		||||
pub async fn update(
 | 
			
		||||
    client: LibVirtReq,
 | 
			
		||||
    id: web::Path<SingleVMUUidReq>,
 | 
			
		||||
    req: web::Json<VMInfo>,
 | 
			
		||||
) -> HttpResult {
 | 
			
		||||
    let mut domain = req.0.to_domain().map_err(|e| {
 | 
			
		||||
        log::error!("Failed to extract domain info! {e}");
 | 
			
		||||
        HttpResponse::BadRequest().body(e.to_string())
 | 
			
		||||
    })?;
 | 
			
		||||
 | 
			
		||||
    domain.uuid = Some(id.uid);
 | 
			
		||||
    client.update_domain(domain).await?;
 | 
			
		||||
 | 
			
		||||
    Ok(HttpResponse::Ok().finish())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(serde::Deserialize)]
 | 
			
		||||
pub struct DeleteVMQuery {
 | 
			
		||||
    keep_files: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Delete a VM
 | 
			
		||||
pub async fn delete(
 | 
			
		||||
    client: LibVirtReq,
 | 
			
		||||
    id: web::Path<SingleVMUUidReq>,
 | 
			
		||||
    req: web::Json<DeleteVMQuery>,
 | 
			
		||||
) -> HttpResult {
 | 
			
		||||
    if let Err(e) = client.kill_domain(id.uid).await {
 | 
			
		||||
        log::info!("Failed to kill domain before deleting it: {e}");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    client
 | 
			
		||||
        .delete_domain(id.uid, req.keep_files)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|e| {
 | 
			
		||||
            log::error!("Failed to delete domain! {e}");
 | 
			
		||||
            HttpResponse::InternalServerError().body(e.to_string())
 | 
			
		||||
        })?;
 | 
			
		||||
 | 
			
		||||
    Ok(HttpResponse::Ok().finish())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Start a VM
 | 
			
		||||
pub async fn start(client: LibVirtReq, id: web::Path<SingleVMUUidReq>) -> HttpResult {
 | 
			
		||||
    Ok(match client.start_domain(id.uid).await {
 | 
			
		||||
 
 | 
			
		||||
@@ -33,6 +33,13 @@ impl LibVirtClient {
 | 
			
		||||
        self.0.send(libvirt_actor::DefineDomainReq(xml)).await?
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Delete a domain
 | 
			
		||||
    pub async fn delete_domain(&self, id: DomainXMLUuid, keep_files: bool) -> anyhow::Result<()> {
 | 
			
		||||
        self.0
 | 
			
		||||
            .send(libvirt_actor::DeleteDomainReq { id, keep_files })
 | 
			
		||||
            .await?
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get the state of a domain
 | 
			
		||||
    pub async fn get_domain_state(&self, id: DomainXMLUuid) -> anyhow::Result<DomainState> {
 | 
			
		||||
        self.0.send(libvirt_actor::GetDomainStateReq(id)).await?
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,7 @@ impl DomainXMLUuid {
 | 
			
		||||
#[derive(serde::Serialize, serde::Deserialize)]
 | 
			
		||||
#[serde(rename = "os")]
 | 
			
		||||
pub struct OSXML {
 | 
			
		||||
    #[serde(rename(serialize = "@firmware"))]
 | 
			
		||||
    #[serde(rename(serialize = "@firmware"), default)]
 | 
			
		||||
    pub firmware: String,
 | 
			
		||||
    pub r#type: OSTypeXML,
 | 
			
		||||
    pub loader: Option<OSLoaderXML>,
 | 
			
		||||
 
 | 
			
		||||
@@ -138,6 +138,8 @@ async fn main() -> std::io::Result<()> {
 | 
			
		||||
            .route("/api/vm/create", web::post().to(vm_controller::create))
 | 
			
		||||
            .route("/api/vm/list", web::get().to(vm_controller::list_all))
 | 
			
		||||
            .route("/api/vm/{uid}", web::get().to(vm_controller::get_single))
 | 
			
		||||
            .route("/api/vm/{uid}", web::put().to(vm_controller::update))
 | 
			
		||||
            .route("/api/vm/{uid}", web::delete().to(vm_controller::delete))
 | 
			
		||||
            .route("/api/vm/{uid}/start", web::get().to(vm_controller::start))
 | 
			
		||||
            .route(
 | 
			
		||||
                "/api/vm/{uid}/shutdown",
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user