Skip to main content

openstack_sdk_load_balancer/v2/availability_zone/
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 openstack_sdk_core::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 AvailabilityZone<'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
40#[derive(Builder, Debug, Clone)]
41#[builder(setter(strip_option))]
42pub struct Request<'a> {
43    /// Defines the attributes of a PUT request.
44    #[builder(setter(into))]
45    pub(crate) availability_zone: AvailabilityZone<'a>,
46
47    /// availabilityzone_id parameter for
48    /// /v2/lbaas/availabilityzones/{availabilityzone_id} API
49    #[builder(default, setter(into))]
50    id: Cow<'a, str>,
51
52    #[builder(setter(name = "_headers"), default, private)]
53    _headers: Option<HeaderMap>,
54}
55impl<'a> Request<'a> {
56    /// Create a builder for the endpoint.
57    pub fn builder() -> RequestBuilder<'a> {
58        RequestBuilder::default()
59    }
60}
61
62impl<'a> RequestBuilder<'a> {
63    /// Add a single header to the Availability_Zone.
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::PUT
93    }
94
95    fn endpoint(&self) -> Cow<'static, str> {
96        format!("lbaas/availabilityzones/{id}", id = self.id.as_ref(),).into()
97    }
98
99    fn parameters(&self) -> QueryParams<'_> {
100        QueryParams::default()
101    }
102
103    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
104        let mut params = JsonBodyParams::default();
105
106        params.push(
107            "availability_zone",
108            serde_json::to_value(&self.availability_zone)?,
109        );
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("availability_zone".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    use http::{HeaderName, HeaderValue};
137    use httpmock::MockServer;
138    #[cfg(feature = "sync")]
139    use openstack_sdk_core::api::Query;
140    use openstack_sdk_core::test::client::FakeOpenStackClient;
141    use openstack_sdk_core::types::ServiceType;
142    use serde_json::json;
143
144    #[test]
145    fn test_service_type() {
146        assert_eq!(
147            Request::builder()
148                .availability_zone(AvailabilityZoneBuilder::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                .availability_zone(AvailabilityZoneBuilder::default().build().unwrap())
161                .build()
162                .unwrap()
163                .response_key()
164                .unwrap(),
165            "availability_zone"
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/availabilityzones/{id}", id = "id",));
177
178            then.status(200)
179                .header("content-type", "application/json")
180                .json_body(json!({ "availability_zone": {} }));
181        });
182
183        let endpoint = Request::builder()
184            .id("id")
185            .availability_zone(AvailabilityZoneBuilder::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/availabilityzones/{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!({ "availability_zone": {} }));
205        });
206
207        let endpoint = Request::builder()
208            .id("id")
209            .availability_zone(AvailabilityZoneBuilder::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}