openstack_sdk/api/identity/v3/user/access_rule/
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//! Get access rule resource.
19//!
20//! GET/HEAD /v3/users/{user_id}/access_rules/{access_rule_id}
21//!
22use derive_builder::Builder;
23use http::{HeaderMap, HeaderName, HeaderValue};
24
25use crate::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    /// access_rule_id parameter for
33    /// /v3/users/{user_id}/access_rules/{access_rule_id} API
34    #[builder(default, setter(into))]
35    id: Cow<'a, str>,
36
37    /// user_id parameter for /v3/users/{user_id}/access_rules/{access_rule_id}
38    /// API
39    #[builder(default, setter(into))]
40    user_id: 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 Access_Rule.
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::HEAD
83    }
84
85    fn endpoint(&self) -> Cow<'static, str> {
86        format!(
87            "users/{user_id}/access_rules/{id}",
88            id = self.id.as_ref(),
89            user_id = self.user_id.as_ref(),
90        )
91        .into()
92    }
93
94    fn parameters(&self) -> QueryParams<'_> {
95        QueryParams::default()
96    }
97
98    fn service_type(&self) -> ServiceType {
99        ServiceType::Identity
100    }
101
102    fn response_key(&self) -> Option<Cow<'static, str>> {
103        None
104    }
105
106    /// Returns headers to be set into the request
107    fn request_headers(&self) -> Option<&HeaderMap> {
108        self._headers.as_ref()
109    }
110
111    /// Returns required API version
112    fn api_version(&self) -> Option<ApiVersion> {
113        Some(ApiVersion::new(3, 0))
114    }
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120    #[cfg(feature = "sync")]
121    use crate::api::RawQuery;
122    use crate::test::client::FakeOpenStackClient;
123    use crate::types::ServiceType;
124    use http::{HeaderName, HeaderValue};
125    use httpmock::MockServer;
126
127    #[test]
128    fn test_service_type() {
129        assert_eq!(
130            Request::builder().build().unwrap().service_type(),
131            ServiceType::Identity
132        );
133    }
134
135    #[test]
136    fn test_response_key() {
137        assert!(Request::builder().build().unwrap().response_key().is_none())
138    }
139
140    #[cfg(feature = "sync")]
141    #[test]
142    fn endpoint() {
143        let server = MockServer::start();
144        let client = FakeOpenStackClient::new(server.base_url());
145        let mock = server.mock(|when, then| {
146            when.method(httpmock::Method::HEAD).path(format!(
147                "/users/{user_id}/access_rules/{id}",
148                id = "id",
149                user_id = "user_id",
150            ));
151
152            then.status(200).header("content-type", "application/json");
153        });
154
155        let endpoint = Request::builder()
156            .id("id")
157            .user_id("user_id")
158            .build()
159            .unwrap();
160        let _ = endpoint.raw_query(&client).unwrap();
161        mock.assert();
162    }
163
164    #[cfg(feature = "sync")]
165    #[test]
166    fn endpoint_headers() {
167        let server = MockServer::start();
168        let client = FakeOpenStackClient::new(server.base_url());
169        let mock = server.mock(|when, then| {
170            when.method(httpmock::Method::HEAD)
171                .path(format!(
172                    "/users/{user_id}/access_rules/{id}",
173                    id = "id",
174                    user_id = "user_id",
175                ))
176                .header("foo", "bar")
177                .header("not_foo", "not_bar");
178            then.status(200).header("content-type", "application/json");
179        });
180
181        let endpoint = Request::builder()
182            .id("id")
183            .user_id("user_id")
184            .headers(
185                [(
186                    Some(HeaderName::from_static("foo")),
187                    HeaderValue::from_static("bar"),
188                )]
189                .into_iter(),
190            )
191            .header(
192                HeaderName::from_static("not_foo"),
193                HeaderValue::from_static("not_bar"),
194            )
195            .build()
196            .unwrap();
197        let _ = endpoint.raw_query(&client).unwrap();
198        mock.assert();
199    }
200}