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
use crate::machines::{
    MachineConfig,
    MachineRegions,
    // Checks, DnsConfig, FileConfig, GuestConfig, Header, InitConfig,
    // MetricsConfig, MountConfig, ProcessConfig, RestartPolicy, ServiceConfig, StaticConfig,
    // StopConfig, TimeoutConfig,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Serialize, Deserialize, Debug)]
pub struct MachineRequest {
    pub name: Option<String>,
    pub config: MachineConfig,
    pub region: Option<MachineRegions>,
    pub lease_ttl: Option<u64>,
    pub lsvd: Option<bool>,
    pub skip_launch: Option<bool>,
    pub skip_service_registration: Option<bool>,
}

impl MachineRequest {
    pub fn new(
        config: MachineConfig,
        name: Option<String>,
        region: Option<MachineRegions>,
    ) -> Self {
        Self {
            name: name,
            config,
            region: region,
            lease_ttl: None,
            lsvd: None,
            skip_launch: None,
            skip_service_registration: None,
        }
    }

    pub fn with_lease_ttl(mut self, lease_ttl: u64) -> Self {
        self.lease_ttl = Some(lease_ttl);
        self
    }

    pub fn with_lsvd(mut self, lsvd: bool) -> Self {
        self.lsvd = Some(lsvd);
        self
    }

    pub fn with_skip_launch(mut self, skip_launch: bool) -> Self {
        self.skip_launch = Some(skip_launch);
        self
    }

    pub fn with_skip_service_registration(mut self, skip_service_registration: bool) -> Self {
        self.skip_service_registration = Some(skip_service_registration);
        self
    }

    pub fn to_json(&self) -> String {
        serde_json::to_string(&self).unwrap()
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MachineResponse {
    pub id: Option<String>,
    pub checks: Option<Vec<CheckResponse>>,
    pub config: Option<MachineConfig>,
    pub created_at: Option<String>,
    pub events: Option<Vec<EventResponse>>,
    pub host_status: Option<HostStatusEnum>,
    pub image_ref: Option<ImageRef>,
    pub incomplete_config: Option<Value>,
    pub instance_id: Option<String>,
    pub name: Option<String>,
    pub nonce: Option<String>,
    pub private_ip: Option<String>,
    pub region: Option<String>,
    pub state: Option<String>,
    pub updated_at: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct EventResponse {
    pub id: Option<String>,
    pub request: Option<Value>,
    pub source: Option<String>,
    pub status: Option<String>,
    pub timestamp: Option<u64>,
    pub event_type: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum HostStatusEnum {
    Ok,
    Unknown,
    Unreachable,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ImageRef {
    pub digest: Option<String>,
    pub labels: Option<Value>,
    pub registry: Option<String>,
    pub repository: Option<String>,
    pub tag: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CheckResponse {
    pub name: Option<String>,
    pub output: Option<String>,
    pub status: Option<String>,
    pub updated_at: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
struct CreateMachinesResponse {
    machines: Vec<MachineResponse>,
}