1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::{collections::BTreeMap, net::Ipv4Addr};
use jsonrpsee_types::{DeserializeOwned, JsonValue};
use crate::{declare_id_type, impl_xo_object, types::XoObject};
#[derive(serde::Deserialize, Debug)]
pub struct Vm<O> {
pub id: VmId,
pub name_label: String,
pub name_description: String,
pub power_state: PowerState,
#[serde(rename = "$pool")]
pub pool: String,
pub tags: Vec<String>,
#[serde(default)]
pub(crate) addresses: BTreeMap<String, String>,
#[serde(deserialize_with = "map_from_optional_map", default)]
pub os_version: BTreeMap<String, String>,
pub other: O,
}
impl<O: DeserializeOwned> XoObject for Vm<O> {
const OBJECT_TYPE: &'static str = "VM";
type IdType = VmId;
}
#[derive(Debug, Clone, Copy, serde::Deserialize, PartialEq, Eq)]
pub enum PowerState {
Running,
Halted,
Suspended,
Paused,
}
impl<'a, O: serde::de::DeserializeOwned> Vm<O> {
pub fn is_running(&self) -> bool {
matches!(self.power_state, PowerState::Running)
}
pub fn distro(&self) -> Option<&str> {
match &self.os_version.get("distro") {
Some(distro) => Some(distro),
None if self.os_version.contains_key("spmajor") => Some("windows"),
None => None,
}
}
pub fn ipv4_addresses(&self) -> impl Iterator<Item = Ipv4Addr> + '_ {
self.addresses
.iter()
.filter(|(tag, _ip)| tag.contains("ipv4"))
.flat_map(|(_tag, ip)| ip.split(' '))
.filter_map(move |ip| match ip.parse() {
Ok(ip) => Some(ip),
Err(e) => {
log::warn!("Invalid IP found for VM: {}, {:?}", self.name_label, e);
None
}
})
}
}
fn map_from_optional_map<'de, D>(des: D) -> Result<BTreeMap<String, String>, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let option: Option<_> = serde::de::Deserialize::deserialize(des)?;
Ok(option.unwrap_or_default())
}
pub trait OtherInfo: serde::de::DeserializeOwned {}
impl OtherInfo for BTreeMap<String, String> {}
impl OtherInfo for std::collections::HashMap<String, String> {}
declare_id_type! {
pub struct VmId;
}
declare_id_type! {
pub struct SnapshotId;
}
#[derive(Debug, PartialEq, Clone, Eq, PartialOrd, Ord)]
pub struct VmOrSnapshotId(pub(crate) String);
impl From<SnapshotId> for VmOrSnapshotId {
fn from(id: SnapshotId) -> Self {
VmOrSnapshotId(id.0)
}
}
impl From<VmOrSnapshotId> for JsonValue {
fn from(id: VmOrSnapshotId) -> Self {
JsonValue::String(id.0)
}
}
impl From<VmId> for VmOrSnapshotId {
fn from(id: VmId) -> Self {
VmOrSnapshotId(id.0)
}
}
#[derive(serde::Deserialize, Debug)]
pub struct Snapshot {
pub id: SnapshotId,
pub name_label: String,
pub name_description: String,
}
impl_xo_object!(Snapshot => "VM-snapshot", SnapshotId);