openstack_sdk/api/network/v2/router/
remove_router_interface.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 an internal interface from a logical router.
19//!
20//! This operation deletes an internal router interface, which detaches a
21//! subnet from the router. If this subnet ID is the last subnet on the port,
22//! this operation deletes the port itself. You must specify either a subnet ID
23//! or port ID in the request body; the operation uses this value to identify
24//! which router interface to deletes.
25//!
26//! You can also specify both a subnet ID and port ID. If you specify both IDs,
27//! the subnet ID must correspond to the subnet ID of the first IP address on
28//! the port. Otherwise, this operation returns the `Conflict (409)` response
29//! code with information about the affected router and interface.
30//!
31//! If you try to delete the router interface for subnets that are used by one
32//! or more `routes`, this operation returns the `Conflict (409)` response. In
33//! this case, you first need to delete such routes from the router.
34//!
35//! If the router or the subnet and port do not exist or are not visible to
36//! you, this operation returns the `Not Found (404)` response code. As a
37//! consequence of this operation, the operation removes the port connecting
38//! the router with the subnet from the subnet for the network.
39//!
40//! Normal response codes: 200
41//!
42//! Error response codes: 400, 401, 404, 409
43//!
44use derive_builder::Builder;
45use http::{HeaderMap, HeaderName, HeaderValue};
46
47use crate::api::rest_endpoint_prelude::*;
48
49use std::borrow::Cow;
50
51#[derive(Builder, Debug, Clone)]
52#[builder(setter(strip_option))]
53pub struct Request<'a> {
54    /// The ID of the port. One of subnet_id or port_id must be specified.
55    #[builder(default, setter(into))]
56    pub(crate) port_id: Option<Cow<'a, str>>,
57
58    /// The ID of the subnet. One of subnet_id or port_id must be specified.
59    #[builder(default, setter(into))]
60    pub(crate) subnet_id: Option<Cow<'a, str>>,
61
62    /// id parameter for /v2.0/routers/{id} API
63    #[builder(default, setter(into))]
64    id: Cow<'a, str>,
65
66    #[builder(setter(name = "_headers"), default, private)]
67    _headers: Option<HeaderMap>,
68}
69impl<'a> Request<'a> {
70    /// Create a builder for the endpoint.
71    pub fn builder() -> RequestBuilder<'a> {
72        RequestBuilder::default()
73    }
74}
75
76impl RequestBuilder<'_> {
77    /// Add a single header to the Router.
78    pub fn header(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
79where {
80        self._headers
81            .get_or_insert(None)
82            .get_or_insert_with(HeaderMap::new)
83            .insert(header_name, HeaderValue::from_static(header_value));
84        self
85    }
86
87    /// Add multiple headers.
88    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
89    where
90        I: Iterator<Item = T>,
91        T: Into<(Option<HeaderName>, HeaderValue)>,
92    {
93        self._headers
94            .get_or_insert(None)
95            .get_or_insert_with(HeaderMap::new)
96            .extend(iter.map(Into::into));
97        self
98    }
99}
100
101impl RestEndpoint for Request<'_> {
102    fn method(&self) -> http::Method {
103        http::Method::PUT
104    }
105
106    fn endpoint(&self) -> Cow<'static, str> {
107        format!(
108            "routers/{id}/remove_router_interface",
109            id = self.id.as_ref(),
110        )
111        .into()
112    }
113
114    fn parameters(&self) -> QueryParams {
115        QueryParams::default()
116    }
117
118    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
119        let mut params = JsonBodyParams::default();
120
121        if let Some(val) = &self.subnet_id {
122            params.push("subnet_id", serde_json::to_value(val)?);
123        }
124        if let Some(val) = &self.port_id {
125            params.push("port_id", serde_json::to_value(val)?);
126        }
127
128        params.into_body()
129    }
130
131    fn service_type(&self) -> ServiceType {
132        ServiceType::Network
133    }
134
135    fn response_key(&self) -> Option<Cow<'static, str>> {
136        None
137    }
138
139    /// Returns headers to be set into the request
140    fn request_headers(&self) -> Option<&HeaderMap> {
141        self._headers.as_ref()
142    }
143
144    /// Returns required API version
145    fn api_version(&self) -> Option<ApiVersion> {
146        Some(ApiVersion::new(2, 0))
147    }
148}
149
150#[cfg(test)]
151mod tests {
152    use super::*;
153    #[cfg(feature = "sync")]
154    use crate::api::Query;
155    use crate::test::client::FakeOpenStackClient;
156    use crate::types::ServiceType;
157    use http::{HeaderName, HeaderValue};
158    use httpmock::MockServer;
159    use serde_json::json;
160
161    #[test]
162    fn test_service_type() {
163        assert_eq!(
164            Request::builder().build().unwrap().service_type(),
165            ServiceType::Network
166        );
167    }
168
169    #[test]
170    fn test_response_key() {
171        assert!(Request::builder().build().unwrap().response_key().is_none())
172    }
173
174    #[cfg(feature = "sync")]
175    #[test]
176    fn endpoint() {
177        let server = MockServer::start();
178        let client = FakeOpenStackClient::new(server.base_url());
179        let mock = server.mock(|when, then| {
180            when.method(httpmock::Method::PUT)
181                .path(format!("/routers/{id}/remove_router_interface", id = "id",));
182
183            then.status(200)
184                .header("content-type", "application/json")
185                .json_body(json!({ "dummy": {} }));
186        });
187
188        let endpoint = Request::builder().id("id").build().unwrap();
189        let _: serde_json::Value = endpoint.query(&client).unwrap();
190        mock.assert();
191    }
192
193    #[cfg(feature = "sync")]
194    #[test]
195    fn endpoint_headers() {
196        let server = MockServer::start();
197        let client = FakeOpenStackClient::new(server.base_url());
198        let mock = server.mock(|when, then| {
199            when.method(httpmock::Method::PUT)
200                .path(format!("/routers/{id}/remove_router_interface", id = "id",))
201                .header("foo", "bar")
202                .header("not_foo", "not_bar");
203            then.status(200)
204                .header("content-type", "application/json")
205                .json_body(json!({ "dummy": {} }));
206        });
207
208        let endpoint = Request::builder()
209            .id("id")
210            .headers(
211                [(
212                    Some(HeaderName::from_static("foo")),
213                    HeaderValue::from_static("bar"),
214                )]
215                .into_iter(),
216            )
217            .header("not_foo", "not_bar")
218            .build()
219            .unwrap();
220        let _: serde_json::Value = endpoint.query(&client).unwrap();
221        mock.assert();
222    }
223}