openstack_sdk/api/placement/v1/resource_provider/
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//! Return a representation of the resource provider identified by {uuid}.
19//!
20//! Normal Response Codes: 200
21//!
22//! Error response codes: 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    /// uuid parameter for /resource_providers/{uuid} API
35    #[builder(default, setter(into))]
36    uuid: 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<'a> RequestBuilder<'a> {
49    /// Add a single header to the Resource_Provider.
50    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
51    where
52        K: Into<HeaderName>,
53        V: Into<HeaderValue>,
54    {
55        self._headers
56            .get_or_insert(None)
57            .get_or_insert_with(HeaderMap::new)
58            .insert(header_name.into(), header_value.into());
59        self
60    }
61
62    /// Add multiple headers.
63    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
64    where
65        I: Iterator<Item = T>,
66        T: Into<(Option<HeaderName>, HeaderValue)>,
67    {
68        self._headers
69            .get_or_insert(None)
70            .get_or_insert_with(HeaderMap::new)
71            .extend(iter.map(Into::into));
72        self
73    }
74}
75
76impl RestEndpoint for Request<'_> {
77    fn method(&self) -> http::Method {
78        http::Method::GET
79    }
80
81    fn endpoint(&self) -> Cow<'static, str> {
82        format!("resource_providers/{uuid}", uuid = self.uuid.as_ref(),).into()
83    }
84
85    fn parameters(&self) -> QueryParams<'_> {
86        QueryParams::default()
87    }
88
89    fn service_type(&self) -> ServiceType {
90        ServiceType::Placement
91    }
92
93    fn response_key(&self) -> Option<Cow<'static, str>> {
94        None
95    }
96
97    /// Returns headers to be set into the request
98    fn request_headers(&self) -> Option<&HeaderMap> {
99        self._headers.as_ref()
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106    #[cfg(feature = "sync")]
107    use crate::api::Query;
108    use crate::test::client::FakeOpenStackClient;
109    use crate::types::ServiceType;
110    use http::{HeaderName, HeaderValue};
111    use httpmock::MockServer;
112    use serde_json::json;
113
114    #[test]
115    fn test_service_type() {
116        assert_eq!(
117            Request::builder().build().unwrap().service_type(),
118            ServiceType::Placement
119        );
120    }
121
122    #[test]
123    fn test_response_key() {
124        assert!(Request::builder().build().unwrap().response_key().is_none())
125    }
126
127    #[cfg(feature = "sync")]
128    #[test]
129    fn endpoint() {
130        let server = MockServer::start();
131        let client = FakeOpenStackClient::new(server.base_url());
132        let mock = server.mock(|when, then| {
133            when.method(httpmock::Method::GET)
134                .path(format!("/resource_providers/{uuid}", uuid = "uuid",));
135
136            then.status(200)
137                .header("content-type", "application/json")
138                .json_body(json!({ "dummy": {} }));
139        });
140
141        let endpoint = Request::builder().uuid("uuid").build().unwrap();
142        let _: serde_json::Value = endpoint.query(&client).unwrap();
143        mock.assert();
144    }
145
146    #[cfg(feature = "sync")]
147    #[test]
148    fn endpoint_headers() {
149        let server = MockServer::start();
150        let client = FakeOpenStackClient::new(server.base_url());
151        let mock = server.mock(|when, then| {
152            when.method(httpmock::Method::GET)
153                .path(format!("/resource_providers/{uuid}", uuid = "uuid",))
154                .header("foo", "bar")
155                .header("not_foo", "not_bar");
156            then.status(200)
157                .header("content-type", "application/json")
158                .json_body(json!({ "dummy": {} }));
159        });
160
161        let endpoint = Request::builder()
162            .uuid("uuid")
163            .headers(
164                [(
165                    Some(HeaderName::from_static("foo")),
166                    HeaderValue::from_static("bar"),
167                )]
168                .into_iter(),
169            )
170            .header(
171                HeaderName::from_static("not_foo"),
172                HeaderValue::from_static("not_bar"),
173            )
174            .build()
175            .unwrap();
176        let _: serde_json::Value = endpoint.query(&client).unwrap();
177        mock.assert();
178    }
179}