nazrin/api/src/model.rs

130 lines
3.5 KiB
Rust
Raw Normal View History

2022-12-30 06:06:14 +00:00
use serde::{Deserialize, Serialize};
2023-01-17 04:42:01 +00:00
use std::{fmt, net::Ipv4Addr};
2022-12-30 06:06:14 +00:00
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<u32> 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),
}
)
}
}
2023-01-17 04:42:01 +00:00
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateStatus {
pub status_text: String,
pub completion: f32,
pub result: Option<Result<Instance, String>>,
}
2022-12-30 06:06:14 +00:00
/// Struct representing a VM instance.
#[derive(Debug, Serialize, Deserialize)]
pub struct Instance {
pub name: String,
pub uuid: uuid::Uuid,
pub lease: Option<Lease>,
pub state: DomainState,
}
/// Struct representing a logical "lease" held by a VM.
#[derive(Debug, Serialize, Deserialize)]
pub struct Lease {
2023-01-17 04:42:01 +00:00
/// Subnet name corresponding to the lease
pub subnet: String,
2022-12-30 06:06:14 +00:00
/// 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.
2023-01-17 04:42:01 +00:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2022-12-30 06:06:14 +00:00
pub struct Subnet {
2023-01-17 04:42:01 +00:00
/// The subnet short name.
pub name: String,
2022-12-30 06:06:14 +00:00
pub data: SubnetData,
}
2023-01-17 04:42:01 +00:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2022-12-30 06:06:14 +00:00
pub struct SubnetData {
2023-01-17 04:42:01 +00:00
/// The name of the interface the subnet is accessible via.
pub ifname: String,
2022-12-30 06:06:14 +00:00
/// 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.
2023-01-17 04:42:01 +00:00
pub gateway4: Ipv4Addr,
2022-12-30 06:06:14 +00:00
/// The primary DNS server for the subnet.
pub dns: Vec<Ipv4Addr>,
/// The base domain used for DNS lookup.
pub domain_name: Option<Name>,
2023-01-17 04:42:01 +00:00
/// The VLAN ID used for the domain. If none, no VLAN is set.
pub vlan_id: Option<u32>,
2022-12-30 06:06:14 +00:00
}
2023-01-17 04:42:01 +00:00
impl SubnetData {
pub fn start_bytes(&self) -> u32 {
self.network.host_bits(&self.start_host)
2022-12-30 06:06:14 +00:00
}
2023-01-17 04:42:01 +00:00
pub fn end_bytes(&self) -> u32 {
self.network.host_bits(&self.end_host)
2022-12-30 06:06:14 +00:00
}
}