openstack_sdk/api/identity/v3/role/imply/
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 implied (inference) roles for a role.
19//!
20//! Relationship:
21//! `https://developer.openstack.org/api-ref/identity/v3/#list-implied-roles-for-role`
22//!
23use derive_builder::Builder;
24use http::{HeaderMap, HeaderName, HeaderValue};
25
26use crate::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    /// prior_role_id parameter for
34    /// /v3/roles/{prior_role_id}/implies/{implied_role_id} API
35    #[builder(default, setter(into))]
36    prior_role_id: Cow<'a, str>,
37
38    #[builder(setter(name = "_headers"), default, private)]
39    _headers: Option<HeaderMap>,
40}
41impl<'a> Request<'a> {
42    /// Create a builder for the endpoint.
43    pub fn builder() -> RequestBuilder<'a> {
44        RequestBuilder::default()
45    }
46}
47
48impl<'a> RequestBuilder<'a> {
49    /// Add a single header to the Imply.
50    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
51    where
52        K: Into<HeaderName>,
53        V: Into<HeaderValue>,
54    {
55        self._headers
56            .get_or_insert(None)
57            .get_or_insert_with(HeaderMap::new)
58            .insert(header_name.into(), header_value.into());
59        self
60    }
61
62    /// Add multiple headers.
63    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
64    where
65        I: Iterator<Item = T>,
66        T: Into<(Option<HeaderName>, HeaderValue)>,
67    {
68        self._headers
69            .get_or_insert(None)
70            .get_or_insert_with(HeaderMap::new)
71            .extend(iter.map(Into::into));
72        self
73    }
74}
75
76impl RestEndpoint for Request<'_> {
77    fn method(&self) -> http::Method {
78        http::Method::GET
79    }
80
81    fn endpoint(&self) -> Cow<'static, str> {
82        format!(
83            "roles/{prior_role_id}/implies",
84            prior_role_id = self.prior_role_id.as_ref(),
85        )
86        .into()
87    }
88
89    fn parameters(&self) -> QueryParams<'_> {
90        QueryParams::default()
91    }
92
93    fn service_type(&self) -> ServiceType {
94        ServiceType::Identity
95    }
96
97    fn response_key(&self) -> Option<Cow<'static, str>> {
98        Some("role_inference".into())
99    }
100
101    /// Returns headers to be set into the request
102    fn request_headers(&self) -> Option<&HeaderMap> {
103        self._headers.as_ref()
104    }
105
106    /// Returns required API version
107    fn api_version(&self) -> Option<ApiVersion> {
108        Some(ApiVersion::new(3, 0))
109    }
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115    #[cfg(feature = "sync")]
116    use crate::api::Query;
117    use crate::test::client::FakeOpenStackClient;
118    use crate::types::ServiceType;
119    use http::{HeaderName, HeaderValue};
120    use httpmock::MockServer;
121    use serde_json::json;
122
123    #[test]
124    fn test_service_type() {
125        assert_eq!(
126            Request::builder().build().unwrap().service_type(),
127            ServiceType::Identity
128        );
129    }
130
131    #[test]
132    fn test_response_key() {
133        assert_eq!(
134            Request::builder().build().unwrap().response_key().unwrap(),
135            "role_inference"
136        );
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::GET).path(format!(
146                "/roles/{prior_role_id}/implies",
147                prior_role_id = "prior_role_id",
148            ));
149
150            then.status(200)
151                .header("content-type", "application/json")
152                .json_body(json!({ "role_inference": {} }));
153        });
154
155        let endpoint = Request::builder()
156            .prior_role_id("prior_role_id")
157            .build()
158            .unwrap();
159        let _: serde_json::Value = endpoint.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::GET)
170                .path(format!(
171                    "/roles/{prior_role_id}/implies",
172                    prior_role_id = "prior_role_id",
173                ))
174                .header("foo", "bar")
175                .header("not_foo", "not_bar");
176            then.status(200)
177                .header("content-type", "application/json")
178                .json_body(json!({ "role_inference": {} }));
179        });
180
181        let endpoint = Request::builder()
182            .prior_role_id("prior_role_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 _: serde_json::Value = endpoint.query(&client).unwrap();
197        mock.assert();
198    }
199}