Skip to main content

gitlab/api/groups/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::groups::variables::GroupVariableFilter;
12
13/// Delete a variable of a group.
14#[derive(Debug, Builder, Clone)]
15#[builder(setter(strip_option))]
16pub struct DeleteGroupVariable<'a> {
17    /// The group to remove the variable from
18    #[builder(setter(into))]
19    group: 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<GroupVariableFilter<'a>>,
26}
27
28impl<'a> DeleteGroupVariable<'a> {
29    /// Create a builder for the endpoint.
30    pub fn builder() -> DeleteGroupVariableBuilder<'a> {
31        DeleteGroupVariableBuilder::default()
32    }
33}
34
35impl Endpoint for DeleteGroupVariable<'_> {
36    fn method(&self) -> Method {
37        Method::DELETE
38    }
39
40    fn endpoint(&self) -> Cow<'static, str> {
41        format!(
42            "groups/{}/variables/{}",
43            self.group,
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::groups::variables::delete::{
65        DeleteGroupVariable, DeleteGroupVariableBuilderError, GroupVariableFilter,
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 = DeleteGroupVariable::builder().build().unwrap_err();
73        crate::test::assert_missing_field!(err, DeleteGroupVariableBuilderError, "group");
74    }
75
76    #[test]
77    fn group_is_necessary() {
78        let err = DeleteGroupVariable::builder()
79            .key("testkey")
80            .build()
81            .unwrap_err();
82        crate::test::assert_missing_field!(err, DeleteGroupVariableBuilderError, "group");
83    }
84
85    #[test]
86    fn key_is_necessary() {
87        let err = DeleteGroupVariable::builder().group(1).build().unwrap_err();
88        crate::test::assert_missing_field!(err, DeleteGroupVariableBuilderError, "key");
89    }
90
91    #[test]
92    fn sufficient_parameters() {
93        DeleteGroupVariable::builder()
94            .group(1)
95            .key("testkey")
96            .build()
97            .unwrap();
98    }
99
100    #[test]
101    fn endpoint() {
102        let endpoint = ExpectedUrl::builder()
103            .method(Method::DELETE)
104            .endpoint("groups/simple%2Fgroup/variables/testkey")
105            .content_type("application/x-www-form-urlencoded")
106            .build()
107            .unwrap();
108        let client = SingleTestClient::new_raw(endpoint, "");
109
110        let endpoint = DeleteGroupVariable::builder()
111            .group("simple/group")
112            .key("testkey")
113            .build()
114            .unwrap();
115        api::ignore(endpoint).query(&client).unwrap();
116    }
117
118    #[test]
119    fn endpoint_filter() {
120        let endpoint = ExpectedUrl::builder()
121            .method(Method::DELETE)
122            .endpoint("groups/simple%2Fgroup/variables/testkey")
123            .content_type("application/x-www-form-urlencoded")
124            .body_str("filter%5Benvironment_scope%5D=production")
125            .build()
126            .unwrap();
127        let client = SingleTestClient::new_raw(endpoint, "");
128
129        let endpoint = DeleteGroupVariable::builder()
130            .group("simple/group")
131            .key("testkey")
132            .filter(
133                GroupVariableFilter::builder()
134                    .environment_scope("production")
135                    .build()
136                    .unwrap(),
137            )
138            .build()
139            .unwrap();
140        api::ignore(endpoint).query(&client).unwrap();
141    }
142}