diff options
author | Cara Salter <cara@devcara.com> | 2022-04-19 12:46:15 -0400 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-04-19 12:46:15 -0400 |
commit | 53256aacaa8a8ec5728334afc20ba2945f829b69 (patch) | |
tree | a69ffbcd88124e6578fe8192458f7e37290a04a3 /src | |
parent | 71d31463e019b5c5d6bea382eb490d53e7c0cea7 (diff) | |
download | solarlib-53256aacaa8a8ec5728334afc20ba2945f829b69.tar.gz solarlib-53256aacaa8a8ec5728334afc20ba2945f829b69.zip |
Unitype and tests
Diffstat (limited to 'src')
-rw-r--r-- | src/errors.rs | 2 | ||||
-rw-r--r-- | src/house.rs | 48 | ||||
-rw-r--r-- | src/waifu.rs | 49 |
3 files changed, 86 insertions, 13 deletions
diff --git a/src/errors.rs b/src/errors.rs index 2960ba9..3a0eedd 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -6,4 +6,6 @@ pub enum Error { Libvirt(#[from] virt::error::Error), #[error("Unknown: {0}")] Other(String), + #[error("Missing connection: {0}")] + Connection(String), } diff --git a/src/house.rs b/src/house.rs index 6159c33..735413e 100644 --- a/src/house.rs +++ b/src/house.rs @@ -30,19 +30,42 @@ pub struct House { /// Hostname pub name: String, /// FQDN or IP address, a way to talk to the house - pub address: Address + pub address: String, + + /// Connection to the House, if available + #[serde(skip)] + con: Option<Connect>, } impl House { - pub fn new(hostname: String, addr: Address) -> Result<Self, Error> { - let mut c = Connect::open(&format!("qemu+ssh://{}/system", addr.to_string()))?; + pub fn new(url: String) -> Result<Self, Error> { + let mut c = Connect::open(&url.clone())?; // If the connection succeeds, we've got one! Ok(Self { - name: hostname.clone(), - address: addr + name: c.get_hostname()?, + address: c.get_uri()?, + con: Some(c), }) } + + pub fn inhabitants(&mut self) -> Result<Vec<Waifu>, Error> { + match &self.con { + Some(c) => { + let domains = c.list_all_domains(0)?; + + let mut waifus: Vec<Waifu> = Vec::new(); + + for d in domains.iter() { + waifus.push(d.clone().try_into()?); + } + Ok(waifus) + }, + None => { + return Err(Error::Connection("Domain connection was None".to_string())); + } + } + } } #[cfg(test)] @@ -58,4 +81,19 @@ mod test { assert_eq!(String::from("127.0.0.1"), a.to_string()); assert_eq!("example.com".to_string(), d.to_string()); } + + #[test] + fn connect_to_house() { + let h: House = House::new("test:///default".to_string()).unwrap(); + } + + #[test] + fn list_inhabitants() { + let mut h: House = House::new("test:///default".to_string()).unwrap(); + + let empty_vec: Vec<Waifu> = vec![]; + + h.inhabitants().unwrap(); + } + } diff --git a/src/waifu.rs b/src/waifu.rs index a7dfba8..50bad19 100644 --- a/src/waifu.rs +++ b/src/waifu.rs @@ -4,7 +4,28 @@ use serde::{Serialize, Deserialize}; use crate::errors::Error; -type Fqdn = String; +/** + * Defines the amount of memory a waifu has + */ +#[derive(Debug, Serialize, Deserialize)] +pub struct Memory(u64); + +impl From<u64> for Memory { + fn from(u: u64) -> Self { + Self(u) + } +} +/** + * Defines the number of vCPUs a waifu has + */ +#[derive(Debug, Serialize, Deserialize)] +pub struct CpuCount(u64); + +impl From<u64> for CpuCount { + fn from(u: u64) -> Self { + Self(u) + } +} /** * Represents a virtual machine, that's active on some server @@ -17,27 +38,38 @@ pub struct Waifu { pub name: String, /// The physical machine where this one lives - pub host: Fqdn, + pub host: String, + + /// The UUID + pub uuid: String, /// 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, + pub mem: Memory, /// The amount of vCPUs assigned to this machine - pub cpu_count: u64, + pub cpu_count: CpuCount, } -impl TryFrom<Domain> for Waifu { +impl PartialEq for Waifu { + fn eq(&self, other: &Self) -> bool { + self.uuid == other.uuid + } +} + +impl TryFrom<&Domain> for Waifu { type Error = Error; - fn try_from(d: Domain) -> Result<Self, Self::Error> { + fn try_from(d: &Domain) -> Result<Self, Self::Error> { let c = d.get_connect()?; // This... feels wrong // // I know it probably works + // + // Based on code by Cadey in waifud 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)? @@ -60,8 +92,9 @@ impl TryFrom<Domain> for Waifu { name: d.get_name()?, host: c.get_hostname()?, addr, - mem: d.get_max_memory()?, - cpu_count: d.get_max_vcpus()?, + uuid: d.get_uuid_string()?, + mem: d.get_max_memory()?.into(), + cpu_count: d.get_max_vcpus()?.into(), }) } } |