openstack_sdk/api/object_store/v1/object/
put.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 an object with data content and metadata, or replaces an existing
19//! object with data content and metadata. The PUT operation always creates an
20//! object. If you use this operation on an existing object, you replace the
21//! existing object and metadata rather than modifying the object.
22//! Consequently, this operation returns the Created (201) response code. If
23//! you use this operation to copy a manifest object, the new object is a
24//! normal object and not a copy of the manifest. Instead it is a concatenation
25//! of all the segment objects. This means that you cannot copy objects larger
26//! than 5 GB. Note that the provider may have limited the characters which are
27//! allowed in an object name. Any name limits are exposed under the name_check
28//! key in the /info discoverability response. Regardless of name_check
29//! limitations, names must be URL quoted UTF-8. To create custom metadata, use
30//! the X-Object-Meta-name header, where name is the name of the metadata item.
31//!
32use derive_builder::Builder;
33use http::{HeaderMap, HeaderName, HeaderValue};
34
35use crate::api::rest_endpoint_prelude::*;
36
37use std::borrow::Cow;
38
39#[derive(Builder, Debug, Clone)]
40#[builder(setter(strip_option))]
41pub struct Request<'a> {
42    /// The unique name for the account. An account is also known as the
43    /// project or tenant.
44    #[builder(default, setter(into))]
45    account: Cow<'a, str>,
46
47    /// The unique (within an account) name for the container. The container
48    /// name must be from 1 to 256 characters long and can start with any
49    /// character and contain any pattern. Character set must be UTF-8. The
50    /// container name cannot contain a slash (/) character because this
51    /// character delimits the container and object name. For example, the path
52    /// /v1/account/www/pages specifies the www container, not the www/pages
53    /// container.
54    #[builder(default, setter(into))]
55    container: Cow<'a, str>,
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 RequestBuilder<'_> {
111    /// Add a single header to the Object.
112    pub fn header(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
113where {
114        self._headers
115            .get_or_insert(None)
116            .get_or_insert_with(HeaderMap::new)
117            .insert(header_name, HeaderValue::from_static(header_value));
118        self
119    }
120
121    /// Add multiple headers.
122    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
123    where
124        I: Iterator<Item = T>,
125        T: Into<(Option<HeaderName>, HeaderValue)>,
126    {
127        self._headers
128            .get_or_insert(None)
129            .get_or_insert_with(HeaderMap::new)
130            .extend(iter.map(Into::into));
131        self
132    }
133}
134
135impl RestEndpoint for Request<'_> {
136    fn method(&self) -> http::Method {
137        http::Method::PUT
138    }
139
140    fn endpoint(&self) -> Cow<'static, str> {
141        format!(
142            "{account}/{container}/{object}",
143            account = self.account.as_ref(),
144            container = self.container.as_ref(),
145            object = self.object.as_ref(),
146        )
147        .into()
148    }
149
150    fn parameters(&self) -> QueryParams {
151        let mut params = QueryParams::default();
152        params.push_opt("multipart-manifest", self.multipart_manifest.as_ref());
153        params.push_opt("temp_url_sig", self.temp_url_sig.as_ref());
154        params.push_opt("temp_url_expires", self.temp_url_expires);
155        params.push_opt("filename", self.filename.as_ref());
156        params.push_opt("symlink", self.symlink.as_ref());
157
158        params
159    }
160
161    fn service_type(&self) -> ServiceType {
162        ServiceType::ObjectStore
163    }
164
165    fn response_key(&self) -> Option<Cow<'static, str>> {
166        None
167    }
168
169    /// Returns headers to be set into the request
170    fn request_headers(&self) -> Option<&HeaderMap> {
171        self._headers.as_ref()
172    }
173
174    /// Returns required API version
175    fn api_version(&self) -> Option<ApiVersion> {
176        Some(ApiVersion::new(1, 0))
177    }
178}
179
180#[cfg(test)]
181mod tests {
182    use super::*;
183    #[cfg(feature = "sync")]
184    use crate::api::Query;
185    use crate::test::client::FakeOpenStackClient;
186    use crate::types::ServiceType;
187    use http::{HeaderName, HeaderValue};
188    use httpmock::MockServer;
189    use serde_json::json;
190
191    #[test]
192    fn test_service_type() {
193        assert_eq!(
194            Request::builder().build().unwrap().service_type(),
195            ServiceType::ObjectStore
196        );
197    }
198
199    #[test]
200    fn test_response_key() {
201        assert!(Request::builder().build().unwrap().response_key().is_none())
202    }
203
204    #[cfg(feature = "sync")]
205    #[test]
206    fn endpoint() {
207        let server = MockServer::start();
208        let client = FakeOpenStackClient::new(server.base_url());
209        let mock = server.mock(|when, then| {
210            when.method(httpmock::Method::PUT).path(format!(
211                "/{account}/{container}/{object}",
212                account = "account",
213                container = "container",
214                object = "object",
215            ));
216
217            then.status(200)
218                .header("content-type", "application/json")
219                .json_body(json!({ "dummy": {} }));
220        });
221
222        let endpoint = Request::builder()
223            .account("account")
224            .container("container")
225            .object("object")
226            .build()
227            .unwrap();
228        let _: serde_json::Value = endpoint.query(&client).unwrap();
229        mock.assert();
230    }
231
232    #[cfg(feature = "sync")]
233    #[test]
234    fn endpoint_headers() {
235        let server = MockServer::start();
236        let client = FakeOpenStackClient::new(server.base_url());
237        let mock = server.mock(|when, then| {
238            when.method(httpmock::Method::PUT)
239                .path(format!(
240                    "/{account}/{container}/{object}",
241                    account = "account",
242                    container = "container",
243                    object = "object",
244                ))
245                .header("foo", "bar")
246                .header("not_foo", "not_bar");
247            then.status(200)
248                .header("content-type", "application/json")
249                .json_body(json!({ "dummy": {} }));
250        });
251
252        let endpoint = Request::builder()
253            .account("account")
254            .container("container")
255            .object("object")
256            .headers(
257                [(
258                    Some(HeaderName::from_static("foo")),
259                    HeaderValue::from_static("bar"),
260                )]
261                .into_iter(),
262            )
263            .header("not_foo", "not_bar")
264            .build()
265            .unwrap();
266        let _: serde_json::Value = endpoint.query(&client).unwrap();
267        mock.assert();
268    }
269}