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