Skip to main content

openstack_sdk_identity/v4/federation/mapping/
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
18use derive_builder::Builder;
19use http::{HeaderMap, HeaderName, HeaderValue};
20
21use openstack_sdk_core::api::rest_endpoint_prelude::*;
22
23use std::borrow::Cow;
24
25#[derive(Builder, Debug, Clone)]
26#[builder(setter(strip_option))]
27pub struct Request<'a> {
28    /// The ID of the attribute mapping
29    #[builder(default, setter(into))]
30    id: Cow<'a, str>,
31
32    #[builder(setter(name = "_headers"), default, private)]
33    _headers: Option<HeaderMap>,
34}
35impl<'a> Request<'a> {
36    /// Create a builder for the endpoint.
37    pub fn builder() -> RequestBuilder<'a> {
38        RequestBuilder::default()
39    }
40}
41
42impl<'a> RequestBuilder<'a> {
43    /// Add a single header to the Mapping.
44    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
45    where
46        K: Into<HeaderName>,
47        V: Into<HeaderValue>,
48    {
49        self._headers
50            .get_or_insert(None)
51            .get_or_insert_with(HeaderMap::new)
52            .insert(header_name.into(), header_value.into());
53        self
54    }
55
56    /// Add multiple headers.
57    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
58    where
59        I: Iterator<Item = T>,
60        T: Into<(Option<HeaderName>, HeaderValue)>,
61    {
62        self._headers
63            .get_or_insert(None)
64            .get_or_insert_with(HeaderMap::new)
65            .extend(iter.map(Into::into));
66        self
67    }
68}
69
70impl RestEndpoint for Request<'_> {
71    fn method(&self) -> http::Method {
72        http::Method::DELETE
73    }
74
75    fn endpoint(&self) -> Cow<'static, str> {
76        format!("federation/mappings/{id}", id = self.id.as_ref(),).into()
77    }
78
79    fn parameters(&self) -> QueryParams<'_> {
80        QueryParams::default()
81    }
82
83    fn service_type(&self) -> ServiceType {
84        ServiceType::Identity
85    }
86
87    fn response_key(&self) -> Option<Cow<'static, str>> {
88        None
89    }
90
91    /// Returns headers to be set into the request
92    fn request_headers(&self) -> Option<&HeaderMap> {
93        self._headers.as_ref()
94    }
95
96    /// Returns required API version
97    fn api_version(&self) -> Option<ApiVersion> {
98        Some(ApiVersion::new(4, 0))
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105    use http::{HeaderName, HeaderValue};
106    use httpmock::MockServer;
107    #[cfg(feature = "sync")]
108    use openstack_sdk_core::api::Query;
109    use openstack_sdk_core::test::client::FakeOpenStackClient;
110    use openstack_sdk_core::types::ServiceType;
111    use serde_json::json;
112
113    #[test]
114    fn test_service_type() {
115        assert_eq!(
116            Request::builder().build().unwrap().service_type(),
117            ServiceType::Identity
118        );
119    }
120
121    #[test]
122    fn test_response_key() {
123        assert!(Request::builder().build().unwrap().response_key().is_none())
124    }
125
126    #[cfg(feature = "sync")]
127    #[test]
128    fn endpoint() {
129        let server = MockServer::start();
130        let client = FakeOpenStackClient::new(server.base_url());
131        let mock = server.mock(|when, then| {
132            when.method(httpmock::Method::DELETE)
133                .path(format!("/federation/mappings/{id}", id = "id",));
134
135            then.status(200)
136                .header("content-type", "application/json")
137                .json_body(json!({ "dummy": {} }));
138        });
139
140        let endpoint = Request::builder().id("id").build().unwrap();
141        let _: serde_json::Value = endpoint.query(&client).unwrap();
142        mock.assert();
143    }
144
145    #[cfg(feature = "sync")]
146    #[test]
147    fn endpoint_headers() {
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::DELETE)
152                .path(format!("/federation/mappings/{id}", id = "id",))
153                .header("foo", "bar")
154                .header("not_foo", "not_bar");
155            then.status(200)
156                .header("content-type", "application/json")
157                .json_body(json!({ "dummy": {} }));
158        });
159
160        let endpoint = Request::builder()
161            .id("id")
162            .headers(
163                [(
164                    Some(HeaderName::from_static("foo")),
165                    HeaderValue::from_static("bar"),
166                )]
167                .into_iter(),
168            )
169            .header(
170                HeaderName::from_static("not_foo"),
171                HeaderValue::from_static("not_bar"),
172            )
173            .build()
174            .unwrap();
175        let _: serde_json::Value = endpoint.query(&client).unwrap();
176        mock.assert();
177    }
178}