openstack_sdk/api/load_balancer/v2/flavor/
get.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//! Gets a flavor's detail.
19//!
20use derive_builder::Builder;
21use http::{HeaderMap, HeaderName, HeaderValue};
22
23use crate::api::rest_endpoint_prelude::*;
24
25use std::borrow::Cow;
26
27#[derive(Builder, Debug, Clone)]
28#[builder(setter(strip_option))]
29pub struct Request<'a> {
30    /// flavor_id parameter for /v2/lbaas/flavors/{flavor_id} API
31    #[builder(default, setter(into))]
32    id: Cow<'a, str>,
33
34    #[builder(setter(name = "_headers"), default, private)]
35    _headers: Option<HeaderMap>,
36}
37impl<'a> Request<'a> {
38    /// Create a builder for the endpoint.
39    pub fn builder() -> RequestBuilder<'a> {
40        RequestBuilder::default()
41    }
42}
43
44impl<'a> RequestBuilder<'a> {
45    /// Add a single header to the Flavor.
46    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
47    where
48        K: Into<HeaderName>,
49        V: Into<HeaderValue>,
50    {
51        self._headers
52            .get_or_insert(None)
53            .get_or_insert_with(HeaderMap::new)
54            .insert(header_name.into(), header_value.into());
55        self
56    }
57
58    /// Add multiple headers.
59    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
60    where
61        I: Iterator<Item = T>,
62        T: Into<(Option<HeaderName>, HeaderValue)>,
63    {
64        self._headers
65            .get_or_insert(None)
66            .get_or_insert_with(HeaderMap::new)
67            .extend(iter.map(Into::into));
68        self
69    }
70}
71
72impl RestEndpoint for Request<'_> {
73    fn method(&self) -> http::Method {
74        http::Method::GET
75    }
76
77    fn endpoint(&self) -> Cow<'static, str> {
78        format!("lbaas/flavors/{id}", id = self.id.as_ref(),).into()
79    }
80
81    fn parameters(&self) -> QueryParams<'_> {
82        QueryParams::default()
83    }
84
85    fn service_type(&self) -> ServiceType {
86        ServiceType::LoadBalancer
87    }
88
89    fn response_key(&self) -> Option<Cow<'static, str>> {
90        Some("flavor".into())
91    }
92
93    /// Returns headers to be set into the request
94    fn request_headers(&self) -> Option<&HeaderMap> {
95        self._headers.as_ref()
96    }
97
98    /// Returns required API version
99    fn api_version(&self) -> Option<ApiVersion> {
100        Some(ApiVersion::new(2, 0))
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107    #[cfg(feature = "sync")]
108    use crate::api::Query;
109    use crate::test::client::FakeOpenStackClient;
110    use crate::types::ServiceType;
111    use http::{HeaderName, HeaderValue};
112    use httpmock::MockServer;
113    use serde_json::json;
114
115    #[test]
116    fn test_service_type() {
117        assert_eq!(
118            Request::builder().build().unwrap().service_type(),
119            ServiceType::LoadBalancer
120        );
121    }
122
123    #[test]
124    fn test_response_key() {
125        assert_eq!(
126            Request::builder().build().unwrap().response_key().unwrap(),
127            "flavor"
128        );
129    }
130
131    #[cfg(feature = "sync")]
132    #[test]
133    fn endpoint() {
134        let server = MockServer::start();
135        let client = FakeOpenStackClient::new(server.base_url());
136        let mock = server.mock(|when, then| {
137            when.method(httpmock::Method::GET)
138                .path(format!("/lbaas/flavors/{id}", id = "id",));
139
140            then.status(200)
141                .header("content-type", "application/json")
142                .json_body(json!({ "flavor": {} }));
143        });
144
145        let endpoint = Request::builder().id("id").build().unwrap();
146        let _: serde_json::Value = endpoint.query(&client).unwrap();
147        mock.assert();
148    }
149
150    #[cfg(feature = "sync")]
151    #[test]
152    fn endpoint_headers() {
153        let server = MockServer::start();
154        let client = FakeOpenStackClient::new(server.base_url());
155        let mock = server.mock(|when, then| {
156            when.method(httpmock::Method::GET)
157                .path(format!("/lbaas/flavors/{id}", id = "id",))
158                .header("foo", "bar")
159                .header("not_foo", "not_bar");
160            then.status(200)
161                .header("content-type", "application/json")
162                .json_body(json!({ "flavor": {} }));
163        });
164
165        let endpoint = Request::builder()
166            .id("id")
167            .headers(
168                [(
169                    Some(HeaderName::from_static("foo")),
170                    HeaderValue::from_static("bar"),
171                )]
172                .into_iter(),
173            )
174            .header(
175                HeaderName::from_static("not_foo"),
176                HeaderValue::from_static("not_bar"),
177            )
178            .build()
179            .unwrap();
180        let _: serde_json::Value = endpoint.query(&client).unwrap();
181        mock.assert();
182    }
183}