Skip to main content

openstack_sdk_object_store/v1/object/
set.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//! Creates or updates object metadata.
19//!
20use derive_builder::Builder;
21use http::{HeaderMap, HeaderName, HeaderValue};
22
23use openstack_sdk_core::api::rest_endpoint_prelude::*;
24
25use std::borrow::Cow;
26
27#[derive(Builder, Debug, Clone)]
28#[builder(setter(strip_option))]
29pub struct Request<'a> {
30    /// The unique name for the account. An account is also known as the
31    /// project or tenant.
32    #[builder(default, setter(into))]
33    account: Cow<'a, str>,
34
35    /// When the bulk-delete query parameter is present in the POST request,
36    /// multiple objects or containers can be deleted with a single request.
37    /// See Bulk Delete for how this feature is used.
38    #[builder(default)]
39    bulk_delete: Option<bool>,
40
41    /// The unique (within an account) name for the container. The container
42    /// name must be from 1 to 256 characters long and can start with any
43    /// character and contain any pattern. Character set must be UTF-8. The
44    /// container name cannot contain a slash (/) character because this
45    /// character delimits the container and object name. For example, the path
46    /// /v1/account/www/pages specifies the www container, not the www/pages
47    /// container.
48    #[builder(default, setter(into))]
49    container: Cow<'a, str>,
50
51    /// When the extract-archive query parameter is present in the POST
52    /// request, an archive (tar file) is uploaded and extracted to create
53    /// multiple objects. See Extract Archive for how this feature is used.
54    #[builder(default)]
55    extract_archive: Option<bool>,
56
57    /// Overrides the default file name. Object Storage generates a default
58    /// file name for GET temporary URLs that is based on the object name.
59    /// Object Storage returns this value in the Content-Disposition response
60    /// header. Browsers can interpret this file name value as a file
61    /// attachment to save. For more information about temporary URLs, see
62    /// Temporary URL middleware.
63    #[builder(default, setter(into))]
64    filename: Option<Cow<'a, str>>,
65
66    /// If you include the multipart-manifest=get query parameter and the
67    /// object is a large object, the object contents are not returned.
68    /// Instead, the manifest is returned in the X-Object-Manifest response
69    /// header for dynamic large objects or in the response body for static
70    /// large objects.
71    #[builder(default, setter(into))]
72    multipart_manifest: Option<Cow<'a, str>>,
73
74    /// The unique name for the object.
75    #[builder(default, setter(into))]
76    object: Cow<'a, str>,
77
78    /// If you include the symlink=get query parameter and the object is a
79    /// symlink, then the response will include data and metadata from the
80    /// symlink itself rather than from the target.
81    #[builder(default, setter(into))]
82    symlink: Option<Cow<'a, str>>,
83
84    /// The date and time in UNIX Epoch time stamp format or ISO 8601 UTC
85    /// timestamp when the signature for temporary URLs expires. For example,
86    /// 1440619048 or 2015-08-26T19:57:28Z is equivalent to Mon, Wed, 26 Aug
87    /// 2015 19:57:28 GMT. For more information about temporary URLs, see
88    /// Temporary URL middleware.
89    #[builder(default)]
90    temp_url_expires: Option<i32>,
91
92    /// Used with temporary URLs to sign the request with an HMAC-SHA1
93    /// cryptographic signature that defines the allowed HTTP method,
94    /// expiration date, full path to the object, and the secret key for the
95    /// temporary URL. For more information about temporary URLs, see Temporary
96    /// URL middleware.
97    #[builder(default, setter(into))]
98    temp_url_sig: Option<Cow<'a, str>>,
99
100    #[builder(setter(name = "_headers"), default, private)]
101    _headers: Option<HeaderMap>,
102}
103impl<'a> Request<'a> {
104    /// Create a builder for the endpoint.
105    pub fn builder() -> RequestBuilder<'a> {
106        RequestBuilder::default()
107    }
108}
109
110impl<'a> RequestBuilder<'a> {
111    /// Add a single header to the Object.
112    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
113    where
114        K: Into<HeaderName>,
115        V: Into<HeaderValue>,
116    {
117        self._headers
118            .get_or_insert(None)
119            .get_or_insert_with(HeaderMap::new)
120            .insert(header_name.into(), header_value.into());
121        self
122    }
123
124    /// Add multiple headers.
125    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
126    where
127        I: Iterator<Item = T>,
128        T: Into<(Option<HeaderName>, HeaderValue)>,
129    {
130        self._headers
131            .get_or_insert(None)
132            .get_or_insert_with(HeaderMap::new)
133            .extend(iter.map(Into::into));
134        self
135    }
136}
137
138impl RestEndpoint for Request<'_> {
139    fn method(&self) -> http::Method {
140        http::Method::POST
141    }
142
143    fn endpoint(&self) -> Cow<'static, str> {
144        format!(
145            "{account}/{container}/{object}",
146            account = self.account.as_ref(),
147            container = self.container.as_ref(),
148            object = self.object.as_ref(),
149        )
150        .into()
151    }
152
153    fn parameters(&self) -> QueryParams<'_> {
154        let mut params = QueryParams::default();
155        params.push_opt("multipart-manifest", self.multipart_manifest.as_ref());
156        params.push_opt("temp_url_sig", self.temp_url_sig.as_ref());
157        params.push_opt("temp_url_expires", self.temp_url_expires);
158        params.push_opt("filename", self.filename.as_ref());
159        params.push_opt("symlink", self.symlink.as_ref());
160        params.push_opt("bulk-delete", self.bulk_delete);
161        params.push_opt("extract-archive", self.extract_archive);
162
163        params
164    }
165
166    fn service_type(&self) -> ServiceType {
167        ServiceType::ObjectStore
168    }
169
170    fn response_key(&self) -> Option<Cow<'static, str>> {
171        None
172    }
173
174    /// Returns headers to be set into the request
175    fn request_headers(&self) -> Option<&HeaderMap> {
176        self._headers.as_ref()
177    }
178
179    /// Returns required API version
180    fn api_version(&self) -> Option<ApiVersion> {
181        Some(ApiVersion::new(1, 0))
182    }
183}
184
185#[cfg(test)]
186mod tests {
187    use super::*;
188    use http::{HeaderName, HeaderValue};
189    use httpmock::MockServer;
190    #[cfg(feature = "sync")]
191    use openstack_sdk_core::api::Query;
192    use openstack_sdk_core::test::client::FakeOpenStackClient;
193    use openstack_sdk_core::types::ServiceType;
194    use serde_json::json;
195
196    #[test]
197    fn test_service_type() {
198        assert_eq!(
199            Request::builder().build().unwrap().service_type(),
200            ServiceType::ObjectStore
201        );
202    }
203
204    #[test]
205    fn test_response_key() {
206        assert!(Request::builder().build().unwrap().response_key().is_none())
207    }
208
209    #[cfg(feature = "sync")]
210    #[test]
211    fn endpoint() {
212        let server = MockServer::start();
213        let client = FakeOpenStackClient::new(server.base_url());
214        let mock = server.mock(|when, then| {
215            when.method(httpmock::Method::POST).path(format!(
216                "/{account}/{container}/{object}",
217                account = "account",
218                container = "container",
219                object = "object",
220            ));
221
222            then.status(200)
223                .header("content-type", "application/json")
224                .json_body(json!({ "dummy": {} }));
225        });
226
227        let endpoint = Request::builder()
228            .account("account")
229            .container("container")
230            .object("object")
231            .build()
232            .unwrap();
233        let _: serde_json::Value = endpoint.query(&client).unwrap();
234        mock.assert();
235    }
236
237    #[cfg(feature = "sync")]
238    #[test]
239    fn endpoint_headers() {
240        let server = MockServer::start();
241        let client = FakeOpenStackClient::new(server.base_url());
242        let mock = server.mock(|when, then| {
243            when.method(httpmock::Method::POST)
244                .path(format!(
245                    "/{account}/{container}/{object}",
246                    account = "account",
247                    container = "container",
248                    object = "object",
249                ))
250                .header("foo", "bar")
251                .header("not_foo", "not_bar");
252            then.status(200)
253                .header("content-type", "application/json")
254                .json_body(json!({ "dummy": {} }));
255        });
256
257        let endpoint = Request::builder()
258            .account("account")
259            .container("container")
260            .object("object")
261            .headers(
262                [(
263                    Some(HeaderName::from_static("foo")),
264                    HeaderValue::from_static("bar"),
265                )]
266                .into_iter(),
267            )
268            .header(
269                HeaderName::from_static("not_foo"),
270                HeaderValue::from_static("not_bar"),
271            )
272            .build()
273            .unwrap();
274        let _: serde_json::Value = endpoint.query(&client).unwrap();
275        mock.assert();
276    }
277}