clevercloud_sdk/v4/addon_provider/
mongodb.rs

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