Skip to main content

openstack_sdk_object_store/v1/container/
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 an empty container. This operation fails unless the container is
19//! empty. An empty container has no objects.
20//!
21use derive_builder::Builder;
22use http::{HeaderMap, HeaderName, HeaderValue};
23
24use openstack_sdk_core::api::rest_endpoint_prelude::*;
25
26use std::borrow::Cow;
27
28#[derive(Builder, Debug, Clone)]
29#[builder(setter(strip_option))]
30pub struct Request<'a> {
31    /// The unique name for the account. An account is also known as the
32    /// project or tenant.
33    #[builder(default, setter(into))]
34    account: Cow<'a, str>,
35
36    /// The unique (within an account) name for the container. The container
37    /// name must be from 1 to 256 characters long and can start with any
38    /// character and contain any pattern. Character set must be UTF-8. The
39    /// container name cannot contain a slash (/) character because this
40    /// character delimits the container and object name. For example, the path
41    /// /v1/account/www/pages specifies the www container, not the www/pages
42    /// container.
43    #[builder(default, setter(into))]
44    container: Cow<'a, str>,
45
46    #[builder(setter(name = "_headers"), default, private)]
47    _headers: Option<HeaderMap>,
48}
49impl<'a> Request<'a> {
50    /// Create a builder for the endpoint.
51    pub fn builder() -> RequestBuilder<'a> {
52        RequestBuilder::default()
53    }
54}
55
56impl<'a> RequestBuilder<'a> {
57    /// Add a single header to the Container.
58    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
59    where
60        K: Into<HeaderName>,
61        V: Into<HeaderValue>,
62    {
63        self._headers
64            .get_or_insert(None)
65            .get_or_insert_with(HeaderMap::new)
66            .insert(header_name.into(), header_value.into());
67        self
68    }
69
70    /// Add multiple headers.
71    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
72    where
73        I: Iterator<Item = T>,
74        T: Into<(Option<HeaderName>, HeaderValue)>,
75    {
76        self._headers
77            .get_or_insert(None)
78            .get_or_insert_with(HeaderMap::new)
79            .extend(iter.map(Into::into));
80        self
81    }
82}
83
84impl RestEndpoint for Request<'_> {
85    fn method(&self) -> http::Method {
86        http::Method::DELETE
87    }
88
89    fn endpoint(&self) -> Cow<'static, str> {
90        format!(
91            "{account}/{container}",
92            account = self.account.as_ref(),
93            container = self.container.as_ref(),
94        )
95        .into()
96    }
97
98    fn parameters(&self) -> QueryParams<'_> {
99        QueryParams::default()
100    }
101
102    fn service_type(&self) -> ServiceType {
103        ServiceType::ObjectStore
104    }
105
106    fn response_key(&self) -> Option<Cow<'static, str>> {
107        None
108    }
109
110    /// Returns headers to be set into the request
111    fn request_headers(&self) -> Option<&HeaderMap> {
112        self._headers.as_ref()
113    }
114
115    /// Returns required API version
116    fn api_version(&self) -> Option<ApiVersion> {
117        Some(ApiVersion::new(1, 0))
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124    use http::{HeaderName, HeaderValue};
125    use httpmock::MockServer;
126    #[cfg(feature = "sync")]
127    use openstack_sdk_core::api::Query;
128    use openstack_sdk_core::test::client::FakeOpenStackClient;
129    use openstack_sdk_core::types::ServiceType;
130    use serde_json::json;
131
132    #[test]
133    fn test_service_type() {
134        assert_eq!(
135            Request::builder().build().unwrap().service_type(),
136            ServiceType::ObjectStore
137        );
138    }
139
140    #[test]
141    fn test_response_key() {
142        assert!(Request::builder().build().unwrap().response_key().is_none())
143    }
144
145    #[cfg(feature = "sync")]
146    #[test]
147    fn endpoint() {
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).path(format!(
152                "/{account}/{container}",
153                account = "account",
154                container = "container",
155            ));
156
157            then.status(200)
158                .header("content-type", "application/json")
159                .json_body(json!({ "dummy": {} }));
160        });
161
162        let endpoint = Request::builder()
163            .account("account")
164            .container("container")
165            .build()
166            .unwrap();
167        let _: serde_json::Value = endpoint.query(&client).unwrap();
168        mock.assert();
169    }
170
171    #[cfg(feature = "sync")]
172    #[test]
173    fn endpoint_headers() {
174        let server = MockServer::start();
175        let client = FakeOpenStackClient::new(server.base_url());
176        let mock = server.mock(|when, then| {
177            when.method(httpmock::Method::DELETE)
178                .path(format!(
179                    "/{account}/{container}",
180                    account = "account",
181                    container = "container",
182                ))
183                .header("foo", "bar")
184                .header("not_foo", "not_bar");
185            then.status(200)
186                .header("content-type", "application/json")
187                .json_body(json!({ "dummy": {} }));
188        });
189
190        let endpoint = Request::builder()
191            .account("account")
192            .container("container")
193            .headers(
194                [(
195                    Some(HeaderName::from_static("foo")),
196                    HeaderValue::from_static("bar"),
197                )]
198                .into_iter(),
199            )
200            .header(
201                HeaderName::from_static("not_foo"),
202                HeaderValue::from_static("not_bar"),
203            )
204            .build()
205            .unwrap();
206        let _: serde_json::Value = endpoint.query(&client).unwrap();
207        mock.assert();
208    }
209}