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