From 77fa95a342f09953e3207ea0346135301ffe2664 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Thu, 26 May 2022 16:36:41 -0400 Subject: Project rename --- src/waifu.rs | 142 ----------------------------------------------------------- 1 file changed, 142 deletions(-) delete mode 100644 src/waifu.rs (limited to 'src/waifu.rs') diff --git a/src/waifu.rs b/src/waifu.rs deleted file mode 100644 index bb99d2b..0000000 --- a/src/waifu.rs +++ /dev/null @@ -1,142 +0,0 @@ -use std::convert::TryFrom; -use virt::{domain::Domain}; -use serde::{Serialize, Deserialize}; - -use crate::errors::Error; - -/** - * Defines the amount of memory a waifu has - */ -#[derive(Debug, Serialize, Deserialize)] -pub struct Memory(pub u64); - -impl From for Memory { - fn from(u: u64) -> Self { - Self(u) - } -} - -/** - * Defines the number of vCPUs a waifu has - */ -#[derive(Debug, Serialize, Deserialize)] -pub struct CpuCount(pub u64); - -impl From for CpuCount { - fn from(u: u64) -> Self { - Self(u) - } -} - -/** - * 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: String, - - /// The UUID - pub uuid: String, - - /// The network address where this machine can be reached - pub addr: Option, - - /// The amount of RAM (in MB) assigned to this machine - pub mem: Memory, - - /// The amount of vCPUs assigned to this machine - pub cpu_count: CpuCount, -} - -impl PartialEq for Waifu { - fn eq(&self, other: &Self) -> bool { - self.uuid == other.uuid - } -} - -impl TryFrom for Waifu { - type Error = Error; - - fn try_from(d: Domain) -> Result { - let c = d.get_connect()?; - - // This... feels wrong - // - // I know it probably works - // - // Based on code by Cadey in waifud - let addr: Option = if d.is_active()? { - let mut addr: Vec = 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, - uuid: d.get_uuid_string()?, - mem: d.get_max_memory()?.into(), - cpu_count: d.get_max_vcpus()?.into(), - }) - } -} - - -impl TryFrom<&Domain> for Waifu { - type Error = Error; - - fn try_from(d: &Domain) -> Result { - let c = d.get_connect()?; - - // This... feels wrong - // - // I know it probably works - // - // Based on code by Cadey in waifud - let addr: Option = if d.is_active()? { - let mut addr: Vec = 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, - uuid: d.get_uuid_string()?, - mem: d.get_max_memory()?.into(), - cpu_count: d.get_max_vcpus()?.into(), - }) - } -} -- cgit v1.2.3