clevercloud_sdk/v4/addon_provider/
mod.rs

1//! # Addon provider module
2//!
3//! This module provide structures and helpers to interact with clever-cloud's
4//! addon-provider
5
6use std::{
7    collections::BTreeMap,
8    convert::TryFrom,
9    fmt::{self, Debug, Display, Formatter},
10    hash::Hash,
11    str::FromStr,
12};
13
14#[cfg(feature = "jsonschemas")]
15use schemars::JsonSchema;
16use serde::{Deserialize, Serialize};
17
18pub mod config_provider;
19pub mod elasticsearch;
20pub mod mongodb;
21pub mod mysql;
22pub mod plan;
23pub mod postgresql;
24pub mod redis;
25
26// -----------------------------------------------------------------------------
27// Feature structure
28
29#[cfg_attr(feature = "jsonschemas", derive(JsonSchema))]
30#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Clone, Debug)]
31pub struct Feature {
32    #[serde(rename = "name")]
33    pub name: String,
34    #[serde(rename = "enabled")]
35    pub enabled: bool,
36}
37
38// -----------------------------------------------------------------------------
39// Cluster structure
40
41#[cfg_attr(feature = "jsonschemas", derive(JsonSchema))]
42#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
43pub struct Cluster<T> {
44    #[serde(rename = "id")]
45    pub id: String,
46    #[serde(rename = "label")]
47    pub label: String,
48    #[serde(rename = "zone")]
49    pub zone: String,
50    #[serde(rename = "features")]
51    pub features: Vec<Feature>,
52    #[serde(rename = "version")]
53    pub version: T,
54}
55
56// -----------------------------------------------------------------------------
57// AddonProvider structure
58
59#[cfg_attr(feature = "jsonschemas", derive(JsonSchema))]
60#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
61pub struct AddonProvider<T>
62where
63    T: Ord,
64{
65    #[serde(rename = "providerId")]
66    pub provider_id: AddonProviderId,
67    #[serde(rename = "clusters")]
68    pub clusters: Vec<Cluster<T>>,
69    #[serde(rename = "dedicated")]
70    pub dedicated: BTreeMap<T, Vec<Feature>>,
71    #[serde(rename = "defaultDedicatedVersion")]
72    pub default: T,
73}
74
75// -----------------------------------------------------------------------------
76// Error enumeration
77
78#[derive(thiserror::Error, Debug)]
79pub enum Error {
80    #[error(
81        "failed to parse addon provider identifier '{0}', available options are \
82        'postgresql-addon', 'redis-addon', 'mysql-addon', 'mongodb-addon', \
83        'addon-pulsar', 'config-provider', 'es-addon', 'kv' and 'metabase'"
84    )]
85    Parse(String),
86}
87
88// -----------------------------------------------------------------------------
89// AddonProviderName structure
90
91#[cfg_attr(feature = "jsonschemas", derive(JsonSchema))]
92#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug)]
93#[serde(untagged, try_from = "String", into = "String")]
94pub enum AddonProviderId {
95    PostgreSql,
96    Redis,
97    MySql,
98    MongoDb,
99    Pulsar,
100    KV,
101    ConfigProvider,
102    ElasticSearch,
103    Metabase,
104}
105
106impl FromStr for AddonProviderId {
107    type Err = Error;
108
109    #[cfg_attr(feature = "tracing", tracing::instrument)]
110    fn from_str(s: &str) -> Result<Self, Self::Err> {
111        Ok(match s.to_lowercase().as_str() {
112            "postgresql-addon" => Self::PostgreSql,
113            "redis-addon" => Self::Redis,
114            "mysql-addon" => Self::MySql,
115            "mongodb-addon" => Self::MongoDb,
116            "addon-pulsar" => Self::Pulsar,
117            "kv" => Self::KV,
118            "config-provider" => Self::ConfigProvider,
119            "es-addon" => Self::ElasticSearch,
120            "metabase" => Self::Metabase,
121            _ => return Err(Error::Parse(s.to_owned())),
122        })
123    }
124}
125
126impl TryFrom<String> for AddonProviderId {
127    type Error = Error;
128
129    #[cfg_attr(feature = "tracing", tracing::instrument)]
130    fn try_from(s: String) -> Result<Self, Self::Error> {
131        Self::from_str(&s)
132    }
133}
134
135#[allow(clippy::from_over_into)]
136impl Into<String> for AddonProviderId {
137    #[cfg_attr(feature = "tracing", tracing::instrument)]
138    fn into(self) -> String {
139        self.to_string()
140    }
141}
142
143impl Display for AddonProviderId {
144    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
145        match self {
146            Self::PostgreSql => write!(f, "postgresql-addon"),
147            Self::Redis => write!(f, "redis-addon"),
148            Self::MySql => write!(f, "mysql-addon"),
149            Self::MongoDb => write!(f, "mongodb-addon"),
150            Self::Pulsar => write!(f, "addon-pulsar"),
151            Self::KV => write!(f, "kv"),
152            Self::ConfigProvider => write!(f, "config-provider"),
153            Self::ElasticSearch => write!(f, "es-addon"),
154            Self::Metabase => write!(f, "metabase"),
155        }
156    }
157}