Skip to main content

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