openstack_sdk/api/network/v2/vpn/endpoint_group/
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::network::v2::vpn::endpoint_group::{get as Get, list as List};
26
27/// Find for vpn/endpoint_group 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 RequestBuilder<'_> {
46    /// Add a single header to the Volume.
47    pub fn header(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
48where {
49        self._headers
50            .get_or_insert(None)
51            .get_or_insert_with(HeaderMap::new)
52            .insert(header_name, HeaderValue::from_static(header_value));
53        self
54    }
55
56    /// Add multiple headers.
57    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
58    where
59        I: Iterator<Item = T>,
60        T: Into<(Option<HeaderName>, HeaderValue)>,
61    {
62        self._headers
63            .get_or_insert(None)
64            .get_or_insert_with(HeaderMap::new)
65            .extend(iter.map(Into::into));
66        self
67    }
68}
69
70impl<'a> Findable for Request<'a> {
71    type G = Get::Request<'a>;
72    type L = List::Request<'a>;
73    fn get_ep(&self) -> Get::Request<'a> {
74        let mut ep = Get::Request::builder();
75        ep.id(self.id.clone());
76        if let Some(headers) = &self._headers {
77            ep.headers(headers.iter().map(|(k, v)| (Some(k.clone()), v.clone())));
78        }
79        ep.build().unwrap()
80    }
81    fn list_ep(&self) -> List::Request<'a> {
82        let mut ep = List::Request::builder();
83        if let Some(headers) = &self._headers {
84            ep.headers(headers.iter().map(|(k, v)| (Some(k.clone()), v.clone())));
85        }
86        ep.build().unwrap()
87    }
88    /// Locate vpn/endpoint_group in a list
89    fn locate_resource_in_list<C: RestClient>(
90        &self,
91        data: Vec<serde_json::Value>,
92    ) -> Result<serde_json::Value, ApiError<C::Error>> {
93        // vpn/endpoint_group is not supporting name as query parameter to the list.
94        // Therefore it is necessary to go through complete list of results.
95        let mut maybe_result: Option<serde_json::Value> = None;
96        for item in data.iter() {
97            trace!("Validate item {:?} is what we search for", item);
98            if let Some(name_as_val) = item.get("name") {
99                if let Some(name) = name_as_val.as_str() {
100                    if name == self.id {
101                        if maybe_result.is_none() {
102                            maybe_result = Some(item.clone());
103                        } else {
104                            return Err(ApiError::IdNotUnique);
105                        }
106                    }
107                }
108            }
109        }
110        maybe_result.ok_or(ApiError::ResourceNotFound)
111    }
112}