Skip to main content

openstack_sdk_load_balancer/v2/availability_zone_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 an Availability Zone 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 AvailabilityZoneProfile<'a> {
33    #[serde()]
34    #[builder(setter(into))]
35    pub(crate) availability_zone_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) availability_zone_profile: AvailabilityZoneProfile<'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 Availability_Zone_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/availabilityzoneprofiles".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(
108            "availability_zone_profile",
109            serde_json::to_value(&self.availability_zone_profile)?,
110        );
111
112        params.into_body()
113    }
114
115    fn service_type(&self) -> ServiceType {
116        ServiceType::LoadBalancer
117    }
118
119    fn response_key(&self) -> Option<Cow<'static, str>> {
120        Some("availability_zone_profile".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(2, 0))
131    }
132}
133
134#[cfg(test)]
135mod tests {
136    use super::*;
137    use http::{HeaderName, HeaderValue};
138    use httpmock::MockServer;
139    #[cfg(feature = "sync")]
140    use openstack_sdk_core::api::Query;
141    use openstack_sdk_core::test::client::FakeOpenStackClient;
142    use openstack_sdk_core::types::ServiceType;
143    use serde_json::json;
144
145    #[test]
146    fn test_service_type() {
147        assert_eq!(
148            Request::builder()
149                .availability_zone_profile(
150                    AvailabilityZoneProfileBuilder::default()
151                        .availability_zone_data("foo")
152                        .name("foo")
153                        .provider_name("foo")
154                        .build()
155                        .unwrap()
156                )
157                .build()
158                .unwrap()
159                .service_type(),
160            ServiceType::LoadBalancer
161        );
162    }
163
164    #[test]
165    fn test_response_key() {
166        assert_eq!(
167            Request::builder()
168                .availability_zone_profile(
169                    AvailabilityZoneProfileBuilder::default()
170                        .availability_zone_data("foo")
171                        .name("foo")
172                        .provider_name("foo")
173                        .build()
174                        .unwrap()
175                )
176                .build()
177                .unwrap()
178                .response_key()
179                .unwrap(),
180            "availability_zone_profile"
181        );
182    }
183
184    #[cfg(feature = "sync")]
185    #[test]
186    fn endpoint() {
187        let server = MockServer::start();
188        let client = FakeOpenStackClient::new(server.base_url());
189        let mock = server.mock(|when, then| {
190            when.method(httpmock::Method::POST)
191                .path("/lbaas/availabilityzoneprofiles".to_string());
192
193            then.status(200)
194                .header("content-type", "application/json")
195                .json_body(json!({ "availability_zone_profile": {} }));
196        });
197
198        let endpoint = Request::builder()
199            .availability_zone_profile(
200                AvailabilityZoneProfileBuilder::default()
201                    .availability_zone_data("foo")
202                    .name("foo")
203                    .provider_name("foo")
204                    .build()
205                    .unwrap(),
206            )
207            .build()
208            .unwrap();
209        let _: serde_json::Value = endpoint.query(&client).unwrap();
210        mock.assert();
211    }
212
213    #[cfg(feature = "sync")]
214    #[test]
215    fn endpoint_headers() {
216        let server = MockServer::start();
217        let client = FakeOpenStackClient::new(server.base_url());
218        let mock = server.mock(|when, then| {
219            when.method(httpmock::Method::POST)
220                .path("/lbaas/availabilityzoneprofiles".to_string())
221                .header("foo", "bar")
222                .header("not_foo", "not_bar");
223            then.status(200)
224                .header("content-type", "application/json")
225                .json_body(json!({ "availability_zone_profile": {} }));
226        });
227
228        let endpoint = Request::builder()
229            .availability_zone_profile(
230                AvailabilityZoneProfileBuilder::default()
231                    .availability_zone_data("foo")
232                    .name("foo")
233                    .provider_name("foo")
234                    .build()
235                    .unwrap(),
236            )
237            .headers(
238                [(
239                    Some(HeaderName::from_static("foo")),
240                    HeaderValue::from_static("bar"),
241                )]
242                .into_iter(),
243            )
244            .header(
245                HeaderName::from_static("not_foo"),
246                HeaderValue::from_static("not_bar"),
247            )
248            .build()
249            .unwrap();
250        let _: serde_json::Value = endpoint.query(&client).unwrap();
251        mock.assert();
252    }
253}