Skip to main content

openstack_sdk_identity/v3/group/user/
delete.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//! Removes a user from a group.
19//!
20//! Relationship:
21//! `https://docs.openstack.org/api/openstack-identity/3/rel/group_user`
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/groups/{group_id}/users/{user_id} API
34    #[builder(default, setter(into))]
35    group_id: Cow<'a, str>,
36
37    /// user_id parameter for /v3/groups/{group_id}/users/{user_id} API
38    #[builder(default, setter(into))]
39    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 User.
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::DELETE
82    }
83
84    fn endpoint(&self) -> Cow<'static, str> {
85        format!(
86            "groups/{group_id}/users/{id}",
87            group_id = self.group_id.as_ref(),
88            id = self.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::Query;
123    use openstack_sdk_core::test::client::FakeOpenStackClient;
124    use openstack_sdk_core::types::ServiceType;
125    use serde_json::json;
126
127    #[test]
128    fn test_service_type() {
129        assert_eq!(
130            Request::builder().build().unwrap().service_type(),
131            ServiceType::Identity
132        );
133    }
134
135    #[test]
136    fn test_response_key() {
137        assert!(Request::builder().build().unwrap().response_key().is_none())
138    }
139
140    #[cfg(feature = "sync")]
141    #[test]
142    fn endpoint() {
143        let server = MockServer::start();
144        let client = FakeOpenStackClient::new(server.base_url());
145        let mock = server.mock(|when, then| {
146            when.method(httpmock::Method::DELETE).path(format!(
147                "/groups/{group_id}/users/{id}",
148                group_id = "group_id",
149                id = "id",
150            ));
151
152            then.status(200)
153                .header("content-type", "application/json")
154                .json_body(json!({ "dummy": {} }));
155        });
156
157        let endpoint = Request::builder()
158            .group_id("group_id")
159            .id("id")
160            .build()
161            .unwrap();
162        let _: serde_json::Value = endpoint.query(&client).unwrap();
163        mock.assert();
164    }
165
166    #[cfg(feature = "sync")]
167    #[test]
168    fn endpoint_headers() {
169        let server = MockServer::start();
170        let client = FakeOpenStackClient::new(server.base_url());
171        let mock = server.mock(|when, then| {
172            when.method(httpmock::Method::DELETE)
173                .path(format!(
174                    "/groups/{group_id}/users/{id}",
175                    group_id = "group_id",
176                    id = "id",
177                ))
178                .header("foo", "bar")
179                .header("not_foo", "not_bar");
180            then.status(200)
181                .header("content-type", "application/json")
182                .json_body(json!({ "dummy": {} }));
183        });
184
185        let endpoint = Request::builder()
186            .group_id("group_id")
187            .id("id")
188            .headers(
189                [(
190                    Some(HeaderName::from_static("foo")),
191                    HeaderValue::from_static("bar"),
192                )]
193                .into_iter(),
194            )
195            .header(
196                HeaderName::from_static("not_foo"),
197                HeaderValue::from_static("not_bar"),
198            )
199            .build()
200            .unwrap();
201        let _: serde_json::Value = endpoint.query(&client).unwrap();
202        mock.assert();
203    }
204}