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