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