Skip to main content

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