openstack_sdk_load_balancer/v2/l7policy/
delete.rs1use derive_builder::Builder;
24use http::{HeaderMap, HeaderName, HeaderValue};
25
26use openstack_sdk_core::api::rest_endpoint_prelude::*;
27
28use std::borrow::Cow;
29
30#[derive(Builder, Debug, Clone)]
31#[builder(setter(strip_option))]
32pub struct Request<'a> {
33 #[builder(default, setter(into))]
35 id: Cow<'a, str>,
36
37 #[builder(setter(name = "_headers"), default, private)]
38 _headers: Option<HeaderMap>,
39}
40impl<'a> Request<'a> {
41 pub fn builder() -> RequestBuilder<'a> {
43 RequestBuilder::default()
44 }
45}
46
47impl<'a> RequestBuilder<'a> {
48 pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
50 where
51 K: Into<HeaderName>,
52 V: Into<HeaderValue>,
53 {
54 self._headers
55 .get_or_insert(None)
56 .get_or_insert_with(HeaderMap::new)
57 .insert(header_name.into(), header_value.into());
58 self
59 }
60
61 pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
63 where
64 I: Iterator<Item = T>,
65 T: Into<(Option<HeaderName>, HeaderValue)>,
66 {
67 self._headers
68 .get_or_insert(None)
69 .get_or_insert_with(HeaderMap::new)
70 .extend(iter.map(Into::into));
71 self
72 }
73}
74
75impl RestEndpoint for Request<'_> {
76 fn method(&self) -> http::Method {
77 http::Method::DELETE
78 }
79
80 fn endpoint(&self) -> Cow<'static, str> {
81 format!("lbaas/l7policies/{id}", id = self.id.as_ref(),).into()
82 }
83
84 fn parameters(&self) -> QueryParams<'_> {
85 QueryParams::default()
86 }
87
88 fn service_type(&self) -> ServiceType {
89 ServiceType::LoadBalancer
90 }
91
92 fn response_key(&self) -> Option<Cow<'static, str>> {
93 None
94 }
95
96 fn request_headers(&self) -> Option<&HeaderMap> {
98 self._headers.as_ref()
99 }
100
101 fn api_version(&self) -> Option<ApiVersion> {
103 Some(ApiVersion::new(2, 0))
104 }
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110 use http::{HeaderName, HeaderValue};
111 use httpmock::MockServer;
112 #[cfg(feature = "sync")]
113 use openstack_sdk_core::api::Query;
114 use openstack_sdk_core::test::client::FakeOpenStackClient;
115 use openstack_sdk_core::types::ServiceType;
116 use serde_json::json;
117
118 #[test]
119 fn test_service_type() {
120 assert_eq!(
121 Request::builder().build().unwrap().service_type(),
122 ServiceType::LoadBalancer
123 );
124 }
125
126 #[test]
127 fn test_response_key() {
128 assert!(Request::builder().build().unwrap().response_key().is_none())
129 }
130
131 #[cfg(feature = "sync")]
132 #[test]
133 fn endpoint() {
134 let server = MockServer::start();
135 let client = FakeOpenStackClient::new(server.base_url());
136 let mock = server.mock(|when, then| {
137 when.method(httpmock::Method::DELETE)
138 .path(format!("/lbaas/l7policies/{id}", id = "id",));
139
140 then.status(200)
141 .header("content-type", "application/json")
142 .json_body(json!({ "dummy": {} }));
143 });
144
145 let endpoint = Request::builder().id("id").build().unwrap();
146 let _: serde_json::Value = endpoint.query(&client).unwrap();
147 mock.assert();
148 }
149
150 #[cfg(feature = "sync")]
151 #[test]
152 fn endpoint_headers() {
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::DELETE)
157 .path(format!("/lbaas/l7policies/{id}", id = "id",))
158 .header("foo", "bar")
159 .header("not_foo", "not_bar");
160 then.status(200)
161 .header("content-type", "application/json")
162 .json_body(json!({ "dummy": {} }));
163 });
164
165 let endpoint = Request::builder()
166 .id("id")
167 .headers(
168 [(
169 Some(HeaderName::from_static("foo")),
170 HeaderValue::from_static("bar"),
171 )]
172 .into_iter(),
173 )
174 .header(
175 HeaderName::from_static("not_foo"),
176 HeaderValue::from_static("not_bar"),
177 )
178 .build()
179 .unwrap();
180 let _: serde_json::Value = endpoint.query(&client).unwrap();
181 mock.assert();
182 }
183}