gitlab/api/projects/environments/
environments.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::NameOrId;
10use crate::api::endpoint_prelude::*;
11use crate::api::ParamValue;
12
13#[derive(Debug, Clone)]
14enum NameOrSearch<'a> {
15    Name(Cow<'a, str>),
16    Search(Cow<'a, str>),
17}
18
19/// States of environments.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21#[non_exhaustive]
22pub enum EnvironmentState {
23    /// Environments that have been deployed and are available.
24    Available,
25    /// Environments that are being stopped.
26    Stopping,
27    /// Environments that have been stopped.
28    Stopped,
29}
30
31impl EnvironmentState {
32    fn as_str(self) -> &'static str {
33        match self {
34            EnvironmentState::Available => "available",
35            EnvironmentState::Stopping => "stopping",
36            EnvironmentState::Stopped => "stopped",
37        }
38    }
39}
40
41impl ParamValue<'static> for EnvironmentState {
42    fn as_value(&self) -> Cow<'static, str> {
43        self.as_str().into()
44    }
45}
46
47/// Query for environments within a project.
48#[derive(Debug, Builder, Clone)]
49#[builder(setter(strip_option))]
50pub struct Environments<'a> {
51    /// The project to query for environments.
52    #[builder(setter(into))]
53    project: NameOrId<'a>,
54
55    #[builder(setter(name = "_name_or_search"), default, private)]
56    name_or_search: Option<NameOrSearch<'a>>,
57    /// Filter by the state of the environment.
58    ///
59    /// Note that even though the parameter is plural, it only supports a single value.
60    #[builder(setter(into), default)]
61    states: Option<EnvironmentState>,
62}
63
64impl<'a> Environments<'a> {
65    /// Create a builder for the endpoint.
66    pub fn builder() -> EnvironmentsBuilder<'a> {
67        EnvironmentsBuilder::default()
68    }
69}
70
71impl<'a> EnvironmentsBuilder<'a> {
72    /// Filter environments matching a name.
73    ///
74    /// Mutually exclusive with `search`.
75    pub fn name<N>(&mut self, name: N) -> &mut Self
76    where
77        N: Into<Cow<'a, str>>,
78    {
79        self.name_or_search = Some(Some(NameOrSearch::Name(name.into())));
80        self
81    }
82
83    /// Filter environments matching a search criteria.
84    ///
85    /// Mutually exclusive with `name`.
86    pub fn search<S>(&mut self, search: S) -> &mut Self
87    where
88        S: Into<Cow<'a, str>>,
89    {
90        self.name_or_search = Some(Some(NameOrSearch::Search(search.into())));
91        self
92    }
93}
94
95impl Endpoint for Environments<'_> {
96    fn method(&self) -> Method {
97        Method::GET
98    }
99
100    fn endpoint(&self) -> Cow<'static, str> {
101        format!("projects/{}/environments", self.project).into()
102    }
103
104    fn parameters(&self) -> QueryParams<'_> {
105        let mut params = QueryParams::default();
106
107        params.push_opt("states", self.states);
108
109        if let Some(name_or_search) = self.name_or_search.as_ref() {
110            match name_or_search {
111                NameOrSearch::Name(name) => {
112                    params.push("name", name);
113                },
114                NameOrSearch::Search(search) => {
115                    params.push("search", search);
116                },
117            }
118        }
119
120        params
121    }
122}
123
124impl Pageable for Environments<'_> {}
125
126#[cfg(test)]
127mod tests {
128    use crate::api::projects::environments::{
129        EnvironmentState, Environments, EnvironmentsBuilderError,
130    };
131    use crate::api::{self, Query};
132    use crate::test::client::{ExpectedUrl, SingleTestClient};
133
134    #[test]
135    fn environment_state_as_str() {
136        let items = &[
137            (EnvironmentState::Available, "available"),
138            (EnvironmentState::Stopped, "stopped"),
139        ];
140
141        for (i, s) in items {
142            assert_eq!(i.as_str(), *s);
143        }
144    }
145
146    #[test]
147    fn project_is_needed() {
148        let err = Environments::builder().build().unwrap_err();
149        crate::test::assert_missing_field!(err, EnvironmentsBuilderError, "project");
150    }
151
152    #[test]
153    fn project_is_sufficient() {
154        Environments::builder().project(1).build().unwrap();
155    }
156
157    #[test]
158    fn endpoint() {
159        let endpoint = ExpectedUrl::builder()
160            .endpoint("projects/1/environments")
161            .build()
162            .unwrap();
163        let client = SingleTestClient::new_raw(endpoint, "");
164
165        let endpoint = Environments::builder().project(1).build().unwrap();
166        api::ignore(endpoint).query(&client).unwrap();
167    }
168
169    #[test]
170    fn endpoint_name() {
171        let endpoint = ExpectedUrl::builder()
172            .endpoint("projects/simple%2Fproject/environments")
173            .add_query_params(&[("name", "name")])
174            .build()
175            .unwrap();
176        let client = SingleTestClient::new_raw(endpoint, "");
177
178        let endpoint = Environments::builder()
179            .project("simple/project")
180            .name("name")
181            .build()
182            .unwrap();
183        api::ignore(endpoint).query(&client).unwrap();
184    }
185
186    #[test]
187    fn endpoint_search() {
188        let endpoint = ExpectedUrl::builder()
189            .endpoint("projects/simple%2Fproject/environments")
190            .add_query_params(&[("search", "query")])
191            .build()
192            .unwrap();
193        let client = SingleTestClient::new_raw(endpoint, "");
194
195        let endpoint = Environments::builder()
196            .project("simple/project")
197            .search("query")
198            .build()
199            .unwrap();
200        api::ignore(endpoint).query(&client).unwrap();
201    }
202
203    #[test]
204    fn endpoint_state() {
205        let endpoint = ExpectedUrl::builder()
206            .endpoint("projects/simple%2Fproject/environments")
207            .add_query_params(&[("states", "available")])
208            .build()
209            .unwrap();
210        let client = SingleTestClient::new_raw(endpoint, "");
211
212        let endpoint = Environments::builder()
213            .project("simple/project")
214            .states(EnvironmentState::Available)
215            .build()
216            .unwrap();
217        api::ignore(endpoint).query(&client).unwrap();
218    }
219}