Skip to main content

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