openstack_sdk/api/compute/v2/flavor/
find.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`.
17use derive_builder::Builder;
18use http::{HeaderMap, HeaderName, HeaderValue};
19
20use crate::api::find::Findable;
21use crate::api::rest_endpoint_prelude::*;
22use crate::api::{ApiError, RestClient};
23use tracing::trace;
24
25use crate::api::compute::v2::flavor::{get as Get, list_detailed as List};
26
27/// Find for flavor by nameOrId.
28#[derive(Debug, Builder, Clone)]
29#[builder(setter(strip_option))]
30pub struct Request<'a> {
31    #[builder(setter(into), default)]
32    id: Cow<'a, str>,
33
34    #[builder(setter(name = "_headers"), default, private)]
35    _headers: Option<HeaderMap>,
36}
37
38impl<'a> Request<'a> {
39    /// Create a builder for the endpoint.
40    pub fn builder() -> RequestBuilder<'a> {
41        RequestBuilder::default()
42    }
43}
44
45impl<'a> RequestBuilder<'a> {
46    /// Add a single header to the Volume.
47    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
48    where
49        K: Into<HeaderName>,
50        V: Into<HeaderValue>,
51    {
52        self._headers
53            .get_or_insert(None)
54            .get_or_insert_with(HeaderMap::new)
55            .insert(header_name.into(), header_value.into());
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<'a> Findable for Request<'a> {
74    type G = Get::Request<'a>;
75    type L = List::Request<'a>;
76    fn get_ep<C: RestClient>(&self) -> Result<Get::Request<'a>, ApiError<C::Error>> {
77        let mut ep = Get::Request::builder();
78        ep.id(self.id.clone());
79        if let Some(headers) = &self._headers {
80            ep.headers(headers.iter().map(|(k, v)| (Some(k.clone()), v.clone())));
81        }
82        ep.build().map_err(ApiError::endpoint_builder)
83    }
84
85    fn list_ep<C: RestClient>(&self) -> Result<List::Request<'a>, ApiError<C::Error>> {
86        let mut ep = List::Request::builder();
87        if let Some(headers) = &self._headers {
88            ep.headers(headers.iter().map(|(k, v)| (Some(k.clone()), v.clone())));
89        }
90        ep.build().map_err(ApiError::endpoint_builder)
91    }
92    /// Locate flavor in a list
93    fn locate_resource_in_list<C: RestClient>(
94        &self,
95        data: Vec<serde_json::Value>,
96    ) -> Result<serde_json::Value, ApiError<C::Error>> {
97        // flavor is not supporting name as query parameter to the list.
98        // Therefore it is necessary to go through complete list of results.
99        let mut maybe_result: Option<serde_json::Value> = None;
100        for item in data.iter() {
101            trace!("Validate item {:?} is what we search for", item);
102            if let Some(name_as_val) = item.get("name") {
103                if let Some(name) = name_as_val.as_str() {
104                    if name == self.id {
105                        if maybe_result.is_none() {
106                            maybe_result = Some(item.clone());
107                        } else {
108                            return Err(ApiError::IdNotUnique);
109                        }
110                    }
111                }
112            }
113        }
114        maybe_result.ok_or(ApiError::ResourceNotFound)
115    }
116}