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(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
60where {
61        self._headers
62            .get_or_insert(None)
63            .get_or_insert_with(HeaderMap::new)
64            .insert(header_name, HeaderValue::from_static(header_value));
65        self
66    }
67
68    /// Add multiple headers.
69    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
70    where
71        I: Iterator<Item = T>,
72        T: Into<(Option<HeaderName>, HeaderValue)>,
73    {
74        self._headers
75            .get_or_insert(None)
76            .get_or_insert_with(HeaderMap::new)
77            .extend(iter.map(Into::into));
78        self
79    }
80}
81
82impl RestEndpoint for Request<'_> {
83    fn method(&self) -> http::Method {
84        http::Method::DELETE
85    }
86
87    fn endpoint(&self) -> Cow<'static, str> {
88        format!("os-floating-ips/{id}", id = self.id.as_ref(),).into()
89    }
90
91    fn parameters(&self) -> QueryParams {
92        QueryParams::default()
93    }
94
95    fn service_type(&self) -> ServiceType {
96        ServiceType::Compute
97    }
98
99    fn response_key(&self) -> Option<Cow<'static, str>> {
100        None
101    }
102
103    /// Returns headers to be set into the request
104    fn request_headers(&self) -> Option<&HeaderMap> {
105        self._headers.as_ref()
106    }
107
108    /// Returns required API version
109    fn api_version(&self) -> Option<ApiVersion> {
110        Some(ApiVersion::new(2, 1))
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117    #[cfg(feature = "sync")]
118    use crate::api::Query;
119    use crate::test::client::FakeOpenStackClient;
120    use crate::types::ServiceType;
121    use http::{HeaderName, HeaderValue};
122    use httpmock::MockServer;
123    use serde_json::json;
124
125    #[test]
126    fn test_service_type() {
127        assert_eq!(
128            Request::builder().build().unwrap().service_type(),
129            ServiceType::Compute
130        );
131    }
132
133    #[test]
134    fn test_response_key() {
135        assert!(Request::builder().build().unwrap().response_key().is_none())
136    }
137
138    #[cfg(feature = "sync")]
139    #[test]
140    fn endpoint() {
141        let server = MockServer::start();
142        let client = FakeOpenStackClient::new(server.base_url());
143        let mock = server.mock(|when, then| {
144            when.method(httpmock::Method::DELETE)
145                .path(format!("/os-floating-ips/{id}", id = "id",));
146
147            then.status(200)
148                .header("content-type", "application/json")
149                .json_body(json!({ "dummy": {} }));
150        });
151
152        let endpoint = Request::builder().id("id").build().unwrap();
153        let _: serde_json::Value = endpoint.query(&client).unwrap();
154        mock.assert();
155    }
156
157    #[cfg(feature = "sync")]
158    #[test]
159    fn endpoint_headers() {
160        let server = MockServer::start();
161        let client = FakeOpenStackClient::new(server.base_url());
162        let mock = server.mock(|when, then| {
163            when.method(httpmock::Method::DELETE)
164                .path(format!("/os-floating-ips/{id}", id = "id",))
165                .header("foo", "bar")
166                .header("not_foo", "not_bar");
167            then.status(200)
168                .header("content-type", "application/json")
169                .json_body(json!({ "dummy": {} }));
170        });
171
172        let endpoint = Request::builder()
173            .id("id")
174            .headers(
175                [(
176                    Some(HeaderName::from_static("foo")),
177                    HeaderValue::from_static("bar"),
178                )]
179                .into_iter(),
180            )
181            .header("not_foo", "not_bar")
182            .build()
183            .unwrap();
184        let _: serde_json::Value = endpoint.query(&client).unwrap();
185        mock.assert();
186    }
187}