|
| 1 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 | +// option. This file may not be copied, modified, or distributed |
| 5 | +// except according to those terms. |
| 6 | + |
| 7 | +use derive_builder::Builder; |
| 8 | + |
| 9 | +use crate::api::common::NameOrId; |
| 10 | +use crate::api::endpoint_prelude::*; |
| 11 | +use crate::api::ParamValue; |
| 12 | + |
| 13 | +/// The status of a deployment. |
| 14 | +#[derive(Debug, Clone, Copy)] |
| 15 | +#[non_exhaustive] |
| 16 | +pub enum DeploymentStatus { |
| 17 | + /// The deployment is running. |
| 18 | + Running, |
| 19 | + /// The deployment succeeded. |
| 20 | + Success, |
| 21 | + /// The deployment failed. |
| 22 | + Failed, |
| 23 | + /// The deployment is canceled. |
| 24 | + Canceled, |
| 25 | +} |
| 26 | + |
| 27 | +impl DeploymentStatus { |
| 28 | + fn as_str(self) -> &'static str { |
| 29 | + match self { |
| 30 | + Self::Running => "running", |
| 31 | + Self::Success => "success", |
| 32 | + Self::Failed => "failed", |
| 33 | + Self::Canceled => "canceled", |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl ParamValue<'static> for DeploymentStatus { |
| 39 | + fn as_value(&self) -> Cow<'static, str> { |
| 40 | + self.as_str().into() |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// Edit a deployment for a project. |
| 45 | +#[derive(Debug, Builder, Clone)] |
| 46 | +pub struct EditDeployment<'a> { |
| 47 | + /// The project to edit a deployment from. |
| 48 | + #[builder(setter(into))] |
| 49 | + project: NameOrId<'a>, |
| 50 | + /// The ID of the deployment to edit. |
| 51 | + deployment_id: u64, |
| 52 | + /// The status of the deployment. |
| 53 | + status: DeploymentStatus, |
| 54 | +} |
| 55 | + |
| 56 | +impl<'a> EditDeployment<'a> { |
| 57 | + /// Create a builder for the endpoint. |
| 58 | + pub fn builder() -> EditDeploymentBuilder<'a> { |
| 59 | + EditDeploymentBuilder::default() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<'a> Endpoint for EditDeployment<'a> { |
| 64 | + fn method(&self) -> Method { |
| 65 | + Method::PUT |
| 66 | + } |
| 67 | + |
| 68 | + fn endpoint(&self) -> Cow<'static, str> { |
| 69 | + format!( |
| 70 | + "projects/{}/deployments/{}", |
| 71 | + self.project, self.deployment_id, |
| 72 | + ) |
| 73 | + .into() |
| 74 | + } |
| 75 | + |
| 76 | + fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> { |
| 77 | + let mut params = FormParams::default(); |
| 78 | + |
| 79 | + params.push("status", self.status); |
| 80 | + |
| 81 | + params.into_body() |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod tests { |
| 87 | + use http::Method; |
| 88 | + |
| 89 | + use crate::api::projects::deployments::{ |
| 90 | + DeploymentStatus, EditDeployment, EditDeploymentBuilderError, |
| 91 | + }; |
| 92 | + use crate::api::{self, Query}; |
| 93 | + use crate::test::client::{ExpectedUrl, SingleTestClient}; |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn deployment_status_as_str() { |
| 97 | + let items = &[ |
| 98 | + (DeploymentStatus::Running, "running"), |
| 99 | + (DeploymentStatus::Success, "success"), |
| 100 | + (DeploymentStatus::Failed, "failed"), |
| 101 | + (DeploymentStatus::Canceled, "canceled"), |
| 102 | + ]; |
| 103 | + |
| 104 | + for (i, s) in items { |
| 105 | + assert_eq!(i.as_str(), *s); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn project_deployment_id_and_status_are_necessary() { |
| 111 | + let err = EditDeployment::builder().build().unwrap_err(); |
| 112 | + crate::test::assert_missing_field!(err, EditDeploymentBuilderError, "project"); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn project_is_necessary() { |
| 117 | + let err = EditDeployment::builder() |
| 118 | + .deployment_id(1) |
| 119 | + .status(DeploymentStatus::Failed) |
| 120 | + .build() |
| 121 | + .unwrap_err(); |
| 122 | + crate::test::assert_missing_field!(err, EditDeploymentBuilderError, "project"); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn deployment_id_is_necessary() { |
| 127 | + let err = EditDeployment::builder() |
| 128 | + .project("project") |
| 129 | + .status(DeploymentStatus::Failed) |
| 130 | + .build() |
| 131 | + .unwrap_err(); |
| 132 | + crate::test::assert_missing_field!(err, EditDeploymentBuilderError, "deployment_id"); |
| 133 | + } |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn status_is_necessary() { |
| 137 | + let err = EditDeployment::builder() |
| 138 | + .project("project") |
| 139 | + .deployment_id(1) |
| 140 | + .build() |
| 141 | + .unwrap_err(); |
| 142 | + crate::test::assert_missing_field!(err, EditDeploymentBuilderError, "status"); |
| 143 | + } |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn sufficient_parameters() { |
| 147 | + EditDeployment::builder() |
| 148 | + .project("project") |
| 149 | + .deployment_id(1) |
| 150 | + .status(DeploymentStatus::Success) |
| 151 | + .build() |
| 152 | + .unwrap(); |
| 153 | + } |
| 154 | + |
| 155 | + #[test] |
| 156 | + fn endpoint() { |
| 157 | + let endpoint = ExpectedUrl::builder() |
| 158 | + .method(Method::PUT) |
| 159 | + .endpoint("projects/simple%2Fproject/deployments/1") |
| 160 | + .content_type("application/x-www-form-urlencoded") |
| 161 | + .body_str("status=canceled") |
| 162 | + .build() |
| 163 | + .unwrap(); |
| 164 | + let client = SingleTestClient::new_raw(endpoint, ""); |
| 165 | + |
| 166 | + let endpoint = EditDeployment::builder() |
| 167 | + .project("simple/project") |
| 168 | + .deployment_id(1) |
| 169 | + .status(DeploymentStatus::Canceled) |
| 170 | + .build() |
| 171 | + .unwrap(); |
| 172 | + api::ignore(endpoint).query(&client).unwrap(); |
| 173 | + } |
| 174 | +} |
0 commit comments