1use crate::machines::TimeoutConfig;
2use serde::{Deserialize, Serialize};
3
4#[derive(Serialize, Deserialize, Debug)]
5pub struct Checks {
6 pub grace_period: Option<TimeoutConfig>,
7 pub headers: Option<Vec<Header>>,
8 pub interval: Option<TimeoutConfig>,
9 pub kind: Option<CheckKind>,
10 pub method: Option<String>,
11 pub path: Option<String>,
12 pub port: Option<u16>,
13 pub protocol: Option<Protocol>,
14 pub timeout: Option<TimeoutConfig>,
15 pub tls_server_name: Option<String>,
16 pub tls_skip_verify: Option<bool>,
17 #[serde(rename = "type")]
18 pub check_type: Option<CheckType>,
19}
20
21impl Checks {
22 pub fn new() -> Self {
23 Checks {
24 grace_period: None,
25 headers: None,
26 interval: None,
27 kind: None,
28 method: None,
29 path: None,
30 port: None,
31 protocol: None,
32 timeout: None,
33 tls_server_name: None,
34 tls_skip_verify: None,
35 check_type: None,
36 }
37 }
38
39 pub fn builder() -> CheckBuilder {
40 CheckBuilder::new()
41 }
42}
43
44#[derive(Serialize, Deserialize, Debug)]
45#[serde(rename_all = "lowercase")]
46pub enum CheckKind {
47 Informational,
48 Readiness,
49}
50
51#[derive(Serialize, Deserialize, Debug)]
52#[serde(rename_all = "lowercase")]
53pub enum CheckType {
54 Tcp,
55 Http,
56}
57
58#[derive(Serialize, Deserialize, Debug)]
59#[serde(rename_all = "lowercase")]
60pub enum Protocol {
61 Http,
62 Https,
63}
64
65#[derive(Serialize, Deserialize, Debug)]
66pub struct Header {
67 pub name: String,
68 pub values: Vec<String>,
69}
70
71pub struct CheckBuilder {
72 grace_period: Option<TimeoutConfig>,
73 headers: Option<Vec<Header>>,
74 interval: Option<TimeoutConfig>,
75 kind: Option<CheckKind>,
76 method: Option<String>,
77 path: Option<String>,
78 port: Option<u16>,
79 protocol: Option<Protocol>,
80 timeout: Option<TimeoutConfig>,
81 tls_server_name: Option<String>,
82 tls_skip_verify: Option<bool>,
83 check_type: Option<CheckType>,
84}
85
86impl CheckBuilder {
87 pub fn new() -> Self {
88 CheckBuilder {
89 grace_period: None,
90 headers: None,
91 interval: None,
92 kind: None,
93 method: None,
94 path: None,
95 port: None,
96 protocol: None,
97 timeout: None,
98 tls_server_name: None,
99 tls_skip_verify: None,
100 check_type: None,
101 }
102 }
103
104 pub fn grace_period(mut self, seconds: u64) -> Self {
105 self.grace_period = Some(TimeoutConfig::new(seconds));
106 self
107 }
108
109 pub fn add_header(mut self, name: &str, values: Vec<String>) -> Self {
110 let header = Header {
111 name: name.to_string(),
112 values,
113 };
114 if let Some(headers) = &mut self.headers {
115 headers.push(header);
116 } else {
117 self.headers = Some(vec![header]);
118 }
119 self
120 }
121
122 pub fn interval(mut self, seconds: u64) -> Self {
123 self.interval = Some(TimeoutConfig::new(seconds));
124 self
125 }
126
127 pub fn kind(mut self, kind: CheckKind) -> Self {
128 self.kind = Some(kind);
129 self
130 }
131
132 pub fn method(mut self, method: &str) -> Self {
133 self.method = Some(method.to_string());
134 self
135 }
136
137 pub fn path(mut self, path: &str) -> Self {
138 self.path = Some(path.to_string());
139 self
140 }
141
142 pub fn port(mut self, port: u16) -> Self {
143 self.port = Some(port);
144 self
145 }
146
147 pub fn protocol(mut self, protocol: Protocol) -> Self {
148 self.protocol = Some(protocol);
149 self
150 }
151
152 pub fn timeout(mut self, seconds: u64) -> Self {
153 self.timeout = Some(TimeoutConfig::new(seconds));
154 self
155 }
156
157 pub fn tls_server_name(mut self, tls_server_name: &str) -> Self {
158 self.tls_server_name = Some(tls_server_name.to_string());
159 self
160 }
161
162 pub fn tls_skip_verify(mut self, tls_skip_verify: bool) -> Self {
163 self.tls_skip_verify = Some(tls_skip_verify);
164 self
165 }
166
167 pub fn check_type(mut self, check_type: CheckType) -> Self {
168 self.check_type = Some(check_type);
169 self
170 }
171
172 pub fn build(self) -> Checks {
173 Checks {
174 grace_period: self.grace_period,
175 headers: self.headers,
176 interval: self.interval,
177 kind: self.kind,
178 method: self.method,
179 path: self.path,
180 port: self.port,
181 protocol: self.protocol,
182 timeout: self.timeout,
183 tls_server_name: self.tls_server_name,
184 tls_skip_verify: self.tls_skip_verify,
185 check_type: self.check_type,
186 }
187 }
188}