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