Skip to main content

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