Skip to main content

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