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