use serde::{Deserialize, Serialize}; use std::{fmt, net::Ipv4Addr}; use trust_dns_proto::rr::Name; use crate::net::{cidr::CidrV4, mac::MacAddr}; #[derive(Copy, Clone, Debug, Serialize, Deserialize)] #[repr(u32)] pub enum DomainState { NoState = 0u32, Running, Blocked, Paused, ShuttingDown, ShutOff, Crashed, Suspended, Unknown(u32), } impl Default for DomainState { fn default() -> Self { Self::NoState } } impl From for DomainState { fn from(value: u32) -> Self { match value { 0 => Self::NoState, 1 => Self::Running, 2 => Self::Blocked, 3 => Self::Paused, 4 => Self::ShuttingDown, 5 => Self::ShutOff, 6 => Self::Crashed, 7 => Self::Suspended, other => Self::Unknown(other), } } } impl fmt::Display for DomainState { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "{}", match self { Self::NoState => "no state".to_owned(), Self::Running => "running".to_owned(), Self::Blocked => "blocked".to_owned(), Self::Paused => "paused".to_owned(), Self::ShuttingDown => "shutting down".to_owned(), Self::ShutOff => "shut off".to_owned(), Self::Crashed => "crashed".to_owned(), Self::Suspended => "suspended".to_owned(), Self::Unknown(code) => format!("unknown ({})", code), } ) } } #[derive(Debug, Serialize, Deserialize)] pub struct CreateStatus { pub status_text: String, pub completion: f32, pub result: Option>, } /// Struct representing a VM instance. #[derive(Debug, Serialize, Deserialize)] pub struct Instance { pub name: String, pub uuid: uuid::Uuid, pub lease: Option, pub state: DomainState, } /// Struct representing a logical "lease" held by a VM. #[derive(Debug, Serialize, Deserialize)] pub struct Lease { /// Subnet name corresponding to the lease pub subnet: String, /// The IPv4 address held by the Lease pub addr: CidrV4, /// The MAC address associated by the Lease pub mac_addr: MacAddr, } /// Struct representing a subnet used by the host for virtual /// networking. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Subnet { /// The subnet short name. pub name: String, pub data: SubnetData, } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct SubnetData { /// The name of the interface the subnet is accessible via. pub ifname: String, /// The network information for the subnet. pub network: CidrV4, /// The first host address that can be assigned dynamically /// on the subnet. pub start_host: Ipv4Addr, /// The last host address that can be assigned dynamically /// on the subnet. pub end_host: Ipv4Addr, /// The default gateway for the subnet. pub gateway4: Ipv4Addr, /// The primary DNS server for the subnet. pub dns: Vec, /// The base domain used for DNS lookup. pub domain_name: Option, /// The VLAN ID used for the domain. If none, no VLAN is set. pub vlan_id: Option, } impl SubnetData { pub fn start_bytes(&self) -> u32 { self.network.host_bits(&self.start_host) } pub fn end_bytes(&self) -> u32 { self.network.host_bits(&self.end_host) } }