Skip to main content

openstack_sdk_identity/v3/domain/
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//! Deletes a domain. To minimize the risk of accidentally deleting a domain,
19//! you must first disable the domain by using the update domain method.
20//!
21//! When you delete a domain, this call also deletes all entities owned by it,
22//! such as users, groups, and projects, and any credentials and granted roles
23//! that relate to those entities.
24//!
25//! If you try to delete an enabled domain, this call returns the
26//! `Forbidden (403)` response code.
27//!
28//! Relationship:
29//! `https://docs.openstack.org/api/openstack-identity/3/rel/domain`
30//!
31use derive_builder::Builder;
32use http::{HeaderMap, HeaderName, HeaderValue};
33
34use openstack_sdk_core::api::rest_endpoint_prelude::*;
35
36use std::borrow::Cow;
37
38#[derive(Builder, Debug, Clone)]
39#[builder(setter(strip_option))]
40pub struct Request<'a> {
41    /// domain_id parameter for /v3/domains/{domain_id} API
42    #[builder(default, setter(into))]
43    id: Cow<'a, str>,
44
45    #[builder(setter(name = "_headers"), default, private)]
46    _headers: Option<HeaderMap>,
47}
48impl<'a> Request<'a> {
49    /// Create a builder for the endpoint.
50    pub fn builder() -> RequestBuilder<'a> {
51        RequestBuilder::default()
52    }
53}
54
55impl<'a> RequestBuilder<'a> {
56    /// Add a single header to the Domain.
57    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
58    where
59        K: Into<HeaderName>,
60        V: Into<HeaderValue>,
61    {
62        self._headers
63            .get_or_insert(None)
64            .get_or_insert_with(HeaderMap::new)
65            .insert(header_name.into(), header_value.into());
66        self
67    }
68
69    /// Add multiple headers.
70    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
71    where
72        I: Iterator<Item = T>,
73        T: Into<(Option<HeaderName>, HeaderValue)>,
74    {
75        self._headers
76            .get_or_insert(None)
77            .get_or_insert_with(HeaderMap::new)
78            .extend(iter.map(Into::into));
79        self
80    }
81}
82
83impl RestEndpoint for Request<'_> {
84    fn method(&self) -> http::Method {
85        http::Method::DELETE
86    }
87
88    fn endpoint(&self) -> Cow<'static, str> {
89        format!("domains/{id}", id = self.id.as_ref(),).into()
90    }
91
92    fn parameters(&self) -> QueryParams<'_> {
93        QueryParams::default()
94    }
95
96    fn service_type(&self) -> ServiceType {
97        ServiceType::Identity
98    }
99
100    fn response_key(&self) -> Option<Cow<'static, str>> {
101        None
102    }
103
104    /// Returns headers to be set into the request
105    fn request_headers(&self) -> Option<&HeaderMap> {
106        self._headers.as_ref()
107    }
108
109    /// Returns required API version
110    fn api_version(&self) -> Option<ApiVersion> {
111        Some(ApiVersion::new(3, 0))
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118    use http::{HeaderName, HeaderValue};
119    use httpmock::MockServer;
120    #[cfg(feature = "sync")]
121    use openstack_sdk_core::api::Query;
122    use openstack_sdk_core::test::client::FakeOpenStackClient;
123    use openstack_sdk_core::types::ServiceType;
124    use serde_json::json;
125
126    #[test]
127    fn test_service_type() {
128        assert_eq!(
129            Request::builder().build().unwrap().service_type(),
130            ServiceType::Identity
131        );
132    }
133
134    #[test]
135    fn test_response_key() {
136        assert!(Request::builder().build().unwrap().response_key().is_none())
137    }
138
139    #[cfg(feature = "sync")]
140    #[test]
141    fn endpoint() {
142        let server = MockServer::start();
143        let client = FakeOpenStackClient::new(server.base_url());
144        let mock = server.mock(|when, then| {
145            when.method(httpmock::Method::DELETE)
146                .path(format!("/domains/{id}", id = "id",));
147
148            then.status(200)
149                .header("content-type", "application/json")
150                .json_body(json!({ "dummy": {} }));
151        });
152
153        let endpoint = Request::builder().id("id").build().unwrap();
154        let _: serde_json::Value = endpoint.query(&client).unwrap();
155        mock.assert();
156    }
157
158    #[cfg(feature = "sync")]
159    #[test]
160    fn endpoint_headers() {
161        let server = MockServer::start();
162        let client = FakeOpenStackClient::new(server.base_url());
163        let mock = server.mock(|when, then| {
164            when.method(httpmock::Method::DELETE)
165                .path(format!("/domains/{id}", id = "id",))
166                .header("foo", "bar")
167                .header("not_foo", "not_bar");
168            then.status(200)
169                .header("content-type", "application/json")
170                .json_body(json!({ "dummy": {} }));
171        });
172
173        let endpoint = Request::builder()
174            .id("id")
175            .headers(
176                [(
177                    Some(HeaderName::from_static("foo")),
178                    HeaderValue::from_static("bar"),
179                )]
180                .into_iter(),
181            )
182            .header(
183                HeaderName::from_static("not_foo"),
184                HeaderValue::from_static("not_bar"),
185            )
186            .build()
187            .unwrap();
188        let _: serde_json::Value = endpoint.query(&client).unwrap();
189        mock.assert();
190    }
191}