Skip to main content

openstack_sdk_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 openstack_sdk_core::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<'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::PUT
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
161        params
162    }
163
164    fn service_type(&self) -> ServiceType {
165        ServiceType::ObjectStore
166    }
167
168    fn response_key(&self) -> Option<Cow<'static, str>> {
169        None
170    }
171
172    /// Returns headers to be set into the request
173    fn request_headers(&self) -> Option<&HeaderMap> {
174        self._headers.as_ref()
175    }
176
177    /// Returns required API version
178    fn api_version(&self) -> Option<ApiVersion> {
179        Some(ApiVersion::new(1, 0))
180    }
181}
182
183#[cfg(test)]
184mod tests {
185    use super::*;
186    use http::{HeaderName, HeaderValue};
187    use httpmock::MockServer;
188    #[cfg(feature = "sync")]
189    use openstack_sdk_core::api::Query;
190    use openstack_sdk_core::test::client::FakeOpenStackClient;
191    use openstack_sdk_core::types::ServiceType;
192    use serde_json::json;
193
194    #[test]
195    fn test_service_type() {
196        assert_eq!(
197            Request::builder().build().unwrap().service_type(),
198            ServiceType::ObjectStore
199        );
200    }
201
202    #[test]
203    fn test_response_key() {
204        assert!(Request::builder().build().unwrap().response_key().is_none())
205    }
206
207    #[cfg(feature = "sync")]
208    #[test]
209    fn endpoint() {
210        let server = MockServer::start();
211        let client = FakeOpenStackClient::new(server.base_url());
212        let mock = server.mock(|when, then| {
213            when.method(httpmock::Method::PUT).path(format!(
214                "/{account}/{container}/{object}",
215                account = "account",
216                container = "container",
217                object = "object",
218            ));
219
220            then.status(200)
221                .header("content-type", "application/json")
222                .json_body(json!({ "dummy": {} }));
223        });
224
225        let endpoint = Request::builder()
226            .account("account")
227            .container("container")
228            .object("object")
229            .build()
230            .unwrap();
231        let _: serde_json::Value = endpoint.query(&client).unwrap();
232        mock.assert();
233    }
234
235    #[cfg(feature = "sync")]
236    #[test]
237    fn endpoint_headers() {
238        let server = MockServer::start();
239        let client = FakeOpenStackClient::new(server.base_url());
240        let mock = server.mock(|when, then| {
241            when.method(httpmock::Method::PUT)
242                .path(format!(
243                    "/{account}/{container}/{object}",
244                    account = "account",
245                    container = "container",
246                    object = "object",
247                ))
248                .header("foo", "bar")
249                .header("not_foo", "not_bar");
250            then.status(200)
251                .header("content-type", "application/json")
252                .json_body(json!({ "dummy": {} }));
253        });
254
255        let endpoint = Request::builder()
256            .account("account")
257            .container("container")
258            .object("object")
259            .headers(
260                [(
261                    Some(HeaderName::from_static("foo")),
262                    HeaderValue::from_static("bar"),
263                )]
264                .into_iter(),
265            )
266            .header(
267                HeaderName::from_static("not_foo"),
268                HeaderValue::from_static("not_bar"),
269            )
270            .build()
271            .unwrap();
272        let _: serde_json::Value = endpoint.query(&client).unwrap();
273        mock.assert();
274    }
275}