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