openstack_sdk/api/network/v2/service_profile/
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 service profile.
19//!
20//! This operation returns a service profile object by ID. If you are not an
21//! administrative user and the object is not visible to your tenant account,
22//! the service returns the HTTP `Forbidden (403)` response code.
23//!
24//! Use the `fields` query parameter to control which fields are returned in
25//! the response body. For more information, see [Fields](#fields).
26//!
27//! Normal response codes: 200
28//!
29//! Error response codes: 401, 403, 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.0/flavors/{flavor_id}/service_profiles/{id} API
42    #[builder(default, setter(into))]
43    id: Cow<'a, str>,
44
45    #[builder(setter(name = "_headers"), default, private)]
46    _headers: Option<HeaderMap>,
47}
48impl<'a> Request<'a> {
49    /// Create a builder for the endpoint.
50    pub fn builder() -> RequestBuilder<'a> {
51        RequestBuilder::default()
52    }
53}
54
55impl<'a> RequestBuilder<'a> {
56    /// Add a single header to the Service_Profile.
57    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
58    where
59        K: Into<HeaderName>,
60        V: Into<HeaderValue>,
61    {
62        self._headers
63            .get_or_insert(None)
64            .get_or_insert_with(HeaderMap::new)
65            .insert(header_name.into(), header_value.into());
66        self
67    }
68
69    /// Add multiple headers.
70    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
71    where
72        I: Iterator<Item = T>,
73        T: Into<(Option<HeaderName>, HeaderValue)>,
74    {
75        self._headers
76            .get_or_insert(None)
77            .get_or_insert_with(HeaderMap::new)
78            .extend(iter.map(Into::into));
79        self
80    }
81}
82
83impl RestEndpoint for Request<'_> {
84    fn method(&self) -> http::Method {
85        http::Method::GET
86    }
87
88    fn endpoint(&self) -> Cow<'static, str> {
89        format!("service_profiles/{id}", id = self.id.as_ref(),).into()
90    }
91
92    fn parameters(&self) -> QueryParams<'_> {
93        QueryParams::default()
94    }
95
96    fn service_type(&self) -> ServiceType {
97        ServiceType::Network
98    }
99
100    fn response_key(&self) -> Option<Cow<'static, str>> {
101        Some("service_profile".into())
102    }
103
104    /// Returns headers to be set into the request
105    fn request_headers(&self) -> Option<&HeaderMap> {
106        self._headers.as_ref()
107    }
108
109    /// Returns required API version
110    fn api_version(&self) -> Option<ApiVersion> {
111        Some(ApiVersion::new(2, 0))
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118    #[cfg(feature = "sync")]
119    use crate::api::Query;
120    use crate::test::client::FakeOpenStackClient;
121    use crate::types::ServiceType;
122    use http::{HeaderName, HeaderValue};
123    use httpmock::MockServer;
124    use serde_json::json;
125
126    #[test]
127    fn test_service_type() {
128        assert_eq!(
129            Request::builder().build().unwrap().service_type(),
130            ServiceType::Network
131        );
132    }
133
134    #[test]
135    fn test_response_key() {
136        assert_eq!(
137            Request::builder().build().unwrap().response_key().unwrap(),
138            "service_profile"
139        );
140    }
141
142    #[cfg(feature = "sync")]
143    #[test]
144    fn endpoint() {
145        let server = MockServer::start();
146        let client = FakeOpenStackClient::new(server.base_url());
147        let mock = server.mock(|when, then| {
148            when.method(httpmock::Method::GET)
149                .path(format!("/service_profiles/{id}", id = "id",));
150
151            then.status(200)
152                .header("content-type", "application/json")
153                .json_body(json!({ "service_profile": {} }));
154        });
155
156        let endpoint = Request::builder().id("id").build().unwrap();
157        let _: serde_json::Value = endpoint.query(&client).unwrap();
158        mock.assert();
159    }
160
161    #[cfg(feature = "sync")]
162    #[test]
163    fn endpoint_headers() {
164        let server = MockServer::start();
165        let client = FakeOpenStackClient::new(server.base_url());
166        let mock = server.mock(|when, then| {
167            when.method(httpmock::Method::GET)
168                .path(format!("/service_profiles/{id}", id = "id",))
169                .header("foo", "bar")
170                .header("not_foo", "not_bar");
171            then.status(200)
172                .header("content-type", "application/json")
173                .json_body(json!({ "service_profile": {} }));
174        });
175
176        let endpoint = Request::builder()
177            .id("id")
178            .headers(
179                [(
180                    Some(HeaderName::from_static("foo")),
181                    HeaderValue::from_static("bar"),
182                )]
183                .into_iter(),
184            )
185            .header(
186                HeaderName::from_static("not_foo"),
187                HeaderValue::from_static("not_bar"),
188            )
189            .build()
190            .unwrap();
191        let _: serde_json::Value = endpoint.query(&client).unwrap();
192        mock.assert();
193    }
194}