Skip to main content

openstack_sdk_object_store/v1/account/
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 account metadata. To create, update, or delete
19//! custom metadata, use the X-Account-Meta-{name} request header, where {name}
20//! is the name of the metadata item. Account metadata operations work
21//! differently than how object metadata operations work. Depending on the
22//! contents of your POST account metadata request, the Object Storage API
23//! updates the metadata as shown in the following table: TODO: fill the rest
24//! To delete a metadata header, send an empty value for that header, such as
25//! for the X-Account-Meta-Book header. If the tool you use to communicate with
26//! Object Storage, such as an older version of cURL, does not support empty
27//! headers, send the X-Remove-Account- Meta-{name} header with an arbitrary
28//! value. For example, X-Remove-Account-Meta-Book: x. The operation ignores
29//! the arbitrary value.
30//!
31use derive_builder::Builder;
32use http::{HeaderMap, HeaderName, HeaderValue};
33
34use openstack_sdk_core::api::rest_endpoint_prelude::*;
35
36use std::borrow::Cow;
37
38#[derive(Builder, Debug, Clone)]
39#[builder(setter(strip_option))]
40pub struct Request<'a> {
41    /// The unique name for the account. An account is also known as the
42    /// project or tenant.
43    #[builder(default, setter(into))]
44    account: Cow<'a, str>,
45
46    #[builder(setter(name = "_headers"), default, private)]
47    _headers: Option<HeaderMap>,
48}
49impl<'a> Request<'a> {
50    /// Create a builder for the endpoint.
51    pub fn builder() -> RequestBuilder<'a> {
52        RequestBuilder::default()
53    }
54}
55
56impl<'a> RequestBuilder<'a> {
57    /// Add a single header to the Account.
58    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
59    where
60        K: Into<HeaderName>,
61        V: Into<HeaderValue>,
62    {
63        self._headers
64            .get_or_insert(None)
65            .get_or_insert_with(HeaderMap::new)
66            .insert(header_name.into(), header_value.into());
67        self
68    }
69
70    /// Add multiple headers.
71    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
72    where
73        I: Iterator<Item = T>,
74        T: Into<(Option<HeaderName>, HeaderValue)>,
75    {
76        self._headers
77            .get_or_insert(None)
78            .get_or_insert_with(HeaderMap::new)
79            .extend(iter.map(Into::into));
80        self
81    }
82}
83
84impl RestEndpoint for Request<'_> {
85    fn method(&self) -> http::Method {
86        http::Method::POST
87    }
88
89    fn endpoint(&self) -> Cow<'static, str> {
90        self.account.as_ref().to_string().into()
91    }
92
93    fn parameters(&self) -> QueryParams<'_> {
94        QueryParams::default()
95    }
96
97    fn service_type(&self) -> ServiceType {
98        ServiceType::ObjectStore
99    }
100
101    fn response_key(&self) -> Option<Cow<'static, str>> {
102        None
103    }
104
105    /// Returns headers to be set into the request
106    fn request_headers(&self) -> Option<&HeaderMap> {
107        self._headers.as_ref()
108    }
109
110    /// Returns required API version
111    fn api_version(&self) -> Option<ApiVersion> {
112        Some(ApiVersion::new(1, 0))
113    }
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119    use http::{HeaderName, HeaderValue};
120    use httpmock::MockServer;
121    #[cfg(feature = "sync")]
122    use openstack_sdk_core::api::Query;
123    use openstack_sdk_core::test::client::FakeOpenStackClient;
124    use openstack_sdk_core::types::ServiceType;
125    use serde_json::json;
126
127    #[test]
128    fn test_service_type() {
129        assert_eq!(
130            Request::builder().build().unwrap().service_type(),
131            ServiceType::ObjectStore
132        );
133    }
134
135    #[test]
136    fn test_response_key() {
137        assert!(Request::builder().build().unwrap().response_key().is_none())
138    }
139
140    #[cfg(feature = "sync")]
141    #[test]
142    fn endpoint() {
143        let server = MockServer::start();
144        let client = FakeOpenStackClient::new(server.base_url());
145        let mock = server.mock(|when, then| {
146            when.method(httpmock::Method::POST)
147                .path(format!("/{account}", account = "account",));
148
149            then.status(200)
150                .header("content-type", "application/json")
151                .json_body(json!({ "dummy": {} }));
152        });
153
154        let endpoint = Request::builder().account("account").build().unwrap();
155        let _: serde_json::Value = endpoint.query(&client).unwrap();
156        mock.assert();
157    }
158
159    #[cfg(feature = "sync")]
160    #[test]
161    fn endpoint_headers() {
162        let server = MockServer::start();
163        let client = FakeOpenStackClient::new(server.base_url());
164        let mock = server.mock(|when, then| {
165            when.method(httpmock::Method::POST)
166                .path(format!("/{account}", account = "account",))
167                .header("foo", "bar")
168                .header("not_foo", "not_bar");
169            then.status(200)
170                .header("content-type", "application/json")
171                .json_body(json!({ "dummy": {} }));
172        });
173
174        let endpoint = Request::builder()
175            .account("account")
176            .headers(
177                [(
178                    Some(HeaderName::from_static("foo")),
179                    HeaderValue::from_static("bar"),
180                )]
181                .into_iter(),
182            )
183            .header(
184                HeaderName::from_static("not_foo"),
185                HeaderValue::from_static("not_bar"),
186            )
187            .build()
188            .unwrap();
189        let _: serde_json::Value = endpoint.query(&client).unwrap();
190        mock.assert();
191    }
192}