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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::fmt::Display;
use std::str::FromStr;
use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::commands::ignite::types::Deployment;
use crate::util::deserialize_from_str;
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Default)]
pub enum ContainerType {
#[serde(rename = "ephemeral")]
Ephemeral,
#[default]
#[serde(rename = "persistent")]
Persistent,
#[serde(rename = "stateful")]
Stateful,
}
impl FromStr for ContainerType {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
serde_json::from_str(&format!("\"{}\"", s.to_lowercase())).map_err(|e| anyhow!(e))
}
}
impl Display for ContainerType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
serde_json::to_string(self).unwrap().replace('"', "")
)
}
}
impl ContainerType {
pub fn values() -> Vec<Self> {
vec![
Self::Ephemeral,
Self::Persistent,
]
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub enum ContainerState {
#[serde(rename = "exited")]
Exited,
#[serde(rename = "pending")]
Pending,
#[serde(rename = "running")]
Running,
#[serde(rename = "stopped")]
Stopped,
#[serde(rename = "terminating")]
Terminating,
#[serde(rename = "failed")]
Failed,
}
impl Display for ContainerState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
serde_json::to_string(self).unwrap().replace('"', "")
)
}
}
impl ContainerState {
pub fn from_changeable_state(state: &ChangeableContainerState) -> Self {
match state {
ChangeableContainerState::Start => Self::Running,
ChangeableContainerState::Stop => Self::Stopped,
}
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub enum ChangeableContainerState {
#[serde(rename = "stop")]
Stop,
#[serde(rename = "start")]
Start,
}
impl Display for ChangeableContainerState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
serde_json::to_string(self).unwrap().replace('"', "")
)
}
}
impl FromStr for ChangeableContainerState {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
serde_json::from_str(&format!("\"{}\"", s.to_lowercase())).map_err(|e| anyhow!(e))
}
}
impl ChangeableContainerState {
pub fn values() -> Vec<Self> {
vec![Self::Stop, Self::Start]
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Uptime {
#[serde(deserialize_with = "deserialize_from_str")]
pub last_start: DateTime<Utc>,
}
#[derive(Debug, Deserialize)]
pub struct Container {
pub id: String,
pub created_at: String,
pub state: ContainerState,
pub deployment_id: String,
pub internal_ip: Option<String>,
pub region: String,
pub uptime: Option<Uptime>,
#[serde(rename = "type")]
pub type_: ContainerType,
}
#[derive(Debug, Deserialize)]
pub struct MultipleContainersResponse {
pub containers: Vec<Container>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ContainerOptions {
pub containers: Option<u64>,
pub min_containers: Option<u64>,
pub max_containers: Option<u64>,
}
impl ContainerOptions {
pub fn from_deployment(deployment: &Deployment) -> Self {
Self {
containers: Some(deployment.container_count as u64),
min_containers: Some(0),
max_containers: Some(0),
}
}
}
#[derive(Debug, Serialize)]
pub struct CreateContainers {
pub count: u64,
}
#[derive(Debug, Serialize)]
pub struct UpdateContainerState {
pub preferred_state: ContainerState,
}
#[derive(Debug, Deserialize)]
pub struct Log {
pub nonce: String,
pub timestamp: DateTime<Utc>,
pub level: String,
pub message: String,
}
#[derive(Debug, Deserialize)]
pub struct LogsResponse {
pub logs: Vec<Log>,
}