Skip to main content

nomad_rs_api/
deployment.rs

1use crate::option::{QueryOptions, WriteOptions};
2use crate::{ClientError, Nomad};
3use reqwest::Method;
4use serde::{Deserialize, Serialize};
5use time;
6
7#[derive(Debug, Serialize, Deserialize)]
8#[serde(rename_all = "PascalCase")]
9pub struct Deployment {
10    #[serde(rename = "ID")]
11    pub id: String,
12    pub namespace: String,
13    #[serde(rename = "JobID")]
14    pub job_id: String,
15    pub job_version: u64,
16    pub job_modify_index: u64,
17    pub job_spec_modify_index: u64,
18    pub job_create_index: u64,
19    pub is_multiregion: bool,
20    pub task_groups: std::collections::HashMap<String, DeploymentState>,
21    pub status: String,
22    pub status_description: String,
23    pub create_index: u64,
24    pub modify_index: u64,
25    pub create_time: i64,
26    pub modify_time: i64,
27}
28
29#[derive(Debug, Serialize, Deserialize)]
30#[serde(rename_all = "PascalCase")]
31pub struct DeploymentState {
32    pub placed_canaries: Option<Vec<String>>,
33    pub auto_revert: bool,
34    pub progress_deadline: i64,
35    #[serde(with = "time::serde::rfc3339")]
36    pub require_progress_by: time::OffsetDateTime,
37    pub promoted: bool,
38    pub desired_canaries: i32,
39    pub desired_total: i32,
40    pub placed_allocs: i32,
41    pub healthy_allocs: i32,
42    pub unhealthy_allocs: i32,
43}
44
45#[derive(Debug, Serialize, Deserialize)]
46#[serde(rename_all = "PascalCase")]
47pub struct DeploymentUpdateResponse {
48    #[serde(rename = "ID")]
49    pub eval_id: String,
50    pub eval_create_index: u64,
51    pub deployment_modify_index: u64,
52    pub reverted_job_version: Option<u64>,
53}
54
55#[derive(Debug, Serialize, Deserialize)]
56#[serde(rename_all = "PascalCase")]
57pub struct DeploymentPauseRequest {
58    #[serde(rename = "DeploymentID")]
59    pub deployment_id: String,
60    pub pause: bool,
61}
62
63#[derive(Debug, Serialize, Deserialize)]
64#[serde(rename_all = "PascalCase")]
65pub struct DeploymentPromoteRequest {
66    #[serde(rename = "DeploymentID")]
67    pub deployment_id: String,
68    pub all: bool,
69    pub groups: Option<Vec<String>>,
70}
71
72pub struct Endpoint<'a> {
73    client: &'a Nomad,
74}
75
76impl<'a> Endpoint<'a> {
77    /// Create a new `Endpoint` with the given `Nomad` client to interact with
78    /// the deployment endpoints.
79    pub fn new(client: &'a Nomad) -> Self {
80        Self { client }
81    }
82
83    /// Fail a deployment by its ID.
84    ///
85    /// # Arguments
86    /// * `id` - The ID of the deployment to fail.
87    /// * `opts` - Optional write options for the request.
88    ///
89    /// # Returns
90    /// A `Result` containing the deployment update response or an error if the
91    /// request fails.
92    pub async fn fail(
93        &self,
94        id: &str,
95        opts: Option<WriteOptions>,
96    ) -> Result<DeploymentUpdateResponse, ClientError> {
97        let req = self.client.set_request_write_options(
98            self.client
99                .build_request(Method::POST, &format!("/v1/deployment/fail/{}", id)),
100            &opts.unwrap_or_default(),
101        );
102        self.client
103            .send_with_response::<DeploymentUpdateResponse>(req)
104            .await
105    }
106
107    /// Get a specific deployment by its ID.
108    ///
109    /// # Arguments
110    /// * `id` - The ID of the deployment to retrieve.
111    /// * `opts` - Optional query options for the request.
112    ///
113    /// # Returns
114    /// A `Result` containing the deployment or an error if the request fails.
115    pub async fn get(
116        &self,
117        id: &str,
118        opts: Option<QueryOptions>,
119    ) -> Result<Deployment, ClientError> {
120        let req = self.client.set_request_query_options(
121            self.client
122                .build_request(Method::GET, &format!("/v1/deployment/{}", id)),
123            &opts.unwrap_or_default(),
124        );
125        self.client.send_with_response::<Deployment>(req).await
126    }
127
128    /// Get the list of deployments in the Nomad cluster.
129    ///
130    /// # Arguments
131    /// * `opts` - Optional query options for the request.
132    ///
133    /// # Returns
134    /// A `Result` containing a vector of deployments or an error if the request
135    /// fails.
136    pub async fn list(&self, opts: Option<QueryOptions>) -> Result<Vec<Deployment>, ClientError> {
137        let req = self.client.set_request_query_options(
138            self.client.build_request(Method::GET, "/v1/deployments"),
139            &opts.unwrap_or_default(),
140        );
141        self.client.send_with_response::<Vec<Deployment>>(req).await
142    }
143
144    /// Promote a deployment to the next stage.
145    ///
146    /// # Arguments
147    /// * `deployment_promote_request` - The request containing the deployment
148    ///   ID, all flag, and optional groups to promote.
149    /// * `opts` - Optional write options for the request.
150    ///
151    /// # Returns
152    /// A `Result` containing the deployment update response or an error if the
153    /// request fails.
154    pub async fn promote(
155        &self,
156        deployment_promote_request: DeploymentPromoteRequest,
157        opts: Option<WriteOptions>,
158    ) -> Result<DeploymentUpdateResponse, ClientError> {
159        let req = self
160            .client
161            .set_request_write_options(
162                self.client.build_request(
163                    Method::POST,
164                    &format!(
165                        "/v1/deployment/promote/{}",
166                        deployment_promote_request.deployment_id
167                    ),
168                ),
169                &opts.unwrap_or_default(),
170            )
171            .json(&deployment_promote_request);
172        self.client
173            .send_with_response::<DeploymentUpdateResponse>(req)
174            .await
175    }
176
177    /// Pause or resume a deployment.
178    ///
179    /// # Arguments
180    /// * `deployment_pause_request` - The request containing the deployment ID
181    ///   and pause status.
182    /// * `opts` - Optional write options for the request.
183    ///
184    /// # Returns
185    /// A `Result` containing the deployment update response or an error if the
186    /// request fails.
187    pub async fn set_pause(
188        &self,
189        deployment_pause_request: DeploymentPauseRequest,
190        opts: Option<WriteOptions>,
191    ) -> Result<DeploymentUpdateResponse, ClientError> {
192        let req = self
193            .client
194            .set_request_write_options(
195                self.client.build_request(
196                    Method::POST,
197                    &format!(
198                        "/v1/deployment/progress/{}",
199                        deployment_pause_request.deployment_id
200                    ),
201                ),
202                &opts.unwrap_or_default(),
203            )
204            .json(&deployment_pause_request);
205        self.client
206            .send_with_response::<DeploymentUpdateResponse>(req)
207            .await
208    }
209}