openstack_sdk/api/network/v2/port/tag/
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
18use derive_builder::Builder;
19use http::{HeaderMap, HeaderName, HeaderValue};
20
21use crate::api::rest_endpoint_prelude::*;
22
23use std::borrow::Cow;
24
25use crate::api::Pageable;
26#[derive(Builder, Debug, Clone)]
27#[builder(setter(strip_option))]
28pub struct Request<'a> {
29    /// Requests a page size of items. Returns a number of items up to a limit
30    /// value. Use the limit parameter to make an initial limited request and
31    /// use the ID of the last-seen item from the response as the marker
32    /// parameter value in a subsequent limited request.
33    #[builder(default)]
34    limit: Option<u32>,
35
36    /// The ID of the last-seen item. Use the limit parameter to make an
37    /// initial limited request and use the ID of the last-seen item from the
38    /// response as the marker parameter value in a subsequent limited request.
39    #[builder(default, setter(into))]
40    marker: Option<Cow<'a, str>>,
41
42    /// Reverse the page direction
43    #[builder(default)]
44    page_reverse: Option<bool>,
45
46    /// port_id parameter for /v2.0/ports/{port_id}/tags/{id} API
47    #[builder(default, setter(into))]
48    port_id: Cow<'a, str>,
49
50    /// Sort direction. This is an optional feature and may be silently ignored
51    /// by the server.
52    #[builder(default, private, setter(name = "_sort_dir"))]
53    sort_dir: Option<Vec<Cow<'a, str>>>,
54
55    /// Sort results by the attribute. This is an optional feature and may be
56    /// silently ignored by the server.
57    #[builder(default, private, setter(name = "_sort_key"))]
58    sort_key: Option<Vec<Cow<'a, str>>>,
59
60    #[builder(setter(name = "_headers"), default, private)]
61    _headers: Option<HeaderMap>,
62}
63impl<'a> Request<'a> {
64    /// Create a builder for the endpoint.
65    pub fn builder() -> RequestBuilder<'a> {
66        RequestBuilder::default()
67    }
68}
69
70impl<'a> RequestBuilder<'a> {
71    /// Sort direction. This is an optional feature and may be silently ignored
72    /// by the server.
73    pub fn sort_dir<I, T>(&mut self, iter: I) -> &mut Self
74    where
75        I: Iterator<Item = T>,
76        T: Into<Cow<'a, str>>,
77    {
78        self.sort_dir
79            .get_or_insert(None)
80            .get_or_insert_with(Vec::new)
81            .extend(iter.map(Into::into));
82        self
83    }
84
85    /// Sort results by the attribute. This is an optional feature and may be
86    /// silently ignored by the server.
87    pub fn sort_key<I, T>(&mut self, iter: I) -> &mut Self
88    where
89        I: Iterator<Item = T>,
90        T: Into<Cow<'a, str>>,
91    {
92        self.sort_key
93            .get_or_insert(None)
94            .get_or_insert_with(Vec::new)
95            .extend(iter.map(Into::into));
96        self
97    }
98
99    /// Add a single header to the Tag.
100    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
101    where
102        K: Into<HeaderName>,
103        V: Into<HeaderValue>,
104    {
105        self._headers
106            .get_or_insert(None)
107            .get_or_insert_with(HeaderMap::new)
108            .insert(header_name.into(), header_value.into());
109        self
110    }
111
112    /// Add multiple headers.
113    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
114    where
115        I: Iterator<Item = T>,
116        T: Into<(Option<HeaderName>, HeaderValue)>,
117    {
118        self._headers
119            .get_or_insert(None)
120            .get_or_insert_with(HeaderMap::new)
121            .extend(iter.map(Into::into));
122        self
123    }
124}
125
126impl RestEndpoint for Request<'_> {
127    fn method(&self) -> http::Method {
128        http::Method::GET
129    }
130
131    fn endpoint(&self) -> Cow<'static, str> {
132        format!("ports/{port_id}/tags", port_id = self.port_id.as_ref(),).into()
133    }
134
135    fn parameters(&self) -> QueryParams<'_> {
136        let mut params = QueryParams::default();
137        params.push_opt("limit", self.limit);
138        params.push_opt("marker", self.marker.as_ref());
139        params.push_opt("page_reverse", self.page_reverse);
140        if let Some(val) = &self.sort_dir {
141            params.extend(val.iter().map(|value| ("sort_dir", value)));
142        }
143        if let Some(val) = &self.sort_key {
144            params.extend(val.iter().map(|value| ("sort_key", value)));
145        }
146
147        params
148    }
149
150    fn service_type(&self) -> ServiceType {
151        ServiceType::Network
152    }
153
154    fn response_key(&self) -> Option<Cow<'static, str>> {
155        Some("tags".into())
156    }
157
158    /// Returns headers to be set into the request
159    fn request_headers(&self) -> Option<&HeaderMap> {
160        self._headers.as_ref()
161    }
162
163    /// Returns required API version
164    fn api_version(&self) -> Option<ApiVersion> {
165        Some(ApiVersion::new(2, 0))
166    }
167}
168impl Pageable for Request<'_> {}
169
170#[cfg(test)]
171mod tests {
172    use super::*;
173    #[cfg(feature = "sync")]
174    use crate::api::Query;
175    use crate::test::client::FakeOpenStackClient;
176    use crate::types::ServiceType;
177    use http::{HeaderName, HeaderValue};
178    use httpmock::MockServer;
179    use serde_json::json;
180
181    #[test]
182    fn test_service_type() {
183        assert_eq!(
184            Request::builder().build().unwrap().service_type(),
185            ServiceType::Network
186        );
187    }
188
189    #[test]
190    fn test_response_key() {
191        assert_eq!(
192            Request::builder().build().unwrap().response_key().unwrap(),
193            "tags"
194        );
195    }
196
197    #[cfg(feature = "sync")]
198    #[test]
199    fn endpoint() {
200        let server = MockServer::start();
201        let client = FakeOpenStackClient::new(server.base_url());
202        let mock = server.mock(|when, then| {
203            when.method(httpmock::Method::GET)
204                .path(format!("/ports/{port_id}/tags", port_id = "port_id",));
205
206            then.status(200)
207                .header("content-type", "application/json")
208                .json_body(json!({ "tags": {} }));
209        });
210
211        let endpoint = Request::builder().port_id("port_id").build().unwrap();
212        let _: serde_json::Value = endpoint.query(&client).unwrap();
213        mock.assert();
214    }
215
216    #[cfg(feature = "sync")]
217    #[test]
218    fn endpoint_headers() {
219        let server = MockServer::start();
220        let client = FakeOpenStackClient::new(server.base_url());
221        let mock = server.mock(|when, then| {
222            when.method(httpmock::Method::GET)
223                .path(format!("/ports/{port_id}/tags", port_id = "port_id",))
224                .header("foo", "bar")
225                .header("not_foo", "not_bar");
226            then.status(200)
227                .header("content-type", "application/json")
228                .json_body(json!({ "tags": {} }));
229        });
230
231        let endpoint = Request::builder()
232            .port_id("port_id")
233            .headers(
234                [(
235                    Some(HeaderName::from_static("foo")),
236                    HeaderValue::from_static("bar"),
237                )]
238                .into_iter(),
239            )
240            .header(
241                HeaderName::from_static("not_foo"),
242                HeaderValue::from_static("not_bar"),
243            )
244            .build()
245            .unwrap();
246        let _: serde_json::Value = endpoint.query(&client).unwrap();
247        mock.assert();
248    }
249}