clevercloud_sdk/v4/addon_provider/
elasticsearch.rs

1//! # ElasticSearch addon provider module
2//!
3//! This module provides helpers and structures to interact with the elasticsearch
4//! addon provider
5#![allow(deprecated)]
6
7use std::{
8    convert::TryFrom,
9    fmt::{self, Debug, Display, Formatter},
10    str::FromStr,
11};
12
13#[cfg(feature = "logging")]
14use log::{Level, debug, log_enabled};
15use oauth10a::client::{ClientError, RestClient};
16#[cfg(feature = "jsonschemas")]
17use schemars::JsonSchema_repr as JsonSchemaRepr;
18use serde_repr::{Deserialize_repr as DeserializeRepr, Serialize_repr as SerializeRepr};
19
20use crate::{
21    Client,
22    v4::addon_provider::{AddonProvider, AddonProviderId},
23};
24
25// -----------------------------------------------------------------------------
26// Error enumeration
27
28#[derive(thiserror::Error, Debug)]
29pub enum Error {
30    #[error("failed to parse version from '{0}', available version are 7 and 8")]
31    ParseVersion(String),
32    #[error("failed to get information about addon provider '{0}', {1}")]
33    Get(AddonProviderId, ClientError),
34}
35
36// -----------------------------------------------------------------------------
37// Version enum
38
39#[cfg_attr(feature = "jsonschemas", derive(JsonSchemaRepr))]
40#[derive(SerializeRepr, DeserializeRepr, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug)]
41#[serde(untagged)]
42#[repr(i32)]
43pub enum Version {
44    V7 = 7,
45    V8 = 8,
46}
47
48impl FromStr for Version {
49    type Err = Error;
50
51    fn from_str(s: &str) -> Result<Self, Self::Err> {
52        Ok(match s {
53            "7" => Self::V7,
54            "8" => Self::V8,
55            _ => {
56                return Err(Error::ParseVersion(s.to_owned()));
57            }
58        })
59    }
60}
61
62impl TryFrom<String> for Version {
63    type Error = Error;
64
65    fn try_from(s: String) -> Result<Self, Self::Error> {
66        Self::from_str(&s)
67    }
68}
69
70#[allow(clippy::from_over_into)]
71impl Into<String> for Version {
72    fn into(self) -> String {
73        self.to_string()
74    }
75}
76
77impl Display for Version {
78    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
79        match self {
80            Self::V7 => write!(f, "7"),
81            Self::V8 => write!(f, "8"),
82        }
83    }
84}
85
86// -----------------------------------------------------------------------------
87// Helpers functions
88
89/// returns information about the elasticsearch addon provider
90#[cfg_attr(feature = "tracing", tracing::instrument)]
91pub async fn get(client: &Client) -> Result<AddonProvider<Version>, Error> {
92    let path = format!(
93        "{}/v4/addon-providers/{}",
94        client.endpoint,
95        AddonProviderId::ElasticSearch
96    );
97
98    #[cfg(feature = "logging")]
99    if log_enabled!(Level::Debug) {
100        debug!(
101            "execute a request to get information about the elasticsearch addon-provider, path: '{}', name: '{}'",
102            &path,
103            AddonProviderId::ElasticSearch
104        );
105    }
106
107    client
108        .get(&path)
109        .await
110        .map_err(|err| Error::Get(AddonProviderId::ElasticSearch, err))
111}