Skip to main content

openstack_sdk_identity/v3/os_federation/service_provider/
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 a service provider.
19//!
20//! PATCH /OS-FEDERATION/service_providers/{service_provider_id}
21//!
22use derive_builder::Builder;
23use http::{HeaderMap, HeaderName, HeaderValue};
24
25use openstack_sdk_core::api::rest_endpoint_prelude::*;
26
27use serde::Deserialize;
28use serde::Serialize;
29use std::borrow::Cow;
30
31#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
32#[builder(setter(strip_option))]
33pub struct ServiceProvider<'a> {
34    /// The URL to authenticate against
35    #[serde(skip_serializing_if = "Option::is_none")]
36    #[builder(default, setter(into))]
37    pub(crate) auth_url: Option<Cow<'a, str>>,
38
39    /// The description of the service provider
40    #[serde(skip_serializing_if = "Option::is_none")]
41    #[builder(default, setter(into))]
42    pub(crate) description: Option<Option<Cow<'a, str>>>,
43
44    /// Whether the service provider is enabled or not
45    #[serde(skip_serializing_if = "Option::is_none")]
46    #[builder(default, setter(into))]
47    pub(crate) enabled: Option<bool>,
48
49    /// The prefix of the RelayState SAML attribute
50    #[serde(skip_serializing_if = "Option::is_none")]
51    #[builder(default, setter(into))]
52    pub(crate) relay_state_prefix: Option<Option<Cow<'a, str>>>,
53
54    /// The service provider's URL
55    #[serde(skip_serializing_if = "Option::is_none")]
56    #[builder(default, setter(into))]
57    pub(crate) sp_url: Option<Cow<'a, str>>,
58}
59
60#[derive(Builder, Debug, Clone)]
61#[builder(setter(strip_option))]
62pub struct Request<'a> {
63    #[builder(setter(into))]
64    pub(crate) service_provider: ServiceProvider<'a>,
65
66    /// service_provider_id parameter for
67    /// /v3/OS-FEDERATION/service_providers/{service_provider_id} API
68    #[builder(default, setter(into))]
69    id: Cow<'a, str>,
70
71    #[builder(setter(name = "_headers"), default, private)]
72    _headers: Option<HeaderMap>,
73}
74impl<'a> Request<'a> {
75    /// Create a builder for the endpoint.
76    pub fn builder() -> RequestBuilder<'a> {
77        RequestBuilder::default()
78    }
79}
80
81impl<'a> RequestBuilder<'a> {
82    /// Add a single header to the Service_Provider.
83    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
84    where
85        K: Into<HeaderName>,
86        V: Into<HeaderValue>,
87    {
88        self._headers
89            .get_or_insert(None)
90            .get_or_insert_with(HeaderMap::new)
91            .insert(header_name.into(), header_value.into());
92        self
93    }
94
95    /// Add multiple headers.
96    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
97    where
98        I: Iterator<Item = T>,
99        T: Into<(Option<HeaderName>, HeaderValue)>,
100    {
101        self._headers
102            .get_or_insert(None)
103            .get_or_insert_with(HeaderMap::new)
104            .extend(iter.map(Into::into));
105        self
106    }
107}
108
109impl RestEndpoint for Request<'_> {
110    fn method(&self) -> http::Method {
111        http::Method::PATCH
112    }
113
114    fn endpoint(&self) -> Cow<'static, str> {
115        format!(
116            "OS-FEDERATION/service_providers/{id}",
117            id = self.id.as_ref(),
118        )
119        .into()
120    }
121
122    fn parameters(&self) -> QueryParams<'_> {
123        QueryParams::default()
124    }
125
126    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
127        let mut params = JsonBodyParams::default();
128
129        params.push(
130            "service_provider",
131            serde_json::to_value(&self.service_provider)?,
132        );
133
134        params.into_body()
135    }
136
137    fn service_type(&self) -> ServiceType {
138        ServiceType::Identity
139    }
140
141    fn response_key(&self) -> Option<Cow<'static, str>> {
142        Some("service_provider".into())
143    }
144
145    /// Returns headers to be set into the request
146    fn request_headers(&self) -> Option<&HeaderMap> {
147        self._headers.as_ref()
148    }
149
150    /// Returns required API version
151    fn api_version(&self) -> Option<ApiVersion> {
152        Some(ApiVersion::new(3, 0))
153    }
154}
155
156#[cfg(test)]
157mod tests {
158    use super::*;
159    use http::{HeaderName, HeaderValue};
160    use httpmock::MockServer;
161    #[cfg(feature = "sync")]
162    use openstack_sdk_core::api::Query;
163    use openstack_sdk_core::test::client::FakeOpenStackClient;
164    use openstack_sdk_core::types::ServiceType;
165    use serde_json::json;
166
167    #[test]
168    fn test_service_type() {
169        assert_eq!(
170            Request::builder()
171                .service_provider(ServiceProviderBuilder::default().build().unwrap())
172                .build()
173                .unwrap()
174                .service_type(),
175            ServiceType::Identity
176        );
177    }
178
179    #[test]
180    fn test_response_key() {
181        assert_eq!(
182            Request::builder()
183                .service_provider(ServiceProviderBuilder::default().build().unwrap())
184                .build()
185                .unwrap()
186                .response_key()
187                .unwrap(),
188            "service_provider"
189        );
190    }
191
192    #[cfg(feature = "sync")]
193    #[test]
194    fn endpoint() {
195        let server = MockServer::start();
196        let client = FakeOpenStackClient::new(server.base_url());
197        let mock = server.mock(|when, then| {
198            when.method(httpmock::Method::PATCH)
199                .path(format!("/OS-FEDERATION/service_providers/{id}", id = "id",));
200
201            then.status(200)
202                .header("content-type", "application/json")
203                .json_body(json!({ "service_provider": {} }));
204        });
205
206        let endpoint = Request::builder()
207            .id("id")
208            .service_provider(ServiceProviderBuilder::default().build().unwrap())
209            .build()
210            .unwrap();
211        let _: serde_json::Value = endpoint.query(&client).unwrap();
212        mock.assert();
213    }
214
215    #[cfg(feature = "sync")]
216    #[test]
217    fn endpoint_headers() {
218        let server = MockServer::start();
219        let client = FakeOpenStackClient::new(server.base_url());
220        let mock = server.mock(|when, then| {
221            when.method(httpmock::Method::PATCH)
222                .path(format!("/OS-FEDERATION/service_providers/{id}", id = "id",))
223                .header("foo", "bar")
224                .header("not_foo", "not_bar");
225            then.status(200)
226                .header("content-type", "application/json")
227                .json_body(json!({ "service_provider": {} }));
228        });
229
230        let endpoint = Request::builder()
231            .id("id")
232            .service_provider(ServiceProviderBuilder::default().build().unwrap())
233            .headers(
234                [(
235                    Some(HeaderName::from_static("foo")),
236                    HeaderValue::from_static("bar"),
237                )]
238                .into_iter(),
239            )
240            .header(
241                HeaderName::from_static("not_foo"),
242                HeaderValue::from_static("not_bar"),
243            )
244            .build()
245            .unwrap();
246        let _: serde_json::Value = endpoint.query(&client).unwrap();
247        mock.assert();
248    }
249}