shuttle_common/
resource.rs

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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use std::{fmt::Display, str::FromStr};

use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;

use crate::{constants::RESOURCE_SCHEMA_VERSION, database};

/// Return this struct as a resource config to make Shuttle provision it
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ProvisionResourceRequest {
    /// The version of the config+data schema for this Shuttle resource
    pub version: u32,
    /// The type of this resource
    pub r#type: Type,
    /// The config used when creating this resource.
    /// Use `Self::version` and `Self::r#type` to know how to parse this data.
    pub config: Value,

    /// Arbitrary extra data to include in this resource
    pub custom: Value,
}

impl ProvisionResourceRequest {
    pub fn new(r#type: Type, config: Value, custom: Value) -> Self {
        Self {
            version: RESOURCE_SCHEMA_VERSION,
            r#type,
            config,
            custom,
        }
    }
}

impl From<ProvisionResourceRequest> for ProvisionResourceRequestBeta {
    fn from(value: ProvisionResourceRequest) -> Self {
        Self {
            r#type: match value.r#type {
                Type::Database(database::Type::Shared(database::SharedEngine::Postgres)) => {
                    ResourceTypeBeta::DatabaseSharedPostgres
                }
                Type::Secrets => ResourceTypeBeta::Secrets,
                Type::Container => ResourceTypeBeta::Container,
                r => panic!("Resource not supported on shuttle.dev: {r}"),
            },
            config: value.config,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[typeshare::typeshare]
pub struct ProvisionResourceRequestBeta {
    /// The type of this resource
    pub r#type: ResourceTypeBeta,
    /// The config used when creating this resource.
    /// Use `Self::r#type` to know how to parse this data.
    pub config: Value,
}

/// Helper for deserializing
#[derive(Deserialize)]
#[serde(untagged)] // Try deserializing as a Shuttle resource, fall back to a custom value
pub enum ResourceInput {
    Shuttle(ProvisionResourceRequest),
    Custom(Value),
}

/// Helper for deserializing
#[derive(Deserialize)]
#[serde(untagged)] // Try deserializing as a Shuttle resource, fall back to a custom value
pub enum ResourceInputBeta {
    Shuttle(ProvisionResourceRequestBeta),
    Custom(Value),
}

/// The resource state represents the stage of the provisioning process the resource is in.
#[derive(
    Debug, Clone, PartialEq, Eq, strum::Display, strum::EnumString, Serialize, Deserialize,
)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
#[typeshare::typeshare]
pub enum ResourceState {
    Authorizing,
    Provisioning,
    Failed,
    Ready,
    Deleting,
    Deleted,
}

/// Returned when provisioning a Shuttle resource
#[derive(Serialize, Deserialize)]
pub struct ShuttleResourceOutput<T> {
    /// The output type for this Shuttle resource,
    /// contains the data from the provisioner response
    pub output: T,

    /// Arbitrary extra data in this resource
    pub custom: Value,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[typeshare::typeshare]
pub struct ResourceResponseBeta {
    pub r#type: ResourceTypeBeta,
    pub state: ResourceState,
    /// The config used when creating this resource. Use the `r#type` to know how to parse this data.
    pub config: Value,
    /// The output type for this resource, if state is Ready. Use the `r#type` to know how to parse this data.
    pub output: Value,
}

#[derive(Debug, Serialize, Deserialize)]
#[typeshare::typeshare]
pub struct ResourceListResponseBeta {
    pub resources: Vec<ResourceResponseBeta>,
}

/// Common type to hold all the information we need for a generic resource
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct Response {
    /// The type of this resource.
    pub r#type: Type,

    /// The config used when creating this resource. Use the `r#type` to know how to parse this data.
    pub config: Value,

    /// The data associated with this resource. Use the `r#type` to know how to parse this data.
    pub data: Value,
}

impl Response {
    pub fn into_bytes(self) -> Vec<u8> {
        self.to_bytes()
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        serde_json::to_vec(self).expect("to turn resource into a vec")
    }

    pub fn from_bytes(bytes: Vec<u8>) -> Self {
        serde_json::from_slice(&bytes).expect("to turn bytes into a resource")
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Type {
    Database(database::Type),
    Secrets,
    Persist,
    /// Local provisioner only
    Container,
}

#[derive(
    Clone, Copy, Debug, strum::EnumString, strum::Display, Deserialize, Serialize, Eq, PartialEq,
)]
#[typeshare::typeshare]
// is a flat enum instead of nested enum to allow typeshare
pub enum ResourceTypeBeta {
    #[strum(to_string = "database::shared::postgres")]
    #[serde(rename = "database::shared::postgres")]
    DatabaseSharedPostgres,
    #[strum(to_string = "database::aws_rds::postgres")]
    #[serde(rename = "database::aws_rds::postgres")]
    DatabaseAwsRdsPostgres,
    #[strum(to_string = "database::aws_rds::mysql")]
    #[serde(rename = "database::aws_rds::mysql")]
    DatabaseAwsRdsMysql,
    #[strum(to_string = "database::aws_rds::mariadb")]
    #[serde(rename = "database::aws_rds::mariadb")]
    DatabaseAwsRdsMariaDB,
    /// (Will probably be removed)
    #[strum(to_string = "secrets")]
    #[serde(rename = "secrets")]
    Secrets,
    /// Local provisioner only
    #[strum(to_string = "container")]
    #[serde(rename = "container")]
    Container,
}

impl TryFrom<ResourceTypeBeta> for database::AwsRdsEngine {
    type Error = String;

    fn try_from(value: ResourceTypeBeta) -> Result<Self, Self::Error> {
        Ok(match value {
            ResourceTypeBeta::DatabaseAwsRdsPostgres => Self::Postgres,
            ResourceTypeBeta::DatabaseAwsRdsMysql => Self::MySql,
            ResourceTypeBeta::DatabaseAwsRdsMariaDB => Self::MariaDB,
            other => return Err(format!("Invalid conversion of DB type: {other}")),
        })
    }
}

#[derive(Debug, Error)]
pub enum InvalidResourceType {
    #[error("'{0}' is an unknown database type")]
    Type(String),

    #[error("{0}")]
    Database(String),
}

impl FromStr for Type {
    type Err = InvalidResourceType;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Some((prefix, rest)) = s.split_once("::") {
            match prefix {
                "database" => Ok(Self::Database(
                    database::Type::from_str(rest).map_err(InvalidResourceType::Database)?,
                )),
                _ => Err(InvalidResourceType::Type(prefix.to_string())),
            }
        } else {
            match s {
                "secrets" => Ok(Self::Secrets),
                "persist" => Ok(Self::Persist),
                "container" => Ok(Self::Container),
                _ => Err(InvalidResourceType::Type(s.to_string())),
            }
        }
    }
}

impl Display for Type {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Type::Database(db_type) => write!(f, "database::{db_type}"),
            Type::Secrets => write!(f, "secrets"),
            Type::Persist => write!(f, "persist"),
            Type::Container => write!(f, "container"),
        }
    }
}

// this can be removed when deployers AND r-r no longer hold resources in sqlite state
#[cfg(feature = "sqlx")]
mod _sqlx {
    use std::{borrow::Cow, str::FromStr};

    use sqlx::{
        postgres::{PgArgumentBuffer, PgValueRef},
        sqlite::{SqliteArgumentValue, SqliteValueRef},
        Database, Postgres, Sqlite,
    };

    use super::{ResourceState, Type};

    impl<DB: Database> sqlx::Type<DB> for Type
    where
        str: sqlx::Type<DB>,
    {
        fn type_info() -> <DB as Database>::TypeInfo {
            <str as sqlx::Type<DB>>::type_info()
        }
    }

    impl<'q> sqlx::Encode<'q, Sqlite> for Type {
        fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> sqlx::encode::IsNull {
            args.push(SqliteArgumentValue::Text(Cow::Owned(self.to_string())));

            sqlx::encode::IsNull::No
        }
    }

    impl<'r> sqlx::Decode<'r, Sqlite> for Type {
        fn decode(value: SqliteValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
            let value = <&str as sqlx::Decode<Sqlite>>::decode(value)?;

            Self::from_str(value).map_err(Into::into)
        }
    }

    impl<DB: sqlx::Database> sqlx::Type<DB> for ResourceState
    where
        str: sqlx::Type<DB>,
    {
        fn type_info() -> <DB as sqlx::Database>::TypeInfo {
            <str as sqlx::Type<DB>>::type_info()
        }
    }

    impl<'q> sqlx::Encode<'q, sqlx::Postgres> for ResourceState {
        fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> sqlx::encode::IsNull {
            #[allow(clippy::needless_borrows_for_generic_args)]
            <&str as sqlx::Encode<Postgres>>::encode(&self.to_string(), buf)
        }
    }

    impl<'r> sqlx::Decode<'r, Postgres> for ResourceState {
        fn decode(value: PgValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
            let value = <&str as sqlx::Decode<Postgres>>::decode(value)?;

            let state = ResourceState::from_str(value)?;
            Ok(state)
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn to_string_and_back() {
        let inputs = [
            Type::Database(database::Type::AwsRds(database::AwsRdsEngine::Postgres)),
            Type::Database(database::Type::AwsRds(database::AwsRdsEngine::MySql)),
            Type::Database(database::Type::AwsRds(database::AwsRdsEngine::MariaDB)),
            Type::Database(database::Type::Shared(database::SharedEngine::Postgres)),
            Type::Database(database::Type::Shared(database::SharedEngine::MongoDb)),
            Type::Secrets,
            Type::Persist,
            Type::Container,
        ];

        for input in inputs {
            let actual = Type::from_str(&input.to_string()).unwrap();
            assert_eq!(input, actual, ":{} should map back to itself", input);
        }
    }

    #[test]
    fn to_string_and_back_beta() {
        let inputs = [
            ResourceTypeBeta::DatabaseSharedPostgres,
            ResourceTypeBeta::Secrets,
            ResourceTypeBeta::Container,
        ];

        for input in inputs {
            let actual = ResourceTypeBeta::from_str(&input.to_string()).unwrap();
            assert_eq!(input, actual, ":{} should map back to itself", input);
        }
    }

    #[test]
    fn beta_compat() {
        let inputs = [
            (
                Type::Database(database::Type::Shared(database::SharedEngine::Postgres)),
                ResourceTypeBeta::DatabaseSharedPostgres,
            ),
            (
                Type::Database(database::Type::AwsRds(database::AwsRdsEngine::Postgres)),
                ResourceTypeBeta::DatabaseAwsRdsPostgres,
            ),
            (
                Type::Database(database::Type::AwsRds(database::AwsRdsEngine::MySql)),
                ResourceTypeBeta::DatabaseAwsRdsMysql,
            ),
            (
                Type::Database(database::Type::AwsRds(database::AwsRdsEngine::MariaDB)),
                ResourceTypeBeta::DatabaseAwsRdsMariaDB,
            ),
            (Type::Secrets, ResourceTypeBeta::Secrets),
            (Type::Container, ResourceTypeBeta::Container),
        ];

        for (alpha, beta) in inputs {
            assert_eq!(alpha.to_string(), beta.to_string());
        }
    }
}