VirtWeb/virtweb_backend/src/libvirt_client.rs

83 lines
2.8 KiB
Rust

use crate::actors::libvirt_actor;
use crate::actors::libvirt_actor::LibVirtActor;
use crate::libvirt_lib_structures::{DomainState, DomainXML, DomainXMLUuid};
use crate::libvirt_rest_structures::HypervisorInfo;
use actix::Addr;
#[derive(Clone)]
pub struct LibVirtClient(pub Addr<LibVirtActor>);
impl LibVirtClient {
/// Get hypervisor info
pub async fn get_info(&self) -> anyhow::Result<HypervisorInfo> {
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
pub async fn get_single_domain(&self, id: DomainXMLUuid) -> anyhow::Result<DomainXML> {
self.0.send(libvirt_actor::GetDomainXMLReq(id)).await?
}
/// Update a domain
pub async fn update_domain(&self, xml: DomainXML) -> anyhow::Result<DomainXMLUuid> {
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?
}
/// Start a domain
pub async fn start_domain(&self, id: DomainXMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::StartDomainReq(id)).await?
}
/// Shutdown a domain
pub async fn shutdown_domain(&self, id: DomainXMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::ShutdownDomainReq(id)).await?
}
/// Kill a domain
pub async fn kill_domain(&self, id: DomainXMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::KillDomainReq(id)).await?
}
/// Reset a domain
pub async fn reset_domain(&self, id: DomainXMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::ResetDomainReq(id)).await?
}
/// Suspend a domain
pub async fn suspend_domain(&self, id: DomainXMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::SuspendDomainReq(id)).await?
}
/// Resume a domain
pub async fn resume_domain(&self, id: DomainXMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::ResumeDomainReq(id)).await?
}
/// Take a screenshot of the domain
pub async fn screenshot_domain(&self, id: DomainXMLUuid) -> anyhow::Result<Vec<u8>> {
self.0.send(libvirt_actor::ScreenshotDomainReq(id)).await?
}
}