clevercloud_sdk/v4/addon_provider/
postgresql.rs

1//! # Postgresql addon provider module
2//!
3//! This module provide helpers and structures to interact with the postgresql
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(
31        "failed to parse version from '{0}', available versions are 17, 16, 15, 14, 13, 12 and 11"
32    )]
33    ParseVersion(String),
34    #[error("failed to get information about addon provider '{0}', {1}")]
35    Get(AddonProviderId, ClientError),
36}
37
38// -----------------------------------------------------------------------------
39// Version enum
40
41#[cfg_attr(feature = "jsonschemas", derive(JsonSchemaRepr))]
42#[derive(SerializeRepr, DeserializeRepr, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug)]
43#[serde(untagged)]
44#[repr(i32)]
45pub enum Version {
46    V11 = 11,
47    V12 = 12,
48    V13 = 13,
49    V14 = 14,
50    V15 = 15,
51    V16 = 16,
52    V17 = 17,
53}
54
55impl FromStr for Version {
56    type Err = Error;
57
58    fn from_str(s: &str) -> Result<Self, Self::Err> {
59        Ok(match s {
60            "17" => Self::V17,
61            "16" => Self::V16,
62            "15" => Self::V15,
63            "14" => Self::V14,
64            "13" => Self::V13,
65            "12" => Self::V12,
66            "11" => Self::V11,
67            _ => {
68                return Err(Error::ParseVersion(s.to_owned()));
69            }
70        })
71    }
72}
73
74impl TryFrom<String> for Version {
75    type Error = Error;
76
77    fn try_from(s: String) -> Result<Self, Self::Error> {
78        Self::from_str(&s)
79    }
80}
81
82#[allow(clippy::from_over_into)]
83impl Into<String> for Version {
84    fn into(self) -> String {
85        self.to_string()
86    }
87}
88
89impl Display for Version {
90    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
91        match self {
92            Self::V17 => write!(f, "17"),
93            Self::V16 => write!(f, "16"),
94            Self::V15 => write!(f, "15"),
95            Self::V14 => write!(f, "14"),
96            Self::V13 => write!(f, "13"),
97            Self::V12 => write!(f, "12"),
98            Self::V11 => write!(f, "11"),
99        }
100    }
101}
102
103// -----------------------------------------------------------------------------
104// Helpers functions
105
106#[cfg_attr(feature = "tracing", tracing::instrument)]
107/// returns information about the postgresql addon provider
108pub async fn get(client: &Client) -> Result<AddonProvider<Version>, Error> {
109    let path = format!(
110        "{}/v4/addon-providers/{}",
111        client.endpoint,
112        AddonProviderId::PostgreSql
113    );
114
115    #[cfg(feature = "logging")]
116    if log_enabled!(Level::Debug) {
117        debug!(
118            "execute a request to get information about the postgresql addon-provider, path: '{}', name: '{}'",
119            &path,
120            AddonProviderId::PostgreSql
121        );
122    }
123
124    client
125        .get(&path)
126        .await
127        .map_err(|err| Error::Get(AddonProviderId::PostgreSql, err))
128}