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', 'metabase', 'keycloak', \
84        'cellar-addon', and 'addon-matomo'"
85    )]
86    Parse(String),
87}
88
89// -----------------------------------------------------------------------------
90// AddonProviderName structure
91
92#[cfg_attr(feature = "jsonschemas", derive(JsonSchema))]
93#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug)]
94#[serde(untagged, try_from = "String", into = "String")]
95pub enum AddonProviderId {
96    PostgreSql,
97    Redis,
98    MySql,
99    MongoDb,
100    Pulsar,
101    KV,
102    ConfigProvider,
103    ElasticSearch,
104    Metabase,
105    Keycloak,
106    Cellar,
107    Matomo,
108}
109
110impl FromStr for AddonProviderId {
111    type Err = Error;
112
113    #[cfg_attr(feature = "tracing", tracing::instrument)]
114    fn from_str(s: &str) -> Result<Self, Self::Err> {
115        Ok(match s.to_lowercase().as_str() {
116            "postgresql-addon" => Self::PostgreSql,
117            "redis-addon" => Self::Redis,
118            "mysql-addon" => Self::MySql,
119            "mongodb-addon" => Self::MongoDb,
120            "addon-pulsar" => Self::Pulsar,
121            "kv" => Self::KV,
122            "config-provider" => Self::ConfigProvider,
123            "es-addon" => Self::ElasticSearch,
124            "metabase" => Self::Metabase,
125            "keycloack" => Self::Keycloak,
126            "cellar-addon" => Self::Cellar,
127            "keycloak" => Self::Keycloak,
128            "addon-matomo" => Self::Matomo,
129            _ => return Err(Error::Parse(s.to_owned())),
130        })
131    }
132}
133
134impl TryFrom<String> for AddonProviderId {
135    type Error = Error;
136
137    #[cfg_attr(feature = "tracing", tracing::instrument)]
138    fn try_from(s: String) -> Result<Self, Self::Error> {
139        Self::from_str(&s)
140    }
141}
142
143#[allow(clippy::from_over_into)]
144impl Into<String> for AddonProviderId {
145    #[cfg_attr(feature = "tracing", tracing::instrument)]
146    fn into(self) -> String {
147        self.to_string()
148    }
149}
150
151impl Display for AddonProviderId {
152    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
153        match self {
154            Self::PostgreSql => write!(f, "postgresql-addon"),
155            Self::Redis => write!(f, "redis-addon"),
156            Self::MySql => write!(f, "mysql-addon"),
157            Self::MongoDb => write!(f, "mongodb-addon"),
158            Self::Pulsar => write!(f, "addon-pulsar"),
159            Self::KV => write!(f, "kv"),
160            Self::ConfigProvider => write!(f, "config-provider"),
161            Self::ElasticSearch => write!(f, "es-addon"),
162            Self::Metabase => write!(f, "metabase"),
163            Self::Keycloak => write!(f, "keycloak"),
164            Self::Cellar => write!(f, "cellar-addon"),
165            Self::Matomo => write!(f, "addon-matomo"),
166        }
167    }
168}