Skip to main content

openstack_sdk_identity/v3/project/
list.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14//
15// WARNING: This file is automatically generated from OpenAPI schema using
16// `openstack-codegenerator`.
17
18//! Lists projects.
19//!
20//! Relationship:
21//! `https://docs.openstack.org/api/openstack-identity/3/rel/projects`
22//!
23use derive_builder::Builder;
24use http::{HeaderMap, HeaderName, HeaderValue};
25
26use openstack_sdk_core::api::rest_endpoint_prelude::*;
27
28use std::borrow::Cow;
29
30use openstack_sdk_core::api::Pageable;
31#[derive(Builder, Debug, Clone)]
32#[builder(setter(strip_option))]
33pub struct Request<'a> {
34    /// The ID of the domain.
35    #[builder(default, setter(into))]
36    domain_id: Option<Cow<'a, str>>,
37
38    #[builder(default)]
39    enabled: Option<bool>,
40
41    #[builder(default)]
42    is_domain: Option<bool>,
43
44    #[builder(default)]
45    limit: Option<u32>,
46
47    /// ID of the last fetched entry
48    #[builder(default, setter(into))]
49    marker: Option<Cow<'a, str>>,
50
51    /// The resource name.
52    #[builder(default, setter(into))]
53    name: Option<Cow<'a, str>>,
54
55    #[builder(default, setter(into))]
56    not_tags: Option<Cow<'a, str>>,
57
58    #[builder(default, setter(into))]
59    not_tags_any: Option<Cow<'a, str>>,
60
61    #[builder(default, setter(into))]
62    parent_id: Option<Cow<'a, str>>,
63
64    #[builder(default, setter(into))]
65    tags: Option<Cow<'a, str>>,
66
67    #[builder(default, setter(into))]
68    tags_any: Option<Cow<'a, str>>,
69
70    #[builder(setter(name = "_headers"), default, private)]
71    _headers: Option<HeaderMap>,
72}
73impl<'a> Request<'a> {
74    /// Create a builder for the endpoint.
75    pub fn builder() -> RequestBuilder<'a> {
76        RequestBuilder::default()
77    }
78}
79
80impl<'a> RequestBuilder<'a> {
81    /// Add a single header to the Project.
82    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
83    where
84        K: Into<HeaderName>,
85        V: Into<HeaderValue>,
86    {
87        self._headers
88            .get_or_insert(None)
89            .get_or_insert_with(HeaderMap::new)
90            .insert(header_name.into(), header_value.into());
91        self
92    }
93
94    /// Add multiple headers.
95    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
96    where
97        I: Iterator<Item = T>,
98        T: Into<(Option<HeaderName>, HeaderValue)>,
99    {
100        self._headers
101            .get_or_insert(None)
102            .get_or_insert_with(HeaderMap::new)
103            .extend(iter.map(Into::into));
104        self
105    }
106}
107
108impl RestEndpoint for Request<'_> {
109    fn method(&self) -> http::Method {
110        http::Method::GET
111    }
112
113    fn endpoint(&self) -> Cow<'static, str> {
114        "projects".to_string().into()
115    }
116
117    fn parameters(&self) -> QueryParams<'_> {
118        let mut params = QueryParams::default();
119        params.push_opt("domain_id", self.domain_id.as_ref());
120        params.push_opt("enabled", self.enabled);
121        params.push_opt("is_domain", self.is_domain);
122        params.push_opt("limit", self.limit);
123        params.push_opt("marker", self.marker.as_ref());
124        params.push_opt("name", self.name.as_ref());
125        params.push_opt("not-tags", self.not_tags.as_ref());
126        params.push_opt("not-tags-any", self.not_tags_any.as_ref());
127        params.push_opt("parent_id", self.parent_id.as_ref());
128        params.push_opt("tags", self.tags.as_ref());
129        params.push_opt("tags-any", self.tags_any.as_ref());
130
131        params
132    }
133
134    fn service_type(&self) -> ServiceType {
135        ServiceType::Identity
136    }
137
138    fn response_key(&self) -> Option<Cow<'static, str>> {
139        Some("projects".into())
140    }
141
142    /// Returns headers to be set into the request
143    fn request_headers(&self) -> Option<&HeaderMap> {
144        self._headers.as_ref()
145    }
146
147    /// Returns required API version
148    fn api_version(&self) -> Option<ApiVersion> {
149        Some(ApiVersion::new(3, 0))
150    }
151}
152impl Pageable for Request<'_> {}
153
154#[cfg(test)]
155mod tests {
156    use super::*;
157    use http::{HeaderName, HeaderValue};
158    use httpmock::MockServer;
159    #[cfg(feature = "sync")]
160    use openstack_sdk_core::api::Query;
161    use openstack_sdk_core::test::client::FakeOpenStackClient;
162    use openstack_sdk_core::types::ServiceType;
163    use serde_json::json;
164
165    #[test]
166    fn test_service_type() {
167        assert_eq!(
168            Request::builder().build().unwrap().service_type(),
169            ServiceType::Identity
170        );
171    }
172
173    #[test]
174    fn test_response_key() {
175        assert_eq!(
176            Request::builder().build().unwrap().response_key().unwrap(),
177            "projects"
178        );
179    }
180
181    #[cfg(feature = "sync")]
182    #[test]
183    fn endpoint() {
184        let server = MockServer::start();
185        let client = FakeOpenStackClient::new(server.base_url());
186        let mock = server.mock(|when, then| {
187            when.method(httpmock::Method::GET)
188                .path("/projects".to_string());
189
190            then.status(200)
191                .header("content-type", "application/json")
192                .json_body(json!({ "projects": {} }));
193        });
194
195        let endpoint = Request::builder().build().unwrap();
196        let _: serde_json::Value = endpoint.query(&client).unwrap();
197        mock.assert();
198    }
199
200    #[cfg(feature = "sync")]
201    #[test]
202    fn endpoint_headers() {
203        let server = MockServer::start();
204        let client = FakeOpenStackClient::new(server.base_url());
205        let mock = server.mock(|when, then| {
206            when.method(httpmock::Method::GET)
207                .path("/projects".to_string())
208                .header("foo", "bar")
209                .header("not_foo", "not_bar");
210            then.status(200)
211                .header("content-type", "application/json")
212                .json_body(json!({ "projects": {} }));
213        });
214
215        let endpoint = Request::builder()
216            .headers(
217                [(
218                    Some(HeaderName::from_static("foo")),
219                    HeaderValue::from_static("bar"),
220                )]
221                .into_iter(),
222            )
223            .header(
224                HeaderName::from_static("not_foo"),
225                HeaderValue::from_static("not_bar"),
226            )
227            .build()
228            .unwrap();
229        let _: serde_json::Value = endpoint.query(&client).unwrap();
230        mock.assert();
231    }
232}