openstack_sdk/api/block_storage/v3/volume/encryption/
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//! Return a single encryption item.
19//!
20use derive_builder::Builder;
21use http::{HeaderMap, HeaderName, HeaderValue};
22
23use crate::api::rest_endpoint_prelude::*;
24
25use std::borrow::Cow;
26
27#[derive(Builder, Debug, Clone)]
28#[builder(setter(strip_option))]
29pub struct Request<'a> {
30    /// id parameter for /v3/volumes/{volume_id}/encryption/{id} API
31    #[builder(default, setter(into))]
32    id: Cow<'a, str>,
33
34    /// volume_id parameter for /v3/volumes/{volume_id}/encryption/{id} API
35    #[builder(default, setter(into))]
36    volume_id: Cow<'a, str>,
37
38    #[builder(setter(name = "_headers"), default, private)]
39    _headers: Option<HeaderMap>,
40}
41impl<'a> Request<'a> {
42    /// Create a builder for the endpoint.
43    pub fn builder() -> RequestBuilder<'a> {
44        RequestBuilder::default()
45    }
46}
47
48impl RequestBuilder<'_> {
49    /// Add a single header to the Encryption.
50    pub fn header(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
51where {
52        self._headers
53            .get_or_insert(None)
54            .get_or_insert_with(HeaderMap::new)
55            .insert(header_name, HeaderValue::from_static(header_value));
56        self
57    }
58
59    /// Add multiple headers.
60    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
61    where
62        I: Iterator<Item = T>,
63        T: Into<(Option<HeaderName>, HeaderValue)>,
64    {
65        self._headers
66            .get_or_insert(None)
67            .get_or_insert_with(HeaderMap::new)
68            .extend(iter.map(Into::into));
69        self
70    }
71}
72
73impl RestEndpoint for Request<'_> {
74    fn method(&self) -> http::Method {
75        http::Method::GET
76    }
77
78    fn endpoint(&self) -> Cow<'static, str> {
79        format!(
80            "volumes/{volume_id}/encryption/{id}",
81            volume_id = self.volume_id.as_ref(),
82            id = self.id.as_ref(),
83        )
84        .into()
85    }
86
87    fn parameters(&self) -> QueryParams {
88        QueryParams::default()
89    }
90
91    fn service_type(&self) -> ServiceType {
92        ServiceType::BlockStorage
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(3, 0))
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::BlockStorage
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).path(format!(
141                "/volumes/{volume_id}/encryption/{id}",
142                volume_id = "volume_id",
143                id = "id",
144            ));
145
146            then.status(200)
147                .header("content-type", "application/json")
148                .json_body(json!({ "dummy": {} }));
149        });
150
151        let endpoint = Request::builder()
152            .volume_id("volume_id")
153            .id("id")
154            .build()
155            .unwrap();
156        let _: serde_json::Value = endpoint.query(&client).unwrap();
157        mock.assert();
158    }
159
160    #[cfg(feature = "sync")]
161    #[test]
162    fn endpoint_headers() {
163        let server = MockServer::start();
164        let client = FakeOpenStackClient::new(server.base_url());
165        let mock = server.mock(|when, then| {
166            when.method(httpmock::Method::GET)
167                .path(format!(
168                    "/volumes/{volume_id}/encryption/{id}",
169                    volume_id = "volume_id",
170                    id = "id",
171                ))
172                .header("foo", "bar")
173                .header("not_foo", "not_bar");
174            then.status(200)
175                .header("content-type", "application/json")
176                .json_body(json!({ "dummy": {} }));
177        });
178
179        let endpoint = Request::builder()
180            .volume_id("volume_id")
181            .id("id")
182            .headers(
183                [(
184                    Some(HeaderName::from_static("foo")),
185                    HeaderValue::from_static("bar"),
186                )]
187                .into_iter(),
188            )
189            .header("not_foo", "not_bar")
190            .build()
191            .unwrap();
192        let _: serde_json::Value = endpoint.query(&client).unwrap();
193        mock.assert();
194    }
195}