clevercloud_sdk/v4/addon_provider/
redis.rs

1//! # Redis addon provider module
2//!
3//! This module provide helpers and structures to interact with the redis
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 is 7.2.4")]
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    V7dot2dot4 = 724,
45}
46
47impl FromStr for Version {
48    type Err = Error;
49
50    fn from_str(s: &str) -> Result<Self, Self::Err> {
51        Ok(match s {
52            "7.2.4" => Self::V7dot2dot4,
53            _ => {
54                return Err(Error::ParseVersion(s.to_owned()));
55            }
56        })
57    }
58}
59
60impl TryFrom<String> for Version {
61    type Error = Error;
62
63    fn try_from(s: String) -> Result<Self, Self::Error> {
64        Self::from_str(&s)
65    }
66}
67
68#[allow(clippy::from_over_into)]
69impl Into<String> for Version {
70    fn into(self) -> String {
71        self.to_string()
72    }
73}
74
75impl Display for Version {
76    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
77        match self {
78            Self::V7dot2dot4 => write!(f, "7.2.4"),
79        }
80    }
81}
82
83// -----------------------------------------------------------------------------
84// Helpers functions
85
86#[cfg_attr(feature = "tracing", tracing::instrument)]
87/// returns information about the redis addon provider
88pub async fn get(client: &Client) -> Result<AddonProvider<Version>, Error> {
89    let path = format!(
90        "{}/v4/addon-providers/{}",
91        client.endpoint,
92        AddonProviderId::Redis
93    );
94
95    #[cfg(feature = "logging")]
96    if log_enabled!(Level::Debug) {
97        debug!(
98            "execute a request to get information about the redis addon-provider, path: '{}', name: '{}'",
99            &path,
100            AddonProviderId::Redis
101        );
102    }
103
104    client
105        .get(&path)
106        .await
107        .map_err(|err| Error::Get(AddonProviderId::Redis, err))
108}