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