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