diff options
author | Cara Salter <cara@devcara.com> | 2022-04-12 13:30:15 -0400 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-04-12 13:30:15 -0400 |
commit | 1fef014508c915b388de371583b0a869684fc7ea (patch) | |
tree | 9f2f4c04c472e0883055a8b23a71f724c503ee04 /src/waifu.rs | |
parent | 34af52c58daae7ad75c0871e88e7b937fcd078fb (diff) | |
download | solarlib-1fef014508c915b388de371583b0a869684fc7ea.tar.gz solarlib-1fef014508c915b388de371583b0a869684fc7ea.zip |
Add `House` definition and impls
Also rename modules to make more sense with the theme
Diffstat (limited to 'src/waifu.rs')
-rw-r--r-- | src/waifu.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/waifu.rs b/src/waifu.rs new file mode 100644 index 0000000..a7dfba8 --- /dev/null +++ b/src/waifu.rs @@ -0,0 +1,67 @@ +use std::convert::TryFrom; +use virt::{connect::Connect, domain::Domain}; +use serde::{Serialize, Deserialize}; + +use crate::errors::Error; + +type Fqdn = String; + +/** + * Represents a virtual machine, that's active on some server + * + * In keeping with the theme, it's named [Waifu] :) + */ +#[derive(Debug, Serialize, Deserialize)] +pub struct Waifu { + /// The reference name of the machine + pub name: String, + + /// The physical machine where this one lives + pub host: Fqdn, + + /// The network address where this machine can be reached + pub addr: Option<String>, + + /// The amount of RAM (in MB) assigned to this machine + pub mem: u64, + + /// The amount of vCPUs assigned to this machine + pub cpu_count: u64, +} + +impl TryFrom<Domain> for Waifu { + type Error = Error; + + fn try_from(d: Domain) -> Result<Self, Self::Error> { + let c = d.get_connect()?; + + // This... feels wrong + // + // I know it probably works + let addr: Option<String> = if d.is_active()? { + let mut addr: Vec<String> = d + .interface_addresses(virt::domain::VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE, 0)? + .into_iter() + .map(|iface| iface.addrs.clone()) + .filter(|addrs| addrs.get(0).is_some()) + .map(|addrs| addrs.get(0).unwrap().clone().addr) + .collect(); + + if addr.get(0).is_none() { + Some(String::from("localhost")) + } else { + Some(addr.swap_remove(0)) + } + } else { + None + }; + + Ok(Self { + name: d.get_name()?, + host: c.get_hostname()?, + addr, + mem: d.get_max_memory()?, + cpu_count: d.get_max_vcpus()?, + }) + } +} |