Skip to main content

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