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