lrzcc_wire/accounting/
server_state.rs

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
use crate::common::display_option;
use chrono::{DateTime, FixedOffset};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use tabled::Tabled;

#[derive(Clone, Debug, Deserialize, Serialize, Tabled, PartialEq)]
pub struct ServerState {
    pub id: u32,
    pub begin: DateTime<FixedOffset>,
    #[tabled(display_with = "display_option")]
    pub end: Option<DateTime<FixedOffset>>,
    pub instance_id: String, // UUIDv4
    pub instance_name: String,
    pub flavor: u32,
    pub flavor_name: String,
    pub status: String,
    pub user: u32,
    pub username: String,
}

impl Display for ServerState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("ServerState(id={})", self.id))
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, Tabled, PartialEq)]
pub struct ServerStateImport {
    pub new_state_count: u32,
    pub end_state_count: u32,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ServerStateListParams {
    pub server: Option<String>,
    pub user: Option<u32>,
    pub project: Option<u32>,
    pub all: Option<bool>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ServerStateCreateData {
    pub begin: DateTime<FixedOffset>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end: Option<DateTime<FixedOffset>>,
    pub instance_id: String, // UUIDv4
    pub instance_name: String,
    pub flavor: u32,
    // TODO we need an enum here
    pub status: String,
    pub user: u32,
}

impl ServerStateCreateData {
    pub fn new(
        begin: DateTime<FixedOffset>,
        instance_id: String, // UUIDv4
        instance_name: String,
        flavor: u32,
        status: String,
        user: u32,
    ) -> Self {
        Self {
            begin,
            end: None,
            instance_id,
            instance_name,
            flavor,
            status,
            user,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ServerStateModifyData {
    pub id: u32,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub begin: Option<DateTime<FixedOffset>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end: Option<DateTime<FixedOffset>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instance_id: Option<String>, // UUIDv4
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instance_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flavor: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    // TODO we need an enum here
    pub status: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<u32>,
}

impl ServerStateModifyData {
    pub fn new(id: u32) -> Self {
        Self {
            id,
            begin: None,
            end: None,
            instance_id: None,
            instance_name: None,
            flavor: None,
            status: None,
            user: None,
        }
    }
}