Skip to main content

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