avina_wire/resources/
flavor.rs

1use std::fmt::Display;
2
3use serde::{Deserialize, Serialize};
4#[cfg(feature = "sqlx")]
5use sqlx::{FromRow, Row, mysql::MySqlRow};
6#[cfg(feature = "tabled")]
7use tabled::Tabled;
8
9#[cfg(feature = "tabled")]
10use crate::common::display_option;
11use crate::resources::FlavorGroupMinimal;
12
13#[cfg_attr(feature = "tabled", derive(Tabled))]
14#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
15pub struct Flavor {
16    pub id: u32,
17    pub name: String,
18    pub openstack_id: String, // UUIDv4
19    #[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
20    pub group: Option<u32>,
21    #[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
22    pub group_name: Option<String>,
23    pub weight: u32,
24}
25
26#[cfg(feature = "sqlx")]
27impl<'r> FromRow<'r, MySqlRow> for Flavor {
28    fn from_row(row: &'r MySqlRow) -> Result<Self, sqlx::Error> {
29        let id: u32 = row.try_get::<i32, _>("id")?.try_into().unwrap();
30        let name: String = row.try_get("name")?;
31        let openstack_id: String = row.try_get("openstack_id")?;
32        let group: Option<u32> = row
33            .try_get::<Option<i32>, _>("group_id")?
34            .map(|g| g.try_into().unwrap());
35        let group_name: Option<String> = row.try_get("group_name")?;
36        let weight: u32 = row.try_get("weight")?;
37        Ok(Flavor {
38            id,
39            name,
40            openstack_id,
41            group,
42            group_name,
43            weight,
44        })
45    }
46}
47
48impl Display for Flavor {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        f.write_str(&format!("Flavor(id={}, name={})", self.id, self.name))
51    }
52}
53
54#[cfg_attr(feature = "sqlx", derive(FromRow))]
55#[cfg_attr(feature = "tabled", derive(Tabled))]
56#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
57pub struct FlavorMinimal {
58    #[cfg_attr(feature = "sqlx", sqlx(try_from = "i32"))]
59    pub id: u32,
60    pub name: String,
61}
62
63// TODO: maybe rethink the Display implementations
64impl Display for FlavorMinimal {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        f.write_str(&format!("Flavor(id={}, name={})", self.id, self.name))
67    }
68}
69
70#[cfg_attr(feature = "tabled", derive(Tabled))]
71#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
72pub struct FlavorDetailed {
73    pub id: u32,
74    pub name: String,
75    pub openstack_id: String, // UUIDv4
76    #[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
77    pub group: Option<FlavorGroupMinimal>,
78    #[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
79    pub group_name: Option<String>,
80    pub weight: u32,
81}
82
83impl Display for FlavorDetailed {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        f.write_str(&format!("Flavor(id={}, name={})", self.id, self.name))
86    }
87}
88
89#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
90pub struct FlavorListParams {
91    pub all: Option<bool>,
92    #[serde(rename = "flavorgroup")]
93    pub group: Option<u32>,
94}
95
96#[cfg_attr(feature = "tabled", derive(Tabled))]
97#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
98pub struct FlavorImport {
99    pub new_flavor_count: u32,
100}
101
102#[derive(Clone, Debug, Serialize, Deserialize)]
103pub struct FlavorCreateData {
104    pub name: String,
105    pub openstack_id: String, // UUIDv4
106    #[serde(skip_serializing_if = "Option::is_none")]
107    pub group: Option<u32>,
108    #[serde(skip_serializing_if = "Option::is_none")]
109    pub weight: Option<u32>,
110}
111
112impl FlavorCreateData {
113    pub fn new(name: String, openstack_id: String) -> Self {
114        Self {
115            name,
116            openstack_id,
117            group: None,
118            weight: None,
119        }
120    }
121}
122
123#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
124pub struct FlavorModifyData {
125    pub id: u32,
126
127    #[serde(skip_serializing_if = "Option::is_none")]
128    pub name: Option<String>,
129    #[serde(skip_serializing_if = "Option::is_none")]
130    pub openstack_id: Option<String>,
131    #[serde(skip_serializing_if = "Option::is_none")]
132    pub group: Option<Option<u32>>,
133    #[serde(skip_serializing_if = "Option::is_none")]
134    pub weight: Option<u32>,
135}
136
137impl FlavorModifyData {
138    pub fn new(id: u32) -> Self {
139        Self {
140            id,
141            name: None,
142            openstack_id: None,
143            group: None,
144            weight: None,
145        }
146    }
147}
148
149#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
150pub struct FlavorUsageParams {
151    #[serde(skip_serializing_if = "Option::is_none")]
152    pub user: Option<u32>,
153    #[serde(skip_serializing_if = "Option::is_none")]
154    pub project: Option<u32>,
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub all: Option<bool>,
157    #[serde(skip_serializing_if = "Option::is_none")]
158    pub aggregate: Option<bool>,
159}
160
161#[cfg_attr(feature = "tabled", derive(Tabled))]
162#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
163pub struct FlavorUsageSimple {
164    pub user_id: u32,
165    pub user_name: String,
166    pub flavor_id: u32,
167    pub flavor_name: String,
168    #[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
169    pub flavorgroup_id: Option<u32>,
170    #[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
171    pub flavorgroup_name: Option<String>,
172    pub count: u32,
173    pub usage: u32,
174}
175
176#[cfg_attr(feature = "tabled", derive(Tabled))]
177#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
178pub struct FlavorUsageAggregate {
179    pub flavor_id: u32,
180    pub flavor_name: String,
181    #[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
182    pub flavorgroup_id: Option<u32>,
183    #[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
184    pub flavorgroup_name: Option<String>,
185    pub count: u32,
186    pub usage: u32,
187}