openstack_sdk/api/load_balancer/v2/loadbalancer/
status.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 status tree for a load balancer.
19//!
20//! This operation returns a status tree for a load balancer object, by load
21//! balancer ID.
22//!
23//! `provisioning_status` is the status associated with lifecycle of the
24//! resource. See [Provisioning Status Codes](#prov-status) for descriptions of
25//! the status codes.
26//!
27//! `operating_status` is the observed status of the resource. See
28//! [Operating Status Codes](#op-status) for descriptions of the status codes.
29//!
30//! If you are not an administrative user and the load balancer object does not
31//! belong to your project, the service returns the HTTP `Forbidden (403)`
32//! response code.
33//!
34//! If the operation succeeds, the returned element is a status tree that
35//! contains the load balancer and all provisioning and operating statuses for
36//! its children.
37//!
38use derive_builder::Builder;
39use http::{HeaderMap, HeaderName, HeaderValue};
40
41use crate::api::rest_endpoint_prelude::*;
42
43use std::borrow::Cow;
44
45#[derive(Builder, Debug, Clone)]
46#[builder(setter(strip_option))]
47pub struct Request<'a> {
48    /// loadbalancer_id parameter for
49    /// /v2/lbaas/loadbalancers/{loadbalancer_id}/status API
50    #[builder(default, setter(into))]
51    id: Cow<'a, str>,
52
53    #[builder(setter(name = "_headers"), default, private)]
54    _headers: Option<HeaderMap>,
55}
56impl<'a> Request<'a> {
57    /// Create a builder for the endpoint.
58    pub fn builder() -> RequestBuilder<'a> {
59        RequestBuilder::default()
60    }
61}
62
63impl<'a> RequestBuilder<'a> {
64    /// Add a single header to the Loadbalancer.
65    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
66    where
67        K: Into<HeaderName>,
68        V: Into<HeaderValue>,
69    {
70        self._headers
71            .get_or_insert(None)
72            .get_or_insert_with(HeaderMap::new)
73            .insert(header_name.into(), header_value.into());
74        self
75    }
76
77    /// Add multiple headers.
78    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
79    where
80        I: Iterator<Item = T>,
81        T: Into<(Option<HeaderName>, HeaderValue)>,
82    {
83        self._headers
84            .get_or_insert(None)
85            .get_or_insert_with(HeaderMap::new)
86            .extend(iter.map(Into::into));
87        self
88    }
89}
90
91impl RestEndpoint for Request<'_> {
92    fn method(&self) -> http::Method {
93        http::Method::GET
94    }
95
96    fn endpoint(&self) -> Cow<'static, str> {
97        format!("lbaas/loadbalancers/{id}/status", id = self.id.as_ref(),).into()
98    }
99
100    fn parameters(&self) -> QueryParams<'_> {
101        QueryParams::default()
102    }
103
104    fn service_type(&self) -> ServiceType {
105        ServiceType::LoadBalancer
106    }
107
108    fn response_key(&self) -> Option<Cow<'static, str>> {
109        Some("statuses".into())
110    }
111
112    /// Returns headers to be set into the request
113    fn request_headers(&self) -> Option<&HeaderMap> {
114        self._headers.as_ref()
115    }
116
117    /// Returns required API version
118    fn api_version(&self) -> Option<ApiVersion> {
119        Some(ApiVersion::new(2, 0))
120    }
121}
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126    #[cfg(feature = "sync")]
127    use crate::api::Query;
128    use crate::test::client::FakeOpenStackClient;
129    use crate::types::ServiceType;
130    use http::{HeaderName, HeaderValue};
131    use httpmock::MockServer;
132    use serde_json::json;
133
134    #[test]
135    fn test_service_type() {
136        assert_eq!(
137            Request::builder().build().unwrap().service_type(),
138            ServiceType::LoadBalancer
139        );
140    }
141
142    #[test]
143    fn test_response_key() {
144        assert_eq!(
145            Request::builder().build().unwrap().response_key().unwrap(),
146            "statuses"
147        );
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::GET)
157                .path(format!("/lbaas/loadbalancers/{id}/status", id = "id",));
158
159            then.status(200)
160                .header("content-type", "application/json")
161                .json_body(json!({ "statuses": {} }));
162        });
163
164        let endpoint = Request::builder().id("id").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::GET)
176                .path(format!("/lbaas/loadbalancers/{id}/status", id = "id",))
177                .header("foo", "bar")
178                .header("not_foo", "not_bar");
179            then.status(200)
180                .header("content-type", "application/json")
181                .json_body(json!({ "statuses": {} }));
182        });
183
184        let endpoint = Request::builder()
185            .id("id")
186            .headers(
187                [(
188                    Some(HeaderName::from_static("foo")),
189                    HeaderValue::from_static("bar"),
190                )]
191                .into_iter(),
192            )
193            .header(
194                HeaderName::from_static("not_foo"),
195                HeaderValue::from_static("not_bar"),
196            )
197            .build()
198            .unwrap();
199        let _: serde_json::Value = endpoint.query(&client).unwrap();
200        mock.assert();
201    }
202}