openstack_sdk/api/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 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 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(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
65where {
66        self._headers
67            .get_or_insert(None)
68            .get_or_insert_with(HeaderMap::new)
69            .insert(header_name, HeaderValue::from_static(header_value));
70        self
71    }
72
73    /// Add multiple headers.
74    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
75    where
76        I: Iterator<Item = T>,
77        T: Into<(Option<HeaderName>, HeaderValue)>,
78    {
79        self._headers
80            .get_or_insert(None)
81            .get_or_insert_with(HeaderMap::new)
82            .extend(iter.map(Into::into));
83        self
84    }
85}
86
87impl RestEndpoint for Request<'_> {
88    fn method(&self) -> http::Method {
89        http::Method::PUT
90    }
91
92    fn endpoint(&self) -> Cow<'static, str> {
93        format!("lbaas/availabilityzones/{id}", id = self.id.as_ref(),).into()
94    }
95
96    fn parameters(&self) -> QueryParams {
97        QueryParams::default()
98    }
99
100    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
101        let mut params = JsonBodyParams::default();
102
103        params.push(
104            "availability_zone",
105            serde_json::to_value(&self.availability_zone)?,
106        );
107
108        params.into_body()
109    }
110
111    fn service_type(&self) -> ServiceType {
112        ServiceType::LoadBalancer
113    }
114
115    fn response_key(&self) -> Option<Cow<'static, str>> {
116        Some("availability_zone".into())
117    }
118
119    /// Returns headers to be set into the request
120    fn request_headers(&self) -> Option<&HeaderMap> {
121        self._headers.as_ref()
122    }
123
124    /// Returns required API version
125    fn api_version(&self) -> Option<ApiVersion> {
126        Some(ApiVersion::new(2, 0))
127    }
128}
129
130#[cfg(test)]
131mod tests {
132    use super::*;
133    #[cfg(feature = "sync")]
134    use crate::api::Query;
135    use crate::test::client::FakeOpenStackClient;
136    use crate::types::ServiceType;
137    use http::{HeaderName, HeaderValue};
138    use httpmock::MockServer;
139    use serde_json::json;
140
141    #[test]
142    fn test_service_type() {
143        assert_eq!(
144            Request::builder()
145                .availability_zone(AvailabilityZoneBuilder::default().build().unwrap())
146                .build()
147                .unwrap()
148                .service_type(),
149            ServiceType::LoadBalancer
150        );
151    }
152
153    #[test]
154    fn test_response_key() {
155        assert_eq!(
156            Request::builder()
157                .availability_zone(AvailabilityZoneBuilder::default().build().unwrap())
158                .build()
159                .unwrap()
160                .response_key()
161                .unwrap(),
162            "availability_zone"
163        );
164    }
165
166    #[cfg(feature = "sync")]
167    #[test]
168    fn endpoint() {
169        let server = MockServer::start();
170        let client = FakeOpenStackClient::new(server.base_url());
171        let mock = server.mock(|when, then| {
172            when.method(httpmock::Method::PUT)
173                .path(format!("/lbaas/availabilityzones/{id}", id = "id",));
174
175            then.status(200)
176                .header("content-type", "application/json")
177                .json_body(json!({ "availability_zone": {} }));
178        });
179
180        let endpoint = Request::builder()
181            .id("id")
182            .availability_zone(AvailabilityZoneBuilder::default().build().unwrap())
183            .build()
184            .unwrap();
185        let _: serde_json::Value = endpoint.query(&client).unwrap();
186        mock.assert();
187    }
188
189    #[cfg(feature = "sync")]
190    #[test]
191    fn endpoint_headers() {
192        let server = MockServer::start();
193        let client = FakeOpenStackClient::new(server.base_url());
194        let mock = server.mock(|when, then| {
195            when.method(httpmock::Method::PUT)
196                .path(format!("/lbaas/availabilityzones/{id}", id = "id",))
197                .header("foo", "bar")
198                .header("not_foo", "not_bar");
199            then.status(200)
200                .header("content-type", "application/json")
201                .json_body(json!({ "availability_zone": {} }));
202        });
203
204        let endpoint = Request::builder()
205            .id("id")
206            .availability_zone(AvailabilityZoneBuilder::default().build().unwrap())
207            .headers(
208                [(
209                    Some(HeaderName::from_static("foo")),
210                    HeaderValue::from_static("bar"),
211                )]
212                .into_iter(),
213            )
214            .header("not_foo", "not_bar")
215            .build()
216            .unwrap();
217        let _: serde_json::Value = endpoint.query(&client).unwrap();
218        mock.assert();
219    }
220}