openstack_sdk/api/block_storage/v3/volume/metadata/
create.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
18use derive_builder::Builder;
19use http::{HeaderMap, HeaderName, HeaderValue};
20
21use crate::api::rest_endpoint_prelude::*;
22
23use std::borrow::Cow;
24use std::collections::BTreeMap;
25
26#[derive(Builder, Debug, Clone)]
27#[builder(setter(strip_option))]
28pub struct Request<'a> {
29    /// One or more metadata key and value pairs that are associated with the
30    /// volume.
31    #[builder(private, setter(into, name = "_metadata"))]
32    pub(crate) metadata: BTreeMap<Cow<'a, str>, Cow<'a, str>>,
33
34    /// volume_id parameter for /v3/volumes/{volume_id}/metadata API
35    #[builder(default, setter(into))]
36    volume_id: Cow<'a, str>,
37
38    #[builder(setter(name = "_headers"), default, private)]
39    _headers: Option<HeaderMap>,
40}
41impl<'a> Request<'a> {
42    /// Create a builder for the endpoint.
43    pub fn builder() -> RequestBuilder<'a> {
44        RequestBuilder::default()
45    }
46}
47
48impl<'a> RequestBuilder<'a> {
49    /// One or more metadata key and value pairs that are associated with the
50    /// volume.
51    pub fn metadata<I, K, V>(&mut self, iter: I) -> &mut Self
52    where
53        I: Iterator<Item = (K, V)>,
54        K: Into<Cow<'a, str>>,
55        V: Into<Cow<'a, str>>,
56    {
57        self.metadata
58            .get_or_insert_with(BTreeMap::new)
59            .extend(iter.map(|(k, v)| (k.into(), v.into())));
60        self
61    }
62
63    /// Add a single header to the Metadata.
64    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
65    where
66        K: Into<HeaderName>,
67        V: Into<HeaderValue>,
68    {
69        self._headers
70            .get_or_insert(None)
71            .get_or_insert_with(HeaderMap::new)
72            .insert(header_name.into(), header_value.into());
73        self
74    }
75
76    /// Add multiple headers.
77    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
78    where
79        I: Iterator<Item = T>,
80        T: Into<(Option<HeaderName>, HeaderValue)>,
81    {
82        self._headers
83            .get_or_insert(None)
84            .get_or_insert_with(HeaderMap::new)
85            .extend(iter.map(Into::into));
86        self
87    }
88}
89
90impl RestEndpoint for Request<'_> {
91    fn method(&self) -> http::Method {
92        http::Method::POST
93    }
94
95    fn endpoint(&self) -> Cow<'static, str> {
96        format!(
97            "volumes/{volume_id}/metadata",
98            volume_id = self.volume_id.as_ref(),
99        )
100        .into()
101    }
102
103    fn parameters(&self) -> QueryParams<'_> {
104        QueryParams::default()
105    }
106
107    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
108        let mut params = JsonBodyParams::default();
109
110        params.push("metadata", serde_json::to_value(&self.metadata)?);
111
112        params.into_body()
113    }
114
115    fn service_type(&self) -> ServiceType {
116        ServiceType::BlockStorage
117    }
118
119    fn response_key(&self) -> Option<Cow<'static, str>> {
120        Some("metadata".into())
121    }
122
123    /// Returns headers to be set into the request
124    fn request_headers(&self) -> Option<&HeaderMap> {
125        self._headers.as_ref()
126    }
127
128    /// Returns required API version
129    fn api_version(&self) -> Option<ApiVersion> {
130        Some(ApiVersion::new(3, 0))
131    }
132}
133
134#[cfg(test)]
135mod tests {
136    use super::*;
137    #[cfg(feature = "sync")]
138    use crate::api::Query;
139    use crate::test::client::FakeOpenStackClient;
140    use crate::types::ServiceType;
141    use http::{HeaderName, HeaderValue};
142    use httpmock::MockServer;
143    use serde_json::json;
144
145    #[test]
146    fn test_service_type() {
147        assert_eq!(
148            Request::builder()
149                .metadata(BTreeMap::<String, String>::new().into_iter())
150                .build()
151                .unwrap()
152                .service_type(),
153            ServiceType::BlockStorage
154        );
155    }
156
157    #[test]
158    fn test_response_key() {
159        assert_eq!(
160            Request::builder()
161                .metadata(BTreeMap::<String, String>::new().into_iter())
162                .build()
163                .unwrap()
164                .response_key()
165                .unwrap(),
166            "metadata"
167        );
168    }
169
170    #[cfg(feature = "sync")]
171    #[test]
172    fn endpoint() {
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).path(format!(
177                "/volumes/{volume_id}/metadata",
178                volume_id = "volume_id",
179            ));
180
181            then.status(200)
182                .header("content-type", "application/json")
183                .json_body(json!({ "metadata": {} }));
184        });
185
186        let endpoint = Request::builder()
187            .volume_id("volume_id")
188            .metadata(BTreeMap::<String, String>::new().into_iter())
189            .build()
190            .unwrap();
191        let _: serde_json::Value = endpoint.query(&client).unwrap();
192        mock.assert();
193    }
194
195    #[cfg(feature = "sync")]
196    #[test]
197    fn endpoint_headers() {
198        let server = MockServer::start();
199        let client = FakeOpenStackClient::new(server.base_url());
200        let mock = server.mock(|when, then| {
201            when.method(httpmock::Method::POST)
202                .path(format!(
203                    "/volumes/{volume_id}/metadata",
204                    volume_id = "volume_id",
205                ))
206                .header("foo", "bar")
207                .header("not_foo", "not_bar");
208            then.status(200)
209                .header("content-type", "application/json")
210                .json_body(json!({ "metadata": {} }));
211        });
212
213        let endpoint = Request::builder()
214            .volume_id("volume_id")
215            .metadata(BTreeMap::<String, String>::new().into_iter())
216            .headers(
217                [(
218                    Some(HeaderName::from_static("foo")),
219                    HeaderValue::from_static("bar"),
220                )]
221                .into_iter(),
222            )
223            .header(
224                HeaderName::from_static("not_foo"),
225                HeaderValue::from_static("not_bar"),
226            )
227            .build()
228            .unwrap();
229        let _: serde_json::Value = endpoint.query(&client).unwrap();
230        mock.assert();
231    }
232}