Skip to main content

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