openstack_sdk/api/network/v2/rbac_policy/
set.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//! Update RBAC policy for given tenant.
19//!
20//! Normal response codes: 200
21//!
22//! Error response codes: 400, 401, 403, 404
23//!
24use derive_builder::Builder;
25use http::{HeaderMap, HeaderName, HeaderValue};
26
27use crate::api::rest_endpoint_prelude::*;
28
29use serde::Deserialize;
30use serde::Serialize;
31use std::borrow::Cow;
32
33#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
34#[builder(setter(strip_option))]
35pub struct RbacPolicy<'a> {
36    /// The ID of the tenant to which the RBAC policy will be enforced. Please
37    /// note that Neutron does not perform any type of validation that the
38    /// value provided is actually the ID of the existing project. If, for
39    /// example, the name of the project is provided here, it will be accepted
40    /// by the Neutron API, but the RBAC rule created will not work as
41    /// expected.
42    #[serde(skip_serializing_if = "Option::is_none")]
43    #[builder(default, setter(into))]
44    pub(crate) target_tenant: Option<Cow<'a, str>>,
45}
46
47#[derive(Builder, Debug, Clone)]
48#[builder(setter(strip_option))]
49pub struct Request<'a> {
50    #[builder(setter(into))]
51    pub(crate) rbac_policy: RbacPolicy<'a>,
52
53    /// id parameter for /v2.0/rbac-policies/{id} API
54    #[builder(default, setter(into))]
55    id: Cow<'a, str>,
56
57    #[builder(setter(name = "_headers"), default, private)]
58    _headers: Option<HeaderMap>,
59}
60impl<'a> Request<'a> {
61    /// Create a builder for the endpoint.
62    pub fn builder() -> RequestBuilder<'a> {
63        RequestBuilder::default()
64    }
65}
66
67impl<'a> RequestBuilder<'a> {
68    /// Add a single header to the Rbac_Policy.
69    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
70    where
71        K: Into<HeaderName>,
72        V: Into<HeaderValue>,
73    {
74        self._headers
75            .get_or_insert(None)
76            .get_or_insert_with(HeaderMap::new)
77            .insert(header_name.into(), header_value.into());
78        self
79    }
80
81    /// Add multiple headers.
82    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
83    where
84        I: Iterator<Item = T>,
85        T: Into<(Option<HeaderName>, HeaderValue)>,
86    {
87        self._headers
88            .get_or_insert(None)
89            .get_or_insert_with(HeaderMap::new)
90            .extend(iter.map(Into::into));
91        self
92    }
93}
94
95impl RestEndpoint for Request<'_> {
96    fn method(&self) -> http::Method {
97        http::Method::PUT
98    }
99
100    fn endpoint(&self) -> Cow<'static, str> {
101        format!("rbac-policies/{id}", id = self.id.as_ref(),).into()
102    }
103
104    fn parameters(&self) -> QueryParams<'_> {
105        QueryParams::default()
106    }
107
108    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
109        let mut params = JsonBodyParams::default();
110
111        params.push("rbac_policy", serde_json::to_value(&self.rbac_policy)?);
112
113        params.into_body()
114    }
115
116    fn service_type(&self) -> ServiceType {
117        ServiceType::Network
118    }
119
120    fn response_key(&self) -> Option<Cow<'static, str>> {
121        Some("rbac_policy".into())
122    }
123
124    /// Returns headers to be set into the request
125    fn request_headers(&self) -> Option<&HeaderMap> {
126        self._headers.as_ref()
127    }
128
129    /// Returns required API version
130    fn api_version(&self) -> Option<ApiVersion> {
131        Some(ApiVersion::new(2, 0))
132    }
133}
134
135#[cfg(test)]
136mod tests {
137    use super::*;
138    #[cfg(feature = "sync")]
139    use crate::api::Query;
140    use crate::test::client::FakeOpenStackClient;
141    use crate::types::ServiceType;
142    use http::{HeaderName, HeaderValue};
143    use httpmock::MockServer;
144    use serde_json::json;
145
146    #[test]
147    fn test_service_type() {
148        assert_eq!(
149            Request::builder()
150                .rbac_policy(RbacPolicyBuilder::default().build().unwrap())
151                .build()
152                .unwrap()
153                .service_type(),
154            ServiceType::Network
155        );
156    }
157
158    #[test]
159    fn test_response_key() {
160        assert_eq!(
161            Request::builder()
162                .rbac_policy(RbacPolicyBuilder::default().build().unwrap())
163                .build()
164                .unwrap()
165                .response_key()
166                .unwrap(),
167            "rbac_policy"
168        );
169    }
170
171    #[cfg(feature = "sync")]
172    #[test]
173    fn endpoint() {
174        let server = MockServer::start();
175        let client = FakeOpenStackClient::new(server.base_url());
176        let mock = server.mock(|when, then| {
177            when.method(httpmock::Method::PUT)
178                .path(format!("/rbac-policies/{id}", id = "id",));
179
180            then.status(200)
181                .header("content-type", "application/json")
182                .json_body(json!({ "rbac_policy": {} }));
183        });
184
185        let endpoint = Request::builder()
186            .id("id")
187            .rbac_policy(RbacPolicyBuilder::default().build().unwrap())
188            .build()
189            .unwrap();
190        let _: serde_json::Value = endpoint.query(&client).unwrap();
191        mock.assert();
192    }
193
194    #[cfg(feature = "sync")]
195    #[test]
196    fn endpoint_headers() {
197        let server = MockServer::start();
198        let client = FakeOpenStackClient::new(server.base_url());
199        let mock = server.mock(|when, then| {
200            when.method(httpmock::Method::PUT)
201                .path(format!("/rbac-policies/{id}", id = "id",))
202                .header("foo", "bar")
203                .header("not_foo", "not_bar");
204            then.status(200)
205                .header("content-type", "application/json")
206                .json_body(json!({ "rbac_policy": {} }));
207        });
208
209        let endpoint = Request::builder()
210            .id("id")
211            .rbac_policy(RbacPolicyBuilder::default().build().unwrap())
212            .headers(
213                [(
214                    Some(HeaderName::from_static("foo")),
215                    HeaderValue::from_static("bar"),
216                )]
217                .into_iter(),
218            )
219            .header(
220                HeaderName::from_static("not_foo"),
221                HeaderValue::from_static("not_bar"),
222            )
223            .build()
224            .unwrap();
225        let _: serde_json::Value = endpoint.query(&client).unwrap();
226        mock.assert();
227    }
228}