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