openstack_sdk/api/network/v2/address_group/
remove_addresses.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 AddressGroup<'a> {
30    /// A list of IP addresses.
31    #[serde(skip_serializing_if = "Option::is_none")]
32    #[builder(default, setter(into))]
33    pub(crate) addresses: Option<Vec<Cow<'a, str>>>,
34}
35
36#[derive(Builder, Debug, Clone)]
37#[builder(setter(strip_option))]
38pub struct Request<'a> {
39    #[builder(setter(into))]
40    pub(crate) address_group: AddressGroup<'a>,
41
42    /// id parameter for /v2.0/address-groups/{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 Address_Group.
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!(
91            "address-groups/{id}/remove_addresses",
92            id = self.id.as_ref(),
93        )
94        .into()
95    }
96
97    fn parameters(&self) -> QueryParams<'_> {
98        QueryParams::default()
99    }
100
101    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
102        let mut params = JsonBodyParams::default();
103
104        params.push("address_group", serde_json::to_value(&self.address_group)?);
105
106        params.into_body()
107    }
108
109    fn service_type(&self) -> ServiceType {
110        ServiceType::Network
111    }
112
113    fn response_key(&self) -> Option<Cow<'static, str>> {
114        Some("address_group".into())
115    }
116
117    /// Returns headers to be set into the request
118    fn request_headers(&self) -> Option<&HeaderMap> {
119        self._headers.as_ref()
120    }
121
122    /// Returns required API version
123    fn api_version(&self) -> Option<ApiVersion> {
124        Some(ApiVersion::new(2, 0))
125    }
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131    #[cfg(feature = "sync")]
132    use crate::api::Query;
133    use crate::test::client::FakeOpenStackClient;
134    use crate::types::ServiceType;
135    use http::{HeaderName, HeaderValue};
136    use httpmock::MockServer;
137    use serde_json::json;
138
139    #[test]
140    fn test_service_type() {
141        assert_eq!(
142            Request::builder()
143                .address_group(AddressGroupBuilder::default().build().unwrap())
144                .build()
145                .unwrap()
146                .service_type(),
147            ServiceType::Network
148        );
149    }
150
151    #[test]
152    fn test_response_key() {
153        assert_eq!(
154            Request::builder()
155                .address_group(AddressGroupBuilder::default().build().unwrap())
156                .build()
157                .unwrap()
158                .response_key()
159                .unwrap(),
160            "address_group"
161        );
162    }
163
164    #[cfg(feature = "sync")]
165    #[test]
166    fn endpoint() {
167        let server = MockServer::start();
168        let client = FakeOpenStackClient::new(server.base_url());
169        let mock = server.mock(|when, then| {
170            when.method(httpmock::Method::PUT)
171                .path(format!("/address-groups/{id}/remove_addresses", id = "id",));
172
173            then.status(200)
174                .header("content-type", "application/json")
175                .json_body(json!({ "address_group": {} }));
176        });
177
178        let endpoint = Request::builder()
179            .id("id")
180            .address_group(AddressGroupBuilder::default().build().unwrap())
181            .build()
182            .unwrap();
183        let _: serde_json::Value = endpoint.query(&client).unwrap();
184        mock.assert();
185    }
186
187    #[cfg(feature = "sync")]
188    #[test]
189    fn endpoint_headers() {
190        let server = MockServer::start();
191        let client = FakeOpenStackClient::new(server.base_url());
192        let mock = server.mock(|when, then| {
193            when.method(httpmock::Method::PUT)
194                .path(format!("/address-groups/{id}/remove_addresses", id = "id",))
195                .header("foo", "bar")
196                .header("not_foo", "not_bar");
197            then.status(200)
198                .header("content-type", "application/json")
199                .json_body(json!({ "address_group": {} }));
200        });
201
202        let endpoint = Request::builder()
203            .id("id")
204            .address_group(AddressGroupBuilder::default().build().unwrap())
205            .headers(
206                [(
207                    Some(HeaderName::from_static("foo")),
208                    HeaderValue::from_static("bar"),
209                )]
210                .into_iter(),
211            )
212            .header(
213                HeaderName::from_static("not_foo"),
214                HeaderValue::from_static("not_bar"),
215            )
216            .build()
217            .unwrap();
218        let _: serde_json::Value = endpoint.query(&client).unwrap();
219        mock.assert();
220    }
221}