Skip to main content

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