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