Skip to main content

openstack_sdk_identity/v3/auth/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//! New in version 3.3
19//!
20//! This call returns the list of projects that are available to be scoped to
21//! based on the X-Auth-Token provided in the request.
22//!
23//! The structure of the response is exactly the same as listing projects for a
24//! user.
25//!
26//! Relationship:
27//! `https://docs.openstack.org/api/openstack-identity/3/rel/auth_projects`
28//!
29use derive_builder::Builder;
30use http::{HeaderMap, HeaderName, HeaderValue};
31
32use openstack_sdk_core::api::rest_endpoint_prelude::*;
33
34#[derive(Builder, Debug, Clone)]
35#[builder(setter(strip_option))]
36pub struct Request {
37    #[builder(setter(name = "_headers"), default, private)]
38    _headers: Option<HeaderMap>,
39}
40impl Request {
41    /// Create a builder for the endpoint.
42    pub fn builder() -> RequestBuilder {
43        RequestBuilder::default()
44    }
45}
46
47impl RequestBuilder {
48    /// Add a single header to the Project.
49    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
50    where
51        K: Into<HeaderName>,
52        V: Into<HeaderValue>,
53    {
54        self._headers
55            .get_or_insert(None)
56            .get_or_insert_with(HeaderMap::new)
57            .insert(header_name.into(), header_value.into());
58        self
59    }
60
61    /// Add multiple headers.
62    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
63    where
64        I: Iterator<Item = T>,
65        T: Into<(Option<HeaderName>, HeaderValue)>,
66    {
67        self._headers
68            .get_or_insert(None)
69            .get_or_insert_with(HeaderMap::new)
70            .extend(iter.map(Into::into));
71        self
72    }
73}
74
75impl RestEndpoint for Request {
76    fn method(&self) -> http::Method {
77        http::Method::GET
78    }
79
80    fn endpoint(&self) -> Cow<'static, str> {
81        "auth/projects".to_string().into()
82    }
83
84    fn parameters(&self) -> QueryParams<'_> {
85        QueryParams::default()
86    }
87
88    fn service_type(&self) -> ServiceType {
89        ServiceType::Identity
90    }
91
92    fn response_key(&self) -> Option<Cow<'static, str>> {
93        Some("projects".into())
94    }
95
96    /// Returns headers to be set into the request
97    fn request_headers(&self) -> Option<&HeaderMap> {
98        self._headers.as_ref()
99    }
100
101    /// Returns required API version
102    fn api_version(&self) -> Option<ApiVersion> {
103        Some(ApiVersion::new(3, 0))
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110    use http::{HeaderName, HeaderValue};
111    use httpmock::MockServer;
112    #[cfg(feature = "sync")]
113    use openstack_sdk_core::api::Query;
114    use openstack_sdk_core::test::client::FakeOpenStackClient;
115    use openstack_sdk_core::types::ServiceType;
116    use serde_json::json;
117
118    #[test]
119    fn test_service_type() {
120        assert_eq!(
121            Request::builder().build().unwrap().service_type(),
122            ServiceType::Identity
123        );
124    }
125
126    #[test]
127    fn test_response_key() {
128        assert_eq!(
129            Request::builder().build().unwrap().response_key().unwrap(),
130            "projects"
131        );
132    }
133
134    #[cfg(feature = "sync")]
135    #[test]
136    fn endpoint() {
137        let server = MockServer::start();
138        let client = FakeOpenStackClient::new(server.base_url());
139        let mock = server.mock(|when, then| {
140            when.method(httpmock::Method::GET)
141                .path("/auth/projects".to_string());
142
143            then.status(200)
144                .header("content-type", "application/json")
145                .json_body(json!({ "projects": {} }));
146        });
147
148        let endpoint = Request::builder().build().unwrap();
149        let _: serde_json::Value = endpoint.query(&client).unwrap();
150        mock.assert();
151    }
152
153    #[cfg(feature = "sync")]
154    #[test]
155    fn endpoint_headers() {
156        let server = MockServer::start();
157        let client = FakeOpenStackClient::new(server.base_url());
158        let mock = server.mock(|when, then| {
159            when.method(httpmock::Method::GET)
160                .path("/auth/projects".to_string())
161                .header("foo", "bar")
162                .header("not_foo", "not_bar");
163            then.status(200)
164                .header("content-type", "application/json")
165                .json_body(json!({ "projects": {} }));
166        });
167
168        let endpoint = Request::builder()
169            .headers(
170                [(
171                    Some(HeaderName::from_static("foo")),
172                    HeaderValue::from_static("bar"),
173                )]
174                .into_iter(),
175            )
176            .header(
177                HeaderName::from_static("not_foo"),
178                HeaderValue::from_static("not_bar"),
179            )
180            .build()
181            .unwrap();
182        let _: serde_json::Value = endpoint.query(&client).unwrap();
183        mock.assert();
184    }
185}