Can update or delete domains

This commit is contained in:
2023-10-13 16:44:56 +02:00
parent 52d7e35d10
commit 6a3cf2e5c8
6 changed files with 111 additions and 22 deletions

View File

@ -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>;

View File

@ -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 {