Skip to main content

openstack_sdk_load_balancer/v2/flavor_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 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 the attributes of a PUT request.
30#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
31#[builder(setter(strip_option))]
32pub struct Flavorprofile<'a> {
33    #[serde(skip_serializing_if = "Option::is_none")]
34    #[builder(default, setter(into))]
35    pub(crate) flavor_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) flavorprofile: Flavorprofile<'a>,
52
53    /// flavorprofile_id parameter for
54    /// /v2/lbaas/flavorprofiles/{flavorprofile_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 Flavor_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/flavorprofiles/{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("flavorprofile", serde_json::to_value(&self.flavorprofile)?);
113
114        params.into_body()
115    }
116
117    fn service_type(&self) -> ServiceType {
118        ServiceType::LoadBalancer
119    }
120
121    fn response_key(&self) -> Option<Cow<'static, str>> {
122        Some("flavorprofile".into())
123    }
124
125    /// Returns headers to be set into the request
126    fn request_headers(&self) -> Option<&HeaderMap> {
127        self._headers.as_ref()
128    }
129
130    /// Returns required API version
131    fn api_version(&self) -> Option<ApiVersion> {
132        Some(ApiVersion::new(2, 0))
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139    use http::{HeaderName, HeaderValue};
140    use httpmock::MockServer;
141    #[cfg(feature = "sync")]
142    use openstack_sdk_core::api::Query;
143    use openstack_sdk_core::test::client::FakeOpenStackClient;
144    use openstack_sdk_core::types::ServiceType;
145    use serde_json::json;
146
147    #[test]
148    fn test_service_type() {
149        assert_eq!(
150            Request::builder()
151                .flavorprofile(FlavorprofileBuilder::default().build().unwrap())
152                .build()
153                .unwrap()
154                .service_type(),
155            ServiceType::LoadBalancer
156        );
157    }
158
159    #[test]
160    fn test_response_key() {
161        assert_eq!(
162            Request::builder()
163                .flavorprofile(FlavorprofileBuilder::default().build().unwrap())
164                .build()
165                .unwrap()
166                .response_key()
167                .unwrap(),
168            "flavorprofile"
169        );
170    }
171
172    #[cfg(feature = "sync")]
173    #[test]
174    fn endpoint() {
175        let server = MockServer::start();
176        let client = FakeOpenStackClient::new(server.base_url());
177        let mock = server.mock(|when, then| {
178            when.method(httpmock::Method::PUT)
179                .path(format!("/lbaas/flavorprofiles/{id}", id = "id",));
180
181            then.status(200)
182                .header("content-type", "application/json")
183                .json_body(json!({ "flavorprofile": {} }));
184        });
185
186        let endpoint = Request::builder()
187            .id("id")
188            .flavorprofile(FlavorprofileBuilder::default().build().unwrap())
189            .build()
190            .unwrap();
191        let _: serde_json::Value = endpoint.query(&client).unwrap();
192        mock.assert();
193    }
194
195    #[cfg(feature = "sync")]
196    #[test]
197    fn endpoint_headers() {
198        let server = MockServer::start();
199        let client = FakeOpenStackClient::new(server.base_url());
200        let mock = server.mock(|when, then| {
201            when.method(httpmock::Method::PUT)
202                .path(format!("/lbaas/flavorprofiles/{id}", id = "id",))
203                .header("foo", "bar")
204                .header("not_foo", "not_bar");
205            then.status(200)
206                .header("content-type", "application/json")
207                .json_body(json!({ "flavorprofile": {} }));
208        });
209
210        let endpoint = Request::builder()
211            .id("id")
212            .flavorprofile(FlavorprofileBuilder::default().build().unwrap())
213            .headers(
214                [(
215                    Some(HeaderName::from_static("foo")),
216                    HeaderValue::from_static("bar"),
217                )]
218                .into_iter(),
219            )
220            .header(
221                HeaderName::from_static("not_foo"),
222                HeaderValue::from_static("not_bar"),
223            )
224            .build()
225            .unwrap();
226        let _: serde_json::Value = endpoint.query(&client).unwrap();
227        mock.assert();
228    }
229}