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