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