Skip to main content

openstack_sdk_load_balancer/v2/flavor/
list.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//! Lists all flavors.
19//!
20use derive_builder::Builder;
21use http::{HeaderMap, HeaderName, HeaderValue};
22
23use openstack_sdk_core::api::rest_endpoint_prelude::*;
24
25use std::borrow::Cow;
26
27use openstack_sdk_core::api::Pageable;
28#[derive(Builder, Debug, Clone)]
29#[builder(setter(strip_option))]
30pub struct Request<'a> {
31    #[builder(default, setter(into))]
32    description: Option<Cow<'a, str>>,
33
34    #[builder(default)]
35    enabled: Option<bool>,
36
37    #[builder(default, setter(into))]
38    flavor_profile_id: Option<Cow<'a, str>>,
39
40    #[builder(default, setter(into))]
41    id: Option<Cow<'a, str>>,
42
43    /// Page size
44    #[builder(default)]
45    limit: Option<i32>,
46
47    /// ID of the last item in the previous list
48    #[builder(default, setter(into))]
49    marker: Option<Cow<'a, str>>,
50
51    #[builder(default, setter(into))]
52    name: Option<Cow<'a, str>>,
53
54    /// The page direction.
55    #[builder(default)]
56    page_reverse: Option<bool>,
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.
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::GET
99    }
100
101    fn endpoint(&self) -> Cow<'static, str> {
102        "lbaas/flavors".to_string().into()
103    }
104
105    fn parameters(&self) -> QueryParams<'_> {
106        let mut params = QueryParams::default();
107        params.push_opt("description", self.description.as_ref());
108        params.push_opt("enabled", self.enabled);
109        params.push_opt("flavor_profile_id", self.flavor_profile_id.as_ref());
110        params.push_opt("id", self.id.as_ref());
111        params.push_opt("limit", self.limit);
112        params.push_opt("marker", self.marker.as_ref());
113        params.push_opt("name", self.name.as_ref());
114        params.push_opt("page_reverse", self.page_reverse);
115
116        params
117    }
118
119    fn service_type(&self) -> ServiceType {
120        ServiceType::LoadBalancer
121    }
122
123    fn response_key(&self) -> Option<Cow<'static, str>> {
124        Some("flavors".into())
125    }
126
127    /// Returns headers to be set into the request
128    fn request_headers(&self) -> Option<&HeaderMap> {
129        self._headers.as_ref()
130    }
131
132    /// Returns required API version
133    fn api_version(&self) -> Option<ApiVersion> {
134        Some(ApiVersion::new(2, 0))
135    }
136}
137impl Pageable for Request<'_> {}
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().build().unwrap().service_type(),
154            ServiceType::LoadBalancer
155        );
156    }
157
158    #[test]
159    fn test_response_key() {
160        assert_eq!(
161            Request::builder().build().unwrap().response_key().unwrap(),
162            "flavors"
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::GET)
173                .path("/lbaas/flavors".to_string());
174
175            then.status(200)
176                .header("content-type", "application/json")
177                .json_body(json!({ "flavors": {} }));
178        });
179
180        let endpoint = Request::builder().build().unwrap();
181        let _: serde_json::Value = endpoint.query(&client).unwrap();
182        mock.assert();
183    }
184
185    #[cfg(feature = "sync")]
186    #[test]
187    fn endpoint_headers() {
188        let server = MockServer::start();
189        let client = FakeOpenStackClient::new(server.base_url());
190        let mock = server.mock(|when, then| {
191            when.method(httpmock::Method::GET)
192                .path("/lbaas/flavors".to_string())
193                .header("foo", "bar")
194                .header("not_foo", "not_bar");
195            then.status(200)
196                .header("content-type", "application/json")
197                .json_body(json!({ "flavors": {} }));
198        });
199
200        let endpoint = Request::builder()
201            .headers(
202                [(
203                    Some(HeaderName::from_static("foo")),
204                    HeaderValue::from_static("bar"),
205                )]
206                .into_iter(),
207            )
208            .header(
209                HeaderName::from_static("not_foo"),
210                HeaderValue::from_static("not_bar"),
211            )
212            .build()
213            .unwrap();
214        let _: serde_json::Value = endpoint.query(&client).unwrap();
215        mock.assert();
216    }
217}