1use crate::allocation::{AllocationMetric, AllocationStub};
2use crate::option::{QueryOptions, WriteOptions};
3use crate::{ClientError, Nomad};
4use reqwest::Method;
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8pub const EVALUATION_STATUS_BLOCKED: &str = "blocked";
9pub const EVALUATION_STATUS_PENDING: &str = "pending";
10pub const EVALUATION_STATUS_COMPLETE: &str = "complete";
11pub const EVALUATION_STATUS_FAILED: &str = "failed";
12pub const EVALUATION_STATUS_CANCELED: &str = "canceled";
13
14#[derive(Debug, Clone, Deserialize)]
15#[serde(rename_all = "PascalCase")]
16pub struct Evaluation {
17 #[serde(rename = "ID")]
18 pub id: String,
19 pub priority: i32,
20 #[serde(rename = "Type")]
21 pub type_: String,
22 pub triggered_by: String,
23 pub namespace: String,
24 #[serde(rename = "JobID")]
25 pub job_id: String,
26 pub job_modify_index: Option<u64>,
27 #[serde(rename = "NodeID")]
28 pub node_id: Option<String>,
29 pub node_modify_index: Option<u64>,
30 #[serde(rename = "DeploymentID")]
31 pub deployment_id: Option<String>,
32 pub status: String,
33 pub status_description: Option<String>,
34 pub wait: Option<u64>,
35 pub wait_until: Option<String>,
36 pub next_eval: Option<String>,
37 pub previous_eval: Option<String>,
38 pub blocked_eval: Option<String>,
39 pub related_evals: Option<Vec<EvaluationStub>>,
40 pub failed_tg_allocs: Option<HashMap<String, AllocationMetric>>,
41 pub plan_annotations: Option<EvaluationPlanAnnotation>,
42 pub class_eligibility: Option<HashMap<String, bool>>,
43 pub escaped_computed_class: Option<bool>,
44 pub quota_limit_reached: Option<String>,
45 pub annotate_plan: Option<bool>,
46 pub queued_allocations: Option<HashMap<String, i32>>,
47 pub snapshot_index: u64,
48 pub create_index: u64,
49 pub modify_index: u64,
50 pub create_time: i64,
51 pub modify_time: i64,
52}
53
54#[derive(Debug, Clone, Deserialize)]
55#[serde(rename_all = "PascalCase")]
56pub struct EvaluationStub {
57 #[serde(rename = "ID")]
58 pub id: String,
59 pub priority: i32,
60 #[serde(rename = "Type")]
61 pub type_: String,
62 pub triggered_by: String,
63 pub namespace: String,
64 #[serde(rename = "JobID")]
65 pub job_id: String,
66 #[serde(rename = "NodeID")]
67 pub node_id: String,
68 #[serde(rename = "DeploymentID")]
69 pub deployment_id: String,
70 pub status: String,
71 pub status_description: String,
72 pub wait_until: String,
73 pub next_eval: String,
74 pub previous_eval: String,
75 pub blocked_eval: String,
76 pub create_index: u64,
77 pub modify_index: u64,
78 pub create_time: i64,
79 pub modify_time: i64,
80}
81
82#[derive(Debug, Clone, Serialize)]
83#[serde(rename_all = "PascalCase")]
84pub struct EvalualtionDeleteRequest {
85 #[serde(rename = "EvalIDs")]
86 pub eval_ids: Option<Vec<String>>,
87 pub filter: Option<String>,
88}
89
90impl EvalualtionDeleteRequest {
91 pub fn new_with_ids(eval_ids: Vec<String>) -> Self {
94 Self {
95 eval_ids: Some(eval_ids),
96 filter: None,
97 }
98 }
99
100 pub fn new_with_filter(filter: String) -> Self {
103 Self {
104 eval_ids: None,
105 filter: Some(filter),
106 }
107 }
108}
109
110#[derive(Debug, Clone, Deserialize)]
111#[serde(rename_all = "PascalCase")]
112pub struct EvaluationDeleteResponse {
113 pub count: i32,
114}
115
116#[derive(Debug, Clone, Deserialize)]
117#[serde(rename_all = "PascalCase")]
118pub struct EvaluationCountResponse {
119 pub count: i32,
120}
121
122#[derive(Debug, Clone, Deserialize)]
123#[serde(rename_all = "PascalCase")]
124pub struct EvaluationPlanAnnotation {
125 pub desired_tg_updates: Option<HashMap<String, EvaluationDesiredUpdate>>,
126 pub preemptions: Option<Vec<AllocationStub>>,
127}
128
129#[derive(Debug, Clone, Deserialize)]
130#[serde(rename_all = "PascalCase")]
131pub struct EvaluationDesiredUpdate {
132 pub ignore: u64,
133 pub place: u64,
134 pub migrate: u64,
135 pub stop: u64,
136 pub in_place_update: u64,
137 pub destructive_update: u64,
138 pub canary: u64,
139 pub preemptions: u64,
140}
141
142pub struct Endpoint<'a> {
143 client: &'a Nomad,
144}
145
146impl<'a> Endpoint<'a> {
147 pub fn new(client: &'a Nomad) -> Self {
150 Self { client }
151 }
152
153 pub async fn count(
162 &self,
163 opts: Option<QueryOptions>,
164 ) -> Result<EvaluationCountResponse, ClientError> {
165 let req = self.client.set_request_query_options(
166 self.client
167 .build_request(Method::GET, "/v1/evaluations/count"),
168 &opts.unwrap_or_default(),
169 );
170 self.client
171 .send_with_response::<EvaluationCountResponse>(req)
172 .await
173 }
174
175 pub async fn delete(
186 &self,
187 evaluation_delete_request: &EvalualtionDeleteRequest,
188 opts: Option<WriteOptions>,
189 ) -> Result<EvaluationDeleteResponse, ClientError> {
190 let req = self
191 .client
192 .set_request_write_options(
193 self.client.build_request(Method::DELETE, "/v1/evaluations"),
194 &opts.unwrap_or_default(),
195 )
196 .json(evaluation_delete_request);
197 self.client
198 .send_with_response::<EvaluationDeleteResponse>(req)
199 .await
200 }
201
202 pub async fn get(
212 &self,
213 evaluation_id: &str,
214 opts: Option<QueryOptions>,
215 ) -> Result<Evaluation, ClientError> {
216 let req = self.client.set_request_query_options(
217 self.client.build_request(
218 Method::GET,
219 &format!("/v1/evaluation/{}?related=true", evaluation_id),
220 ),
221 &opts.unwrap_or_default(),
222 );
223 self.client.send_with_response::<Evaluation>(req).await
224 }
225
226 pub async fn list(&self, opts: Option<QueryOptions>) -> Result<Vec<Evaluation>, ClientError> {
235 let req = self.client.set_request_query_options(
236 self.client.build_request(Method::GET, "/v1/evaluations"),
237 &opts.unwrap_or_default(),
238 );
239 let mut evaluations = self
240 .client
241 .send_with_response::<Vec<Evaluation>>(req)
242 .await?;
243
244 evaluations.sort_by(|a, b| b.create_index.cmp(&a.create_index));
246
247 Ok(evaluations)
248 }
249
250 pub async fn list_allocations(
260 &self,
261 evaluation_id: &str,
262 opts: Option<QueryOptions>,
263 ) -> Result<Vec<AllocationStub>, ClientError> {
264 let req = self.client.set_request_query_options(
265 self.client.build_request(
266 Method::GET,
267 &format!("/v1/evaluation/{}/allocations", evaluation_id),
268 ),
269 &opts.unwrap_or_default(),
270 );
271 let mut allocations = self
272 .client
273 .send_with_response::<Vec<AllocationStub>>(req)
274 .await?;
275
276 allocations.sort_by(|a, b| b.create_index.cmp(&a.create_index));
278
279 Ok(allocations)
280 }
281}