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