Skip to main content

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