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