VirtWeb/virtweb_backend/src/libvirt_client.rs

98 lines
3.3 KiB
Rust
Raw Normal View History

2023-09-06 16:54:38 +00:00
use crate::actors::libvirt_actor;
2023-10-04 09:18:50 +00:00
use crate::actors::libvirt_actor::LibVirtActor;
2023-10-09 16:45:41 +00:00
use crate::libvirt_lib_structures::{DomainState, DomainXML, DomainXMLUuid};
2023-10-04 09:18:50 +00:00
use crate::libvirt_rest_structures::HypervisorInfo;
2023-09-06 16:54:38 +00:00
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?
}
2023-10-04 09:18:50 +00:00
2023-10-09 17:16:33 +00:00
/// 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)
}
2023-10-04 17:03:20 +00:00
/// 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?
}
2023-10-04 09:18:50 +00:00
/// Update a domain
pub async fn update_domain(&self, xml: DomainXML) -> anyhow::Result<DomainXMLUuid> {
self.0.send(libvirt_actor::DefineDomainReq(xml)).await?
}
2023-10-09 16:45:41 +00:00
2023-10-13 14:44:56 +00:00
/// 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?
}
2023-10-09 16:45:41 +00:00
/// 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?
}
2023-10-10 10:35:43 +00:00
/// 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?
}
2023-10-13 13:27:01 +00:00
/// 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?
}
2023-10-28 15:30:27 +00:00
/// Get auto-start status of a domain
pub async fn is_domain_autostart(&self, id: DomainXMLUuid) -> anyhow::Result<bool> {
self.0.send(libvirt_actor::IsDomainAutostart(id)).await?
}
pub async fn set_domain_autostart(
&self,
id: DomainXMLUuid,
autostart: bool,
) -> anyhow::Result<()> {
self.0
.send(libvirt_actor::SetDomainAutostart(id, autostart))
.await?
}
2023-09-06 16:54:38 +00:00
}