Skip to main content

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