openstack_sdk/api/compute/v2/floating_ip/
delete.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//! Deletes, or deallocates, a floating IP address from the current project and
19//! returns it to the pool from which it was allocated.
20//!
21//! If the IP address is still associated with a running instance, it is
22//! automatically disassociated from that instance.
23//!
24//! Policy defaults enable only users with the administrative role or the owner
25//! of the server to perform this operation. Cloud providers can change these
26//! permissions through the `policy.json` file.
27//!
28//! Normal response codes: 202
29//!
30//! Error response codes: badRequest(400), unauthorized(401), forbidden(403),
31//! itemNotFound(404)
32//!
33use derive_builder::Builder;
34use http::{HeaderMap, HeaderName, HeaderValue};
35
36use crate::api::rest_endpoint_prelude::*;
37
38use std::borrow::Cow;
39
40#[derive(Builder, Debug, Clone)]
41#[builder(setter(strip_option))]
42pub struct Request<'a> {
43    /// id parameter for /v2.1/os-floating-ips/{id} API
44    #[builder(default, setter(into))]
45    id: Cow<'a, str>,
46
47    #[builder(setter(name = "_headers"), default, private)]
48    _headers: Option<HeaderMap>,
49}
50impl<'a> Request<'a> {
51    /// Create a builder for the endpoint.
52    pub fn builder() -> RequestBuilder<'a> {
53        RequestBuilder::default()
54    }
55}
56
57impl<'a> RequestBuilder<'a> {
58    /// Add a single header to the Floating_Ip.
59    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
60    where
61        K: Into<HeaderName>,
62        V: Into<HeaderValue>,
63    {
64        self._headers
65            .get_or_insert(None)
66            .get_or_insert_with(HeaderMap::new)
67            .insert(header_name.into(), header_value.into());
68        self
69    }
70
71    /// Add multiple headers.
72    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
73    where
74        I: Iterator<Item = T>,
75        T: Into<(Option<HeaderName>, HeaderValue)>,
76    {
77        self._headers
78            .get_or_insert(None)
79            .get_or_insert_with(HeaderMap::new)
80            .extend(iter.map(Into::into));
81        self
82    }
83}
84
85impl RestEndpoint for Request<'_> {
86    fn method(&self) -> http::Method {
87        http::Method::DELETE
88    }
89
90    fn endpoint(&self) -> Cow<'static, str> {
91        format!("os-floating-ips/{id}", id = self.id.as_ref(),).into()
92    }
93
94    fn parameters(&self) -> QueryParams<'_> {
95        QueryParams::default()
96    }
97
98    fn service_type(&self) -> ServiceType {
99        ServiceType::Compute
100    }
101
102    fn response_key(&self) -> Option<Cow<'static, str>> {
103        None
104    }
105
106    /// Returns headers to be set into the request
107    fn request_headers(&self) -> Option<&HeaderMap> {
108        self._headers.as_ref()
109    }
110
111    /// Returns required API version
112    fn api_version(&self) -> Option<ApiVersion> {
113        Some(ApiVersion::new(2, 1))
114    }
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120    #[cfg(feature = "sync")]
121    use crate::api::Query;
122    use crate::test::client::FakeOpenStackClient;
123    use crate::types::ServiceType;
124    use http::{HeaderName, HeaderValue};
125    use httpmock::MockServer;
126    use serde_json::json;
127
128    #[test]
129    fn test_service_type() {
130        assert_eq!(
131            Request::builder().build().unwrap().service_type(),
132            ServiceType::Compute
133        );
134    }
135
136    #[test]
137    fn test_response_key() {
138        assert!(Request::builder().build().unwrap().response_key().is_none())
139    }
140
141    #[cfg(feature = "sync")]
142    #[test]
143    fn endpoint() {
144        let server = MockServer::start();
145        let client = FakeOpenStackClient::new(server.base_url());
146        let mock = server.mock(|when, then| {
147            when.method(httpmock::Method::DELETE)
148                .path(format!("/os-floating-ips/{id}", id = "id",));
149
150            then.status(200)
151                .header("content-type", "application/json")
152                .json_body(json!({ "dummy": {} }));
153        });
154
155        let endpoint = Request::builder().id("id").build().unwrap();
156        let _: serde_json::Value = endpoint.query(&client).unwrap();
157        mock.assert();
158    }
159
160    #[cfg(feature = "sync")]
161    #[test]
162    fn endpoint_headers() {
163        let server = MockServer::start();
164        let client = FakeOpenStackClient::new(server.base_url());
165        let mock = server.mock(|when, then| {
166            when.method(httpmock::Method::DELETE)
167                .path(format!("/os-floating-ips/{id}", id = "id",))
168                .header("foo", "bar")
169                .header("not_foo", "not_bar");
170            then.status(200)
171                .header("content-type", "application/json")
172                .json_body(json!({ "dummy": {} }));
173        });
174
175        let endpoint = Request::builder()
176            .id("id")
177            .headers(
178                [(
179                    Some(HeaderName::from_static("foo")),
180                    HeaderValue::from_static("bar"),
181                )]
182                .into_iter(),
183            )
184            .header(
185                HeaderName::from_static("not_foo"),
186                HeaderValue::from_static("not_bar"),
187            )
188            .build()
189            .unwrap();
190        let _: serde_json::Value = endpoint.query(&client).unwrap();
191        mock.assert();
192    }
193}