Skip to main content

openstack_sdk_identity/v3/service/
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 service.
19//!
20//! If you try to delete a service that still has associated endpoints, this
21//! call either deletes all associated endpoints or fails until all endpoints
22//! are deleted.
23//!
24//! Relationship:
25//! `https://docs.openstack.org/api/openstack-identity/3/rel/service`
26//!
27use derive_builder::Builder;
28use http::{HeaderMap, HeaderName, HeaderValue};
29
30use openstack_sdk_core::api::rest_endpoint_prelude::*;
31
32use std::borrow::Cow;
33
34#[derive(Builder, Debug, Clone)]
35#[builder(setter(strip_option))]
36pub struct Request<'a> {
37    /// service_id parameter for /v3/services/{service_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 Service.
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!("services/{id}", id = self.id.as_ref(),).into()
86    }
87
88    fn parameters(&self) -> QueryParams<'_> {
89        QueryParams::default()
90    }
91
92    fn service_type(&self) -> ServiceType {
93        ServiceType::Identity
94    }
95
96    fn response_key(&self) -> Option<Cow<'static, str>> {
97        None
98    }
99
100    /// Returns headers to be set into the request
101    fn request_headers(&self) -> Option<&HeaderMap> {
102        self._headers.as_ref()
103    }
104
105    /// Returns required API version
106    fn api_version(&self) -> Option<ApiVersion> {
107        Some(ApiVersion::new(3, 0))
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114    use http::{HeaderName, HeaderValue};
115    use httpmock::MockServer;
116    #[cfg(feature = "sync")]
117    use openstack_sdk_core::api::Query;
118    use openstack_sdk_core::test::client::FakeOpenStackClient;
119    use openstack_sdk_core::types::ServiceType;
120    use serde_json::json;
121
122    #[test]
123    fn test_service_type() {
124        assert_eq!(
125            Request::builder().build().unwrap().service_type(),
126            ServiceType::Identity
127        );
128    }
129
130    #[test]
131    fn test_response_key() {
132        assert!(Request::builder().build().unwrap().response_key().is_none())
133    }
134
135    #[cfg(feature = "sync")]
136    #[test]
137    fn endpoint() {
138        let server = MockServer::start();
139        let client = FakeOpenStackClient::new(server.base_url());
140        let mock = server.mock(|when, then| {
141            when.method(httpmock::Method::DELETE)
142                .path(format!("/services/{id}", id = "id",));
143
144            then.status(200)
145                .header("content-type", "application/json")
146                .json_body(json!({ "dummy": {} }));
147        });
148
149        let endpoint = Request::builder().id("id").build().unwrap();
150        let _: serde_json::Value = endpoint.query(&client).unwrap();
151        mock.assert();
152    }
153
154    #[cfg(feature = "sync")]
155    #[test]
156    fn endpoint_headers() {
157        let server = MockServer::start();
158        let client = FakeOpenStackClient::new(server.base_url());
159        let mock = server.mock(|when, then| {
160            when.method(httpmock::Method::DELETE)
161                .path(format!("/services/{id}", id = "id",))
162                .header("foo", "bar")
163                .header("not_foo", "not_bar");
164            then.status(200)
165                .header("content-type", "application/json")
166                .json_body(json!({ "dummy": {} }));
167        });
168
169        let endpoint = Request::builder()
170            .id("id")
171            .headers(
172                [(
173                    Some(HeaderName::from_static("foo")),
174                    HeaderValue::from_static("bar"),
175                )]
176                .into_iter(),
177            )
178            .header(
179                HeaderName::from_static("not_foo"),
180                HeaderValue::from_static("not_bar"),
181            )
182            .build()
183            .unwrap();
184        let _: serde_json::Value = endpoint.query(&client).unwrap();
185        mock.assert();
186    }
187}