Can get domains list

This commit is contained in:
Pierre HUBERT 2023-10-09 19:16:33 +02:00
parent b69c97e6fe
commit 908b0f4c56
4 changed files with 49 additions and 0 deletions

View File

@ -59,6 +59,26 @@ impl Handler<GetHypervisorInfo> for LibVirtActor {
} }
} }
#[derive(Message)]
#[rtype(result = "anyhow::Result<Vec<DomainXMLUuid>>")]
pub struct GetDomainsListReq;
impl Handler<GetDomainsListReq> for LibVirtActor {
type Result = anyhow::Result<Vec<DomainXMLUuid>>;
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)] #[derive(Message)]
#[rtype(result = "anyhow::Result<DomainXML>")] #[rtype(result = "anyhow::Result<DomainXML>")]
pub struct GetDomainXMLReq(pub DomainXMLUuid); pub struct GetDomainXMLReq(pub DomainXMLUuid);

View File

@ -24,6 +24,24 @@ pub async fn create(client: LibVirtReq, req: web::Json<VMInfo>) -> HttpResult {
Ok(HttpResponse::Ok().json(id)) 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)] #[derive(serde::Deserialize)]
pub struct SingleVMUUidReq { pub struct SingleVMUUidReq {
uid: DomainXMLUuid, uid: DomainXMLUuid,

View File

@ -13,6 +13,16 @@ impl LibVirtClient {
self.0.send(libvirt_actor::GetHypervisorInfo).await? self.0.send(libvirt_actor::GetHypervisorInfo).await?
} }
/// Get the full list of domain
pub async fn get_full_list(&self) -> anyhow::Result<Vec<DomainXML>> {
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 /// Get the information about a single domain
pub async fn get_single_domain(&self, id: DomainXMLUuid) -> anyhow::Result<DomainXML> { pub async fn get_single_domain(&self, id: DomainXMLUuid) -> anyhow::Result<DomainXML> {
self.0.send(libvirt_actor::GetDomainXMLReq(id)).await? self.0.send(libvirt_actor::GetDomainXMLReq(id)).await?

View File

@ -136,6 +136,7 @@ async fn main() -> std::io::Result<()> {
) )
// Virtual machines controller // Virtual machines controller
.route("/api/vm/create", web::post().to(vm_controller::create)) .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::get().to(vm_controller::get_single))
}) })
.bind(&AppConfig::get().listen_address)? .bind(&AppConfig::get().listen_address)?