From 908b0f4c566e0fbc308309042d20216347ad187f Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Mon, 9 Oct 2023 19:16:33 +0200 Subject: [PATCH] Can get domains list --- virtweb_backend/src/actors/libvirt_actor.rs | 20 +++++++++++++++++++ .../src/controllers/vm_controller.rs | 18 +++++++++++++++++ virtweb_backend/src/libvirt_client.rs | 10 ++++++++++ virtweb_backend/src/main.rs | 1 + 4 files changed, 49 insertions(+) diff --git a/virtweb_backend/src/actors/libvirt_actor.rs b/virtweb_backend/src/actors/libvirt_actor.rs index b3883fc..7d8fdb6 100644 --- a/virtweb_backend/src/actors/libvirt_actor.rs +++ b/virtweb_backend/src/actors/libvirt_actor.rs @@ -59,6 +59,26 @@ impl Handler for LibVirtActor { } } +#[derive(Message)] +#[rtype(result = "anyhow::Result>")] +pub struct GetDomainsListReq; + +impl Handler for LibVirtActor { + type Result = anyhow::Result>; + + fn handle(&mut self, _msg: GetDomainsListReq, _ctx: &mut Self::Context) -> Self::Result { + log::debug!("Get full list of domains"); + let domains = self.m.list_all_domains(0)?; + let mut ids = Vec::with_capacity(domains.len()); + + for d in domains { + ids.push(DomainXMLUuid::parse_from_str(&d.get_uuid_string()?)?); + } + + Ok(ids) + } +} + #[derive(Message)] #[rtype(result = "anyhow::Result")] pub struct GetDomainXMLReq(pub DomainXMLUuid); diff --git a/virtweb_backend/src/controllers/vm_controller.rs b/virtweb_backend/src/controllers/vm_controller.rs index c094425..1b72aec 100644 --- a/virtweb_backend/src/controllers/vm_controller.rs +++ b/virtweb_backend/src/controllers/vm_controller.rs @@ -24,6 +24,24 @@ pub async fn create(client: LibVirtReq, req: web::Json) -> HttpResult { Ok(HttpResponse::Ok().json(id)) } +/// Get the list of domains +pub async fn list_all(client: LibVirtReq) -> HttpResult { + let list = client.get_full_list().await?; + let mut out = Vec::with_capacity(list.len()); + + for entry in list { + let info = VMInfo::from_domain(entry)?; + out.push(VMInfoAndState { + state: client + .get_domain_state(info.uuid.expect("Domain without UUID !")) + .await?, + info, + }) + } + + Ok(HttpResponse::Ok().json(out)) +} + #[derive(serde::Deserialize)] pub struct SingleVMUUidReq { uid: DomainXMLUuid, diff --git a/virtweb_backend/src/libvirt_client.rs b/virtweb_backend/src/libvirt_client.rs index e3f55c8..41b71ed 100644 --- a/virtweb_backend/src/libvirt_client.rs +++ b/virtweb_backend/src/libvirt_client.rs @@ -13,6 +13,16 @@ impl LibVirtClient { self.0.send(libvirt_actor::GetHypervisorInfo).await? } + /// Get the full list of domain + pub async fn get_full_list(&self) -> anyhow::Result> { + let ids = self.0.send(libvirt_actor::GetDomainsListReq).await??; + let mut info = Vec::with_capacity(ids.len()); + for id in ids { + info.push(self.get_single_domain(id).await?) + } + Ok(info) + } + /// Get the information about a single domain pub async fn get_single_domain(&self, id: DomainXMLUuid) -> anyhow::Result { self.0.send(libvirt_actor::GetDomainXMLReq(id)).await? diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index bf60551..4434c25 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -136,6 +136,7 @@ async fn main() -> std::io::Result<()> { ) // Virtual machines controller .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)) }) .bind(&AppConfig::get().listen_address)?