openstack_sdk/api/identity/v3/os_trust/trust/
head.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//! Dispatch for LIST trusts.
19//!
20//! GET /v3/OS-TRUST/trusts
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    /// Represents the user who is capable of consuming the trust.
33    #[builder(default, setter(into))]
34    trustee_user_id: Option<Cow<'a, str>>,
35
36    /// Represents the user who created the trust, and who's authorization is
37    /// being delegated.
38    #[builder(default, setter(into))]
39    trustor_user_id: Option<Cow<'a, str>>,
40
41    #[builder(setter(name = "_headers"), default, private)]
42    _headers: Option<HeaderMap>,
43}
44impl<'a> Request<'a> {
45    /// Create a builder for the endpoint.
46    pub fn builder() -> RequestBuilder<'a> {
47        RequestBuilder::default()
48    }
49}
50
51impl<'a> RequestBuilder<'a> {
52    /// Add a single header to the Trust.
53    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
54    where
55        K: Into<HeaderName>,
56        V: Into<HeaderValue>,
57    {
58        self._headers
59            .get_or_insert(None)
60            .get_or_insert_with(HeaderMap::new)
61            .insert(header_name.into(), header_value.into());
62        self
63    }
64
65    /// Add multiple headers.
66    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
67    where
68        I: Iterator<Item = T>,
69        T: Into<(Option<HeaderName>, HeaderValue)>,
70    {
71        self._headers
72            .get_or_insert(None)
73            .get_or_insert_with(HeaderMap::new)
74            .extend(iter.map(Into::into));
75        self
76    }
77}
78
79impl RestEndpoint for Request<'_> {
80    fn method(&self) -> http::Method {
81        http::Method::HEAD
82    }
83
84    fn endpoint(&self) -> Cow<'static, str> {
85        "OS-TRUST/trusts".to_string().into()
86    }
87
88    fn parameters(&self) -> QueryParams<'_> {
89        let mut params = QueryParams::default();
90        params.push_opt("trustee_user_id", self.trustee_user_id.as_ref());
91        params.push_opt("trustor_user_id", self.trustor_user_id.as_ref());
92
93        params
94    }
95
96    fn service_type(&self) -> ServiceType {
97        ServiceType::Identity
98    }
99
100    fn response_key(&self) -> Option<Cow<'static, str>> {
101        None
102    }
103
104    /// Returns headers to be set into the request
105    fn request_headers(&self) -> Option<&HeaderMap> {
106        self._headers.as_ref()
107    }
108
109    /// Returns required API version
110    fn api_version(&self) -> Option<ApiVersion> {
111        Some(ApiVersion::new(3, 0))
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118    #[cfg(feature = "sync")]
119    use crate::api::RawQuery;
120    use crate::test::client::FakeOpenStackClient;
121    use crate::types::ServiceType;
122    use http::{HeaderName, HeaderValue};
123    use httpmock::MockServer;
124
125    #[test]
126    fn test_service_type() {
127        assert_eq!(
128            Request::builder().build().unwrap().service_type(),
129            ServiceType::Identity
130        );
131    }
132
133    #[test]
134    fn test_response_key() {
135        assert!(Request::builder().build().unwrap().response_key().is_none())
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::HEAD)
145                .path("/OS-TRUST/trusts".to_string());
146
147            then.status(200).header("content-type", "application/json");
148        });
149
150        let endpoint = Request::builder().build().unwrap();
151        let _ = endpoint.raw_query(&client).unwrap();
152        mock.assert();
153    }
154
155    #[cfg(feature = "sync")]
156    #[test]
157    fn endpoint_headers() {
158        let server = MockServer::start();
159        let client = FakeOpenStackClient::new(server.base_url());
160        let mock = server.mock(|when, then| {
161            when.method(httpmock::Method::HEAD)
162                .path("/OS-TRUST/trusts".to_string())
163                .header("foo", "bar")
164                .header("not_foo", "not_bar");
165            then.status(200).header("content-type", "application/json");
166        });
167
168        let endpoint = Request::builder()
169            .headers(
170                [(
171                    Some(HeaderName::from_static("foo")),
172                    HeaderValue::from_static("bar"),
173                )]
174                .into_iter(),
175            )
176            .header(
177                HeaderName::from_static("not_foo"),
178                HeaderValue::from_static("not_bar"),
179            )
180            .build()
181            .unwrap();
182        let _ = endpoint.raw_query(&client).unwrap();
183        mock.assert();
184    }
185}