openstack_sdk/api/compute/v2/floating_ip/
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 address, by ID, that is associated with the
19//! tenant or account.
20//!
21//! Policy defaults enable only users with the administrative role or the owner
22//! of the server to perform this operation. Cloud providers can change these
23//! permissions through the `policy.json` file.
24//!
25//! Normal response codes: 200
26//!
27//! Error response codes: badRequest(400), unauthorized(401), forbidden(403),
28//! itemNotFound(404)
29//!
30use derive_builder::Builder;
31use http::{HeaderMap, HeaderName, HeaderValue};
32
33use crate::api::rest_endpoint_prelude::*;
34
35use std::borrow::Cow;
36
37#[derive(Builder, Debug, Clone)]
38#[builder(setter(strip_option))]
39pub struct Request<'a> {
40    /// id parameter for /v2.1/os-floating-ips/{id} API
41    #[builder(default, setter(into))]
42    id: Cow<'a, str>,
43
44    #[builder(setter(name = "_headers"), default, private)]
45    _headers: Option<HeaderMap>,
46}
47impl<'a> Request<'a> {
48    /// Create a builder for the endpoint.
49    pub fn builder() -> RequestBuilder<'a> {
50        RequestBuilder::default()
51    }
52}
53
54impl<'a> RequestBuilder<'a> {
55    /// Add a single header to the Floating_Ip.
56    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
57    where
58        K: Into<HeaderName>,
59        V: Into<HeaderValue>,
60    {
61        self._headers
62            .get_or_insert(None)
63            .get_or_insert_with(HeaderMap::new)
64            .insert(header_name.into(), header_value.into());
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::GET
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::GET)
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::GET)
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(
182                HeaderName::from_static("not_foo"),
183                HeaderValue::from_static("not_bar"),
184            )
185            .build()
186            .unwrap();
187        let _: serde_json::Value = endpoint.query(&client).unwrap();
188        mock.assert();
189    }
190}