openstack_sdk/api/dns/v2/reverse/floatingip/
set.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//! Set a PTR record for the given FloatingIP. The domain if it does not exist
19//! will be provisioned automatically.
20//!
21use derive_builder::Builder;
22use http::{HeaderMap, HeaderName, HeaderValue};
23
24use crate::api::rest_endpoint_prelude::*;
25
26use std::borrow::Cow;
27
28#[derive(Builder, Debug, Clone)]
29#[builder(setter(strip_option))]
30pub struct Request<'a> {
31    /// The floatingip address for this PTR record.
32    #[builder(default, setter(into))]
33    pub(crate) address: Option<Cow<'a, str>>,
34
35    /// Description for this PTR record
36    #[builder(default, setter(into))]
37    pub(crate) description: Option<Cow<'a, str>>,
38
39    /// Domain name for this PTR record
40    #[builder(default, setter(into))]
41    pub(crate) ptrdname: Option<Option<Cow<'a, str>>>,
42
43    /// Time to live for this PTR record
44    #[builder(default, setter(into))]
45    pub(crate) ttl: Option<i32>,
46
47    /// fip_key parameter for /v2/reverse/floatingips/{fip_key} API
48    #[builder(default, setter(into))]
49    fip_key: Cow<'a, str>,
50
51    #[builder(setter(name = "_headers"), default, private)]
52    _headers: Option<HeaderMap>,
53}
54impl<'a> Request<'a> {
55    /// Create a builder for the endpoint.
56    pub fn builder() -> RequestBuilder<'a> {
57        RequestBuilder::default()
58    }
59}
60
61impl<'a> RequestBuilder<'a> {
62    /// Add a single header to the Floatingip.
63    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
64    where
65        K: Into<HeaderName>,
66        V: Into<HeaderValue>,
67    {
68        self._headers
69            .get_or_insert(None)
70            .get_or_insert_with(HeaderMap::new)
71            .insert(header_name.into(), header_value.into());
72        self
73    }
74
75    /// Add multiple headers.
76    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
77    where
78        I: Iterator<Item = T>,
79        T: Into<(Option<HeaderName>, HeaderValue)>,
80    {
81        self._headers
82            .get_or_insert(None)
83            .get_or_insert_with(HeaderMap::new)
84            .extend(iter.map(Into::into));
85        self
86    }
87}
88
89impl RestEndpoint for Request<'_> {
90    fn method(&self) -> http::Method {
91        http::Method::PATCH
92    }
93
94    fn endpoint(&self) -> Cow<'static, str> {
95        format!(
96            "reverse/floatingips/{fip_key}",
97            fip_key = self.fip_key.as_ref(),
98        )
99        .into()
100    }
101
102    fn parameters(&self) -> QueryParams<'_> {
103        QueryParams::default()
104    }
105
106    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
107        let mut params = JsonBodyParams::default();
108
109        if let Some(val) = &self.address {
110            params.push("address", serde_json::to_value(val)?);
111        }
112        if let Some(val) = &self.description {
113            params.push("description", serde_json::to_value(val)?);
114        }
115        if let Some(val) = &self.ptrdname {
116            params.push("ptrdname", serde_json::to_value(val)?);
117        }
118        if let Some(val) = &self.ttl {
119            params.push("ttl", serde_json::to_value(val)?);
120        }
121
122        params.into_body()
123    }
124
125    fn service_type(&self) -> ServiceType {
126        ServiceType::Dns
127    }
128
129    fn response_key(&self) -> Option<Cow<'static, str>> {
130        None
131    }
132
133    /// Returns headers to be set into the request
134    fn request_headers(&self) -> Option<&HeaderMap> {
135        self._headers.as_ref()
136    }
137
138    /// Returns required API version
139    fn api_version(&self) -> Option<ApiVersion> {
140        Some(ApiVersion::new(2, 0))
141    }
142}
143
144#[cfg(test)]
145mod tests {
146    use super::*;
147    #[cfg(feature = "sync")]
148    use crate::api::Query;
149    use crate::test::client::FakeOpenStackClient;
150    use crate::types::ServiceType;
151    use http::{HeaderName, HeaderValue};
152    use httpmock::MockServer;
153    use serde_json::json;
154
155    #[test]
156    fn test_service_type() {
157        assert_eq!(
158            Request::builder().build().unwrap().service_type(),
159            ServiceType::Dns
160        );
161    }
162
163    #[test]
164    fn test_response_key() {
165        assert!(Request::builder().build().unwrap().response_key().is_none())
166    }
167
168    #[cfg(feature = "sync")]
169    #[test]
170    fn endpoint() {
171        let server = MockServer::start();
172        let client = FakeOpenStackClient::new(server.base_url());
173        let mock = server.mock(|when, then| {
174            when.method(httpmock::Method::PATCH).path(format!(
175                "/reverse/floatingips/{fip_key}",
176                fip_key = "fip_key",
177            ));
178
179            then.status(200)
180                .header("content-type", "application/json")
181                .json_body(json!({ "dummy": {} }));
182        });
183
184        let endpoint = Request::builder().fip_key("fip_key").build().unwrap();
185        let _: serde_json::Value = endpoint.query(&client).unwrap();
186        mock.assert();
187    }
188
189    #[cfg(feature = "sync")]
190    #[test]
191    fn endpoint_headers() {
192        let server = MockServer::start();
193        let client = FakeOpenStackClient::new(server.base_url());
194        let mock = server.mock(|when, then| {
195            when.method(httpmock::Method::PATCH)
196                .path(format!(
197                    "/reverse/floatingips/{fip_key}",
198                    fip_key = "fip_key",
199                ))
200                .header("foo", "bar")
201                .header("not_foo", "not_bar");
202            then.status(200)
203                .header("content-type", "application/json")
204                .json_body(json!({ "dummy": {} }));
205        });
206
207        let endpoint = Request::builder()
208            .fip_key("fip_key")
209            .headers(
210                [(
211                    Some(HeaderName::from_static("foo")),
212                    HeaderValue::from_static("bar"),
213                )]
214                .into_iter(),
215            )
216            .header(
217                HeaderName::from_static("not_foo"),
218                HeaderValue::from_static("not_bar"),
219            )
220            .build()
221            .unwrap();
222        let _: serde_json::Value = endpoint.query(&client).unwrap();
223        mock.assert();
224    }
225}