1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use std::{collections::BTreeMap, fmt::Debug};
use hyper::client::connect::Connect;
#[cfg(feature = "logging")]
use log::{debug, log_enabled, Level};
use oauth10a::client::{ClientError, RestClient};
#[cfg(feature = "jsonschemas")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{v4::addon_provider::config_provider::addon::environment::Variable, Client};
#[cfg_attr(feature = "jsonschemas", derive(JsonSchema))]
#[derive(Serialize, Deserialize, PartialEq, PartialOrd, Clone, Debug)]
pub struct Provider {
#[serde(rename = "id")]
pub id: String,
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "website")]
pub website: String,
#[serde(rename = "supportEmail")]
pub support_email: String,
#[serde(rename = "googlePlusName")]
pub google_plus_name: String,
#[serde(rename = "twitterName")]
pub twitter_name: String,
#[serde(rename = "analyticsId")]
pub analytics_id: String,
#[serde(rename = "shortDesc")]
pub short_description: String,
#[serde(rename = "longDesc")]
pub long_description: String,
#[serde(rename = "logoUrl")]
pub logo_url: String,
#[serde(rename = "status")]
pub status: String,
#[serde(rename = "openInNewTab")]
pub open_in_new_tab: bool,
#[serde(rename = "canUpgrade")]
pub can_upgrade: bool,
#[serde(rename = "regions")]
pub regions: Vec<String>,
}
#[cfg_attr(feature = "jsonschemas", derive(JsonSchema))]
#[derive(Serialize, Deserialize, PartialEq, PartialOrd, Clone, Debug)]
pub struct Feature {
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "type")]
pub kind: String,
#[serde(rename = "value")]
pub value: String,
#[serde(rename = "computable_value")]
pub computable_value: Option<String>,
#[serde(rename = "name_code")]
pub name_code: Option<String>,
}
#[cfg_attr(feature = "jsonschemas", derive(JsonSchema))]
#[derive(Serialize, Deserialize, PartialEq, PartialOrd, Clone, Debug)]
pub struct Plan {
#[serde(rename = "id")]
pub id: String,
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "slug")]
pub slug: String,
#[serde(rename = "price")]
pub price: f32,
#[serde(rename = "price_id")]
pub price_id: String,
#[serde(rename = "features")]
pub features: Vec<Feature>,
#[serde(rename = "zones")]
pub zones: Vec<String>,
}
#[cfg_attr(feature = "jsonschemas", derive(JsonSchema))]
#[derive(Serialize, Deserialize, PartialEq, PartialOrd, Clone, Debug)]
pub struct Addon {
#[serde(rename = "id")]
pub id: String,
#[serde(rename = "name")]
pub name: Option<String>,
#[serde(rename = "realId")]
pub real_id: String,
#[serde(rename = "region")]
pub region: String,
#[serde(rename = "provider")]
pub provider: Provider,
#[serde(rename = "plan")]
pub plan: Plan,
#[serde(rename = "creationDate")]
pub creation_date: u64,
#[serde(rename = "configKeys")]
pub config_keys: Vec<String>,
}
#[cfg_attr(feature = "jsonschemas", derive(JsonSchema))]
#[derive(Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord, Clone, Debug, Default)]
pub struct Opts {
#[serde(rename = "version", skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(rename = "encryption", skip_serializing_if = "Option::is_none")]
pub encryption: Option<String>,
#[serde(rename = "services", skip_serializing_if = "Option::is_none")]
pub services: Option<String>,
}
#[cfg_attr(feature = "jsonschemas", derive(JsonSchema))]
#[derive(Serialize, Deserialize, PartialEq, PartialOrd, Clone, Debug)]
pub struct CreateOpts {
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "region")]
pub region: String,
#[serde(rename = "providerId")]
pub provider_id: String,
#[serde(rename = "plan")]
pub plan: String,
#[serde(rename = "options")]
pub options: Opts,
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("failed to list addons of organisation '{0}', {1}")]
List(String, ClientError),
#[error("failed to get addon '{0}' of organisation '{1}', {2}")]
Get(String, String, ClientError),
#[error("failed to get addon '{0}' environment of organisation '{1}', {2}")]
Environment(String, String, ClientError),
#[error("failed to create addon for organisation '{0}', {1}")]
Create(String, ClientError),
#[error("failed to delete addon '{0}' for organisation '{1}', {2}")]
Delete(String, String, ClientError),
}
#[cfg_attr(feature = "trace", tracing::instrument)]
pub async fn list<C>(client: &Client<C>, organisation_id: &str) -> Result<Vec<Addon>, Error>
where
C: Connect + Clone + Debug + Send + Sync + 'static,
{
let path = format!(
"{}/v2/organisations/{}/addons",
client.endpoint, organisation_id,
);
#[cfg(feature = "logging")]
if log_enabled!(Level::Debug) {
debug!(
"execute a request to get the list of addons, path: '{}', organisation: '{}'",
&path, organisation_id
);
}
client
.get(&path)
.await
.map_err(|err| Error::List(organisation_id.to_owned(), err))
}
#[cfg_attr(feature = "trace", tracing::instrument)]
pub async fn get<C>(client: &Client<C>, organisation_id: &str, id: &str) -> Result<Addon, Error>
where
C: Connect + Clone + Debug + Send + Sync + 'static,
{
let path = format!(
"{}/v2/organisations/{}/addons/{}",
client.endpoint, organisation_id, id
);
#[cfg(feature = "logging")]
if log_enabled!(Level::Debug) {
debug!("execute a request to get information about an addon, path: '{}', organisation: '{}', id: '{}'", &path, organisation_id, id);
}
client
.get(&path)
.await
.map_err(|err| Error::Get(id.to_owned(), organisation_id.to_owned(), err))
}
#[cfg_attr(feature = "trace", tracing::instrument)]
pub async fn create<C>(
client: &Client<C>,
organisation_id: &str,
opts: &CreateOpts,
) -> Result<Addon, Error>
where
C: Connect + Clone + Debug + Send + Sync + 'static,
{
let path = format!(
"{}/v2/organisations/{}/addons",
client.endpoint, organisation_id
);
#[cfg(feature = "logging")]
if log_enabled!(Level::Debug) {
debug!("execute a request to create an addon, path: '{}', organisation: '{}', name: '{}', region: '{}', plan: '{}', provider-id: '{}'", &path, organisation_id, &opts.name, &opts.region, &opts.plan, &opts.provider_id.to_string());
}
client
.post(&path, opts)
.await
.map_err(|err| Error::Create(organisation_id.to_owned(), err))
}
#[cfg_attr(feature = "trace", tracing::instrument)]
pub async fn delete<C>(client: &Client<C>, organisation_id: &str, id: &str) -> Result<(), Error>
where
C: Connect + Clone + Debug + Send + Sync + 'static,
{
let path = format!(
"{}/v2/organisations/{}/addons/{}",
client.endpoint, organisation_id, id
);
#[cfg(feature = "logging")]
if log_enabled!(Level::Debug) {
debug!(
"execute a request to delete an addon, path: '{}', organisation: '{}', id: '{}'",
&path, organisation_id, id
);
}
client
.delete(&path)
.await
.map_err(|err| Error::Delete(id.to_owned(), organisation_id.to_owned(), err))
}
#[cfg_attr(feature = "trace", tracing::instrument)]
pub async fn environment<C>(
client: &Client<C>,
organisation_id: &str,
id: &str,
) -> Result<BTreeMap<String, String>, Error>
where
C: Connect + Clone + Debug + Send + Sync + 'static,
{
let path = format!(
"{}/v2/organisations/{}/addons/{}/env",
client.endpoint, organisation_id, id
);
#[cfg(feature = "logging")]
if log_enabled!(Level::Debug) {
debug!(
"execute a request to get secret of a addon, path: '{}', organisation: '{}', id: '{}'",
&path, organisation_id, id
);
}
let env: Vec<Variable> = client
.get(&path)
.await
.map_err(|err| Error::Environment(id.to_owned(), organisation_id.to_owned(), err))?;
Ok(env.iter().fold(BTreeMap::new(), |mut acc, var| {
acc.insert(var.name.to_owned(), var.value.to_owned());
acc
}))
}