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