Skip to main content

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