openstack_sdk/api/network/v2/agent/
list.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//! Lists all agents.
19//!
20//! Standard query parameters are supported on the URI. For more information,
21//! see [Filtering and Column Selection](#filtering).
22//!
23//! Use the `fields` query parameter to control which fields are returned in
24//! the response body. For more information, see [Fields](#fields).
25//!
26//! Pagination query parameters are supported if Neutron configuration supports
27//! it by overriding `allow_pagination=false`. For more information, see
28//! [Pagination](#pagination).
29//!
30//! Sorting query parameters are supported if Neutron configuration supports it
31//! with `allow_sorting=true`. For more information, see [Sorting](#sorting).
32//!
33//! Normal response codes: 200
34//!
35//! Error response codes: 401
36//!
37use derive_builder::Builder;
38use http::{HeaderMap, HeaderName, HeaderValue};
39
40use crate::api::rest_endpoint_prelude::*;
41
42use std::borrow::Cow;
43
44use crate::api::Pageable;
45#[derive(Builder, Debug, Clone)]
46#[builder(setter(strip_option))]
47pub struct Request<'a> {
48    /// admin_state_up query parameter for /v2.0/agents API
49    #[builder(default)]
50    admin_state_up: Option<bool>,
51
52    /// agent_type query parameter for /v2.0/agents API
53    #[builder(default, setter(into))]
54    agent_type: Option<Cow<'a, str>>,
55
56    /// alive query parameter for /v2.0/agents API
57    #[builder(default, setter(into))]
58    alive: Option<Cow<'a, str>>,
59
60    /// availability_zone query parameter for /v2.0/agents API
61    #[builder(default, setter(into))]
62    availability_zone: Option<Cow<'a, str>>,
63
64    /// binary query parameter for /v2.0/agents API
65    #[builder(default, setter(into))]
66    binary: Option<Cow<'a, str>>,
67
68    /// description query parameter for /v2.0/agents API
69    #[builder(default, setter(into))]
70    description: Option<Cow<'a, str>>,
71
72    /// host query parameter for /v2.0/agents API
73    #[builder(default, setter(into))]
74    host: Option<Cow<'a, str>>,
75
76    /// id query parameter for /v2.0/agents API
77    #[builder(default, setter(into))]
78    id: Option<Cow<'a, str>>,
79
80    /// Requests a page size of items. Returns a number of items up to a limit
81    /// value. Use the limit parameter to make an initial limited request and
82    /// use the ID of the last-seen item from the response as the marker
83    /// parameter value in a subsequent limited request.
84    #[builder(default)]
85    limit: Option<u32>,
86
87    /// The ID of the last-seen item. Use the limit parameter to make an
88    /// initial limited request and use the ID of the last-seen item from the
89    /// response as the marker parameter value in a subsequent limited request.
90    #[builder(default, setter(into))]
91    marker: Option<Cow<'a, str>>,
92
93    /// Reverse the page direction
94    #[builder(default)]
95    page_reverse: Option<bool>,
96
97    /// Sort direction. This is an optional feature and may be silently ignored
98    /// by the server.
99    #[builder(default, private, setter(name = "_sort_dir"))]
100    sort_dir: Option<Vec<Cow<'a, str>>>,
101
102    /// Sort results by the attribute. This is an optional feature and may be
103    /// silently ignored by the server.
104    #[builder(default, private, setter(name = "_sort_key"))]
105    sort_key: Option<Vec<Cow<'a, str>>>,
106
107    /// topic query parameter for /v2.0/agents API
108    #[builder(default, setter(into))]
109    topic: Option<Cow<'a, str>>,
110
111    #[builder(setter(name = "_headers"), default, private)]
112    _headers: Option<HeaderMap>,
113}
114impl<'a> Request<'a> {
115    /// Create a builder for the endpoint.
116    pub fn builder() -> RequestBuilder<'a> {
117        RequestBuilder::default()
118    }
119}
120
121impl<'a> RequestBuilder<'a> {
122    /// Sort direction. This is an optional feature and may be silently ignored
123    /// by the server.
124    pub fn sort_dir<I, T>(&mut self, iter: I) -> &mut Self
125    where
126        I: Iterator<Item = T>,
127        T: Into<Cow<'a, str>>,
128    {
129        self.sort_dir
130            .get_or_insert(None)
131            .get_or_insert_with(Vec::new)
132            .extend(iter.map(Into::into));
133        self
134    }
135
136    /// Sort results by the attribute. This is an optional feature and may be
137    /// silently ignored by the server.
138    pub fn sort_key<I, T>(&mut self, iter: I) -> &mut Self
139    where
140        I: Iterator<Item = T>,
141        T: Into<Cow<'a, str>>,
142    {
143        self.sort_key
144            .get_or_insert(None)
145            .get_or_insert_with(Vec::new)
146            .extend(iter.map(Into::into));
147        self
148    }
149
150    /// Add a single header to the Agent.
151    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
152    where
153        K: Into<HeaderName>,
154        V: Into<HeaderValue>,
155    {
156        self._headers
157            .get_or_insert(None)
158            .get_or_insert_with(HeaderMap::new)
159            .insert(header_name.into(), header_value.into());
160        self
161    }
162
163    /// Add multiple headers.
164    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
165    where
166        I: Iterator<Item = T>,
167        T: Into<(Option<HeaderName>, HeaderValue)>,
168    {
169        self._headers
170            .get_or_insert(None)
171            .get_or_insert_with(HeaderMap::new)
172            .extend(iter.map(Into::into));
173        self
174    }
175}
176
177impl RestEndpoint for Request<'_> {
178    fn method(&self) -> http::Method {
179        http::Method::GET
180    }
181
182    fn endpoint(&self) -> Cow<'static, str> {
183        "agents".to_string().into()
184    }
185
186    fn parameters(&self) -> QueryParams<'_> {
187        let mut params = QueryParams::default();
188        params.push_opt("admin_state_up", self.admin_state_up);
189        params.push_opt("agent_type", self.agent_type.as_ref());
190        params.push_opt("alive", self.alive.as_ref());
191        params.push_opt("availability_zone", self.availability_zone.as_ref());
192        params.push_opt("binary", self.binary.as_ref());
193        params.push_opt("description", self.description.as_ref());
194        params.push_opt("host", self.host.as_ref());
195        params.push_opt("id", self.id.as_ref());
196        params.push_opt("topic", self.topic.as_ref());
197        params.push_opt("limit", self.limit);
198        params.push_opt("marker", self.marker.as_ref());
199        params.push_opt("page_reverse", self.page_reverse);
200        if let Some(val) = &self.sort_dir {
201            params.extend(val.iter().map(|value| ("sort_dir", value)));
202        }
203        if let Some(val) = &self.sort_key {
204            params.extend(val.iter().map(|value| ("sort_key", value)));
205        }
206
207        params
208    }
209
210    fn service_type(&self) -> ServiceType {
211        ServiceType::Network
212    }
213
214    fn response_key(&self) -> Option<Cow<'static, str>> {
215        Some("agents".into())
216    }
217
218    /// Returns headers to be set into the request
219    fn request_headers(&self) -> Option<&HeaderMap> {
220        self._headers.as_ref()
221    }
222
223    /// Returns required API version
224    fn api_version(&self) -> Option<ApiVersion> {
225        Some(ApiVersion::new(2, 0))
226    }
227}
228impl Pageable for Request<'_> {}
229
230#[cfg(test)]
231mod tests {
232    use super::*;
233    #[cfg(feature = "sync")]
234    use crate::api::Query;
235    use crate::test::client::FakeOpenStackClient;
236    use crate::types::ServiceType;
237    use http::{HeaderName, HeaderValue};
238    use httpmock::MockServer;
239    use serde_json::json;
240
241    #[test]
242    fn test_service_type() {
243        assert_eq!(
244            Request::builder().build().unwrap().service_type(),
245            ServiceType::Network
246        );
247    }
248
249    #[test]
250    fn test_response_key() {
251        assert_eq!(
252            Request::builder().build().unwrap().response_key().unwrap(),
253            "agents"
254        );
255    }
256
257    #[cfg(feature = "sync")]
258    #[test]
259    fn endpoint() {
260        let server = MockServer::start();
261        let client = FakeOpenStackClient::new(server.base_url());
262        let mock = server.mock(|when, then| {
263            when.method(httpmock::Method::GET)
264                .path("/agents".to_string());
265
266            then.status(200)
267                .header("content-type", "application/json")
268                .json_body(json!({ "agents": {} }));
269        });
270
271        let endpoint = Request::builder().build().unwrap();
272        let _: serde_json::Value = endpoint.query(&client).unwrap();
273        mock.assert();
274    }
275
276    #[cfg(feature = "sync")]
277    #[test]
278    fn endpoint_headers() {
279        let server = MockServer::start();
280        let client = FakeOpenStackClient::new(server.base_url());
281        let mock = server.mock(|when, then| {
282            when.method(httpmock::Method::GET)
283                .path("/agents".to_string())
284                .header("foo", "bar")
285                .header("not_foo", "not_bar");
286            then.status(200)
287                .header("content-type", "application/json")
288                .json_body(json!({ "agents": {} }));
289        });
290
291        let endpoint = Request::builder()
292            .headers(
293                [(
294                    Some(HeaderName::from_static("foo")),
295                    HeaderValue::from_static("bar"),
296                )]
297                .into_iter(),
298            )
299            .header(
300                HeaderName::from_static("not_foo"),
301                HeaderValue::from_static("not_bar"),
302            )
303            .build()
304            .unwrap();
305        let _: serde_json::Value = endpoint.query(&client).unwrap();
306        mock.assert();
307    }
308}