Skip to main content

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