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