Skip to main content

openstack_sdk_load_balancer/v2/loadbalancer/
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//! Shows the current statistics for a load balancer.
19//!
20//! This operation returns the statistics of a load balancer object identified
21//! by loadbalancer_id.
22//!
23//! If you are not an administrative user and the load balancer object does not
24//! belong to your project, the service returns the HTTP `Forbidden (403)`
25//! response code.
26//!
27//! This operation does not require a request body.
28//!
29use derive_builder::Builder;
30use http::{HeaderMap, HeaderName, HeaderValue};
31
32use openstack_sdk_core::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    /// loadbalancer_id parameter for
40    /// /v2/lbaas/loadbalancers/{loadbalancer_id}/stats 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 Loadbalancer.
56    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
57    where
58        K: Into<HeaderName>,
59        V: Into<HeaderValue>,
60    {
61        self._headers
62            .get_or_insert(None)
63            .get_or_insert_with(HeaderMap::new)
64            .insert(header_name.into(), header_value.into());
65        self
66    }
67
68    /// Add multiple headers.
69    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
70    where
71        I: Iterator<Item = T>,
72        T: Into<(Option<HeaderName>, HeaderValue)>,
73    {
74        self._headers
75            .get_or_insert(None)
76            .get_or_insert_with(HeaderMap::new)
77            .extend(iter.map(Into::into));
78        self
79    }
80}
81
82impl RestEndpoint for Request<'_> {
83    fn method(&self) -> http::Method {
84        http::Method::GET
85    }
86
87    fn endpoint(&self) -> Cow<'static, str> {
88        format!("lbaas/loadbalancers/{id}/stats", id = self.id.as_ref(),).into()
89    }
90
91    fn parameters(&self) -> QueryParams<'_> {
92        QueryParams::default()
93    }
94
95    fn service_type(&self) -> ServiceType {
96        ServiceType::LoadBalancer
97    }
98
99    fn response_key(&self) -> Option<Cow<'static, str>> {
100        Some("stats".into())
101    }
102
103    /// Returns headers to be set into the request
104    fn request_headers(&self) -> Option<&HeaderMap> {
105        self._headers.as_ref()
106    }
107
108    /// Returns required API version
109    fn api_version(&self) -> Option<ApiVersion> {
110        Some(ApiVersion::new(2, 0))
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117    use http::{HeaderName, HeaderValue};
118    use httpmock::MockServer;
119    #[cfg(feature = "sync")]
120    use openstack_sdk_core::api::Query;
121    use openstack_sdk_core::test::client::FakeOpenStackClient;
122    use openstack_sdk_core::types::ServiceType;
123    use serde_json::json;
124
125    #[test]
126    fn test_service_type() {
127        assert_eq!(
128            Request::builder().build().unwrap().service_type(),
129            ServiceType::LoadBalancer
130        );
131    }
132
133    #[test]
134    fn test_response_key() {
135        assert_eq!(
136            Request::builder().build().unwrap().response_key().unwrap(),
137            "stats"
138        );
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::GET)
148                .path(format!("/lbaas/loadbalancers/{id}/stats", id = "id",));
149
150            then.status(200)
151                .header("content-type", "application/json")
152                .json_body(json!({ "stats": {} }));
153        });
154
155        let endpoint = Request::builder().id("id").build().unwrap();
156        let _: serde_json::Value = endpoint.query(&client).unwrap();
157        mock.assert();
158    }
159
160    #[cfg(feature = "sync")]
161    #[test]
162    fn endpoint_headers() {
163        let server = MockServer::start();
164        let client = FakeOpenStackClient::new(server.base_url());
165        let mock = server.mock(|when, then| {
166            when.method(httpmock::Method::GET)
167                .path(format!("/lbaas/loadbalancers/{id}/stats", id = "id",))
168                .header("foo", "bar")
169                .header("not_foo", "not_bar");
170            then.status(200)
171                .header("content-type", "application/json")
172                .json_body(json!({ "stats": {} }));
173        });
174
175        let endpoint = Request::builder()
176            .id("id")
177            .headers(
178                [(
179                    Some(HeaderName::from_static("foo")),
180                    HeaderValue::from_static("bar"),
181                )]
182                .into_iter(),
183            )
184            .header(
185                HeaderName::from_static("not_foo"),
186                HeaderValue::from_static("not_bar"),
187            )
188            .build()
189            .unwrap();
190        let _: serde_json::Value = endpoint.query(&client).unwrap();
191        mock.assert();
192    }
193}