openstack_sdk/api/network/v2/floatingip/
get.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//! Shows details for a floating IP.
19//!
20//! Use the `fields` query parameter to control which fields are returned in
21//! the response body. For more information, see [Fields](#fields).
22//!
23//! This example request shows details for a floating IP in JSON format. This
24//! example also filters the result by the `fixed_ip_address` and
25//! `floating_ip_address` fields.
26//!
27//! Normal response codes: 200
28//!
29//! Error response codes: 401, 403, 404
30//!
31use derive_builder::Builder;
32use http::{HeaderMap, HeaderName, HeaderValue};
33
34use crate::api::rest_endpoint_prelude::*;
35
36use std::borrow::Cow;
37
38#[derive(Builder, Debug, Clone)]
39#[builder(setter(strip_option))]
40pub struct Request<'a> {
41    /// id parameter for /v2.0/floatingips/{id} 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 RequestBuilder<'_> {
56    /// Add a single header to the Floatingip.
57    pub fn header(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
58where {
59        self._headers
60            .get_or_insert(None)
61            .get_or_insert_with(HeaderMap::new)
62            .insert(header_name, HeaderValue::from_static(header_value));
63        self
64    }
65
66    /// Add multiple headers.
67    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
68    where
69        I: Iterator<Item = T>,
70        T: Into<(Option<HeaderName>, HeaderValue)>,
71    {
72        self._headers
73            .get_or_insert(None)
74            .get_or_insert_with(HeaderMap::new)
75            .extend(iter.map(Into::into));
76        self
77    }
78}
79
80impl RestEndpoint for Request<'_> {
81    fn method(&self) -> http::Method {
82        http::Method::GET
83    }
84
85    fn endpoint(&self) -> Cow<'static, str> {
86        format!("floatingips/{id}", id = self.id.as_ref(),).into()
87    }
88
89    fn parameters(&self) -> QueryParams {
90        QueryParams::default()
91    }
92
93    fn service_type(&self) -> ServiceType {
94        ServiceType::Network
95    }
96
97    fn response_key(&self) -> Option<Cow<'static, str>> {
98        Some("floatingip".into())
99    }
100
101    /// Returns headers to be set into the request
102    fn request_headers(&self) -> Option<&HeaderMap> {
103        self._headers.as_ref()
104    }
105
106    /// Returns required API version
107    fn api_version(&self) -> Option<ApiVersion> {
108        Some(ApiVersion::new(2, 0))
109    }
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115    #[cfg(feature = "sync")]
116    use crate::api::Query;
117    use crate::test::client::FakeOpenStackClient;
118    use crate::types::ServiceType;
119    use http::{HeaderName, HeaderValue};
120    use httpmock::MockServer;
121    use serde_json::json;
122
123    #[test]
124    fn test_service_type() {
125        assert_eq!(
126            Request::builder().build().unwrap().service_type(),
127            ServiceType::Network
128        );
129    }
130
131    #[test]
132    fn test_response_key() {
133        assert_eq!(
134            Request::builder().build().unwrap().response_key().unwrap(),
135            "floatingip"
136        );
137    }
138
139    #[cfg(feature = "sync")]
140    #[test]
141    fn endpoint() {
142        let server = MockServer::start();
143        let client = FakeOpenStackClient::new(server.base_url());
144        let mock = server.mock(|when, then| {
145            when.method(httpmock::Method::GET)
146                .path(format!("/floatingips/{id}", id = "id",));
147
148            then.status(200)
149                .header("content-type", "application/json")
150                .json_body(json!({ "floatingip": {} }));
151        });
152
153        let endpoint = Request::builder().id("id").build().unwrap();
154        let _: serde_json::Value = endpoint.query(&client).unwrap();
155        mock.assert();
156    }
157
158    #[cfg(feature = "sync")]
159    #[test]
160    fn endpoint_headers() {
161        let server = MockServer::start();
162        let client = FakeOpenStackClient::new(server.base_url());
163        let mock = server.mock(|when, then| {
164            when.method(httpmock::Method::GET)
165                .path(format!("/floatingips/{id}", id = "id",))
166                .header("foo", "bar")
167                .header("not_foo", "not_bar");
168            then.status(200)
169                .header("content-type", "application/json")
170                .json_body(json!({ "floatingip": {} }));
171        });
172
173        let endpoint = Request::builder()
174            .id("id")
175            .headers(
176                [(
177                    Some(HeaderName::from_static("foo")),
178                    HeaderValue::from_static("bar"),
179                )]
180                .into_iter(),
181            )
182            .header("not_foo", "not_bar")
183            .build()
184            .unwrap();
185        let _: serde_json::Value = endpoint.query(&client).unwrap();
186        mock.assert();
187    }
188}