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