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