Skip to main content

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