Skip to main content

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