Skip to main content

openstack_sdk_identity/v3/system/user/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//! Check if a specific user has a role assignment on the system.
19//!
20//! Relationship:
21//! `https://docs.openstack.org/api/openstack-identity/3/rel/system_user_role`
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    /// role_id parameter for /v3/system/users/{user_id}/roles/{role_id} API
34    #[builder(default, setter(into))]
35    id: Cow<'a, str>,
36
37    /// user_id parameter for /v3/system/users/{user_id}/roles/{role_id} API
38    #[builder(default, setter(into))]
39    user_id: 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::HEAD
82    }
83
84    fn endpoint(&self) -> Cow<'static, str> {
85        format!(
86            "system/users/{user_id}/roles/{id}",
87            id = self.id.as_ref(),
88            user_id = self.user_id.as_ref(),
89        )
90        .into()
91    }
92
93    fn parameters(&self) -> QueryParams<'_> {
94        QueryParams::default()
95    }
96
97    fn service_type(&self) -> ServiceType {
98        ServiceType::Identity
99    }
100
101    fn response_key(&self) -> Option<Cow<'static, str>> {
102        None
103    }
104
105    /// Returns headers to be set into the request
106    fn request_headers(&self) -> Option<&HeaderMap> {
107        self._headers.as_ref()
108    }
109
110    /// Returns required API version
111    fn api_version(&self) -> Option<ApiVersion> {
112        Some(ApiVersion::new(3, 0))
113    }
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119    use http::{HeaderName, HeaderValue};
120    use httpmock::MockServer;
121    #[cfg(feature = "sync")]
122    use openstack_sdk_core::api::RawQuery;
123    use openstack_sdk_core::test::client::FakeOpenStackClient;
124    use openstack_sdk_core::types::ServiceType;
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!(Request::builder().build().unwrap().response_key().is_none())
137    }
138
139    #[cfg(feature = "sync")]
140    #[test]
141    fn endpoint() {
142        let server = MockServer::start();
143        let client = FakeOpenStackClient::new(server.base_url());
144        let mock = server.mock(|when, then| {
145            when.method(httpmock::Method::HEAD).path(format!(
146                "/system/users/{user_id}/roles/{id}",
147                id = "id",
148                user_id = "user_id",
149            ));
150
151            then.status(200).header("content-type", "application/json");
152        });
153
154        let endpoint = Request::builder()
155            .id("id")
156            .user_id("user_id")
157            .build()
158            .unwrap();
159        let _ = endpoint.raw_query(&client).unwrap();
160        mock.assert();
161    }
162
163    #[cfg(feature = "sync")]
164    #[test]
165    fn endpoint_headers() {
166        let server = MockServer::start();
167        let client = FakeOpenStackClient::new(server.base_url());
168        let mock = server.mock(|when, then| {
169            when.method(httpmock::Method::HEAD)
170                .path(format!(
171                    "/system/users/{user_id}/roles/{id}",
172                    id = "id",
173                    user_id = "user_id",
174                ))
175                .header("foo", "bar")
176                .header("not_foo", "not_bar");
177            then.status(200).header("content-type", "application/json");
178        });
179
180        let endpoint = Request::builder()
181            .id("id")
182            .user_id("user_id")
183            .headers(
184                [(
185                    Some(HeaderName::from_static("foo")),
186                    HeaderValue::from_static("bar"),
187                )]
188                .into_iter(),
189            )
190            .header(
191                HeaderName::from_static("not_foo"),
192                HeaderValue::from_static("not_bar"),
193            )
194            .build()
195            .unwrap();
196        let _ = endpoint.raw_query(&client).unwrap();
197        mock.assert();
198    }
199}