gitlab/api/projects/variables/
delete.rs

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
7use derive_builder::Builder;
8
9use crate::api::common::{self, NameOrId};
10use crate::api::endpoint_prelude::*;
11use crate::api::projects::variables::ProjectVariableFilter;
12
13/// Delete a variable of a project.
14#[derive(Debug, Builder, Clone)]
15#[builder(setter(strip_option))]
16pub struct DeleteProjectVariable<'a> {
17    /// The project to remove the variable from.
18    #[builder(setter(into))]
19    project: NameOrId<'a>,
20    /// The name of the variable.
21    #[builder(setter(into))]
22    key: Cow<'a, str>,
23    /// The environment scope filter of the variable.
24    #[builder(default)]
25    filter: Option<ProjectVariableFilter<'a>>,
26}
27
28impl<'a> DeleteProjectVariable<'a> {
29    /// Create a builder for the endpoint.
30    pub fn builder() -> DeleteProjectVariableBuilder<'a> {
31        DeleteProjectVariableBuilder::default()
32    }
33}
34
35impl Endpoint for DeleteProjectVariable<'_> {
36    fn method(&self) -> Method {
37        Method::DELETE
38    }
39
40    fn endpoint(&self) -> Cow<'static, str> {
41        format!(
42            "projects/{}/variables/{}",
43            self.project,
44            common::path_escaped(&self.key),
45        )
46        .into()
47    }
48
49    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
50        let mut params = FormParams::default();
51
52        if let Some(filter) = self.filter.as_ref() {
53            filter.add_query(&mut params);
54        }
55
56        params.into_body()
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use http::Method;
63
64    use crate::api::projects::variables::delete::{
65        DeleteProjectVariable, DeleteProjectVariableBuilderError, ProjectVariableFilter,
66    };
67    use crate::api::{self, Query};
68    use crate::test::client::{ExpectedUrl, SingleTestClient};
69
70    #[test]
71    fn all_parameters_are_needed() {
72        let err = DeleteProjectVariable::builder().build().unwrap_err();
73        crate::test::assert_missing_field!(err, DeleteProjectVariableBuilderError, "project");
74    }
75
76    #[test]
77    fn project_is_necessary() {
78        let err = DeleteProjectVariable::builder()
79            .key("testkey")
80            .build()
81            .unwrap_err();
82        crate::test::assert_missing_field!(err, DeleteProjectVariableBuilderError, "project");
83    }
84
85    #[test]
86    fn key_is_necessary() {
87        let err = DeleteProjectVariable::builder()
88            .project(1)
89            .build()
90            .unwrap_err();
91        crate::test::assert_missing_field!(err, DeleteProjectVariableBuilderError, "key");
92    }
93
94    #[test]
95    fn sufficient_parameters() {
96        DeleteProjectVariable::builder()
97            .project(1)
98            .key("testkey")
99            .build()
100            .unwrap();
101    }
102
103    #[test]
104    fn endpoint() {
105        let endpoint = ExpectedUrl::builder()
106            .method(Method::DELETE)
107            .endpoint("projects/simple%2Fproject/variables/testkey")
108            .content_type("application/x-www-form-urlencoded")
109            .build()
110            .unwrap();
111        let client = SingleTestClient::new_raw(endpoint, "");
112
113        let endpoint = DeleteProjectVariable::builder()
114            .project("simple/project")
115            .key("testkey")
116            .build()
117            .unwrap();
118        api::ignore(endpoint).query(&client).unwrap();
119    }
120
121    #[test]
122    fn endpoint_filter() {
123        let endpoint = ExpectedUrl::builder()
124            .method(Method::DELETE)
125            .endpoint("projects/simple%2Fproject/variables/testkey")
126            .content_type("application/x-www-form-urlencoded")
127            .body_str("filter%5Benvironment_scope%5D=production")
128            .build()
129            .unwrap();
130        let client = SingleTestClient::new_raw(endpoint, "");
131
132        let endpoint = DeleteProjectVariable::builder()
133            .project("simple/project")
134            .key("testkey")
135            .filter(
136                ProjectVariableFilter::builder()
137                    .environment_scope("production")
138                    .build()
139                    .unwrap(),
140            )
141            .build()
142            .unwrap();
143        api::ignore(endpoint).query(&client).unwrap();
144    }
145}