shuttle_common/models/
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
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[typeshare::typeshare]
pub struct ProvisionResourceRequest {
    /// The type of this resource
    pub r#type: ResourceType,
    /// 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),
}

/// 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,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[typeshare::typeshare]
pub struct ResourceResponse {
    pub r#type: ResourceType,
    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 ResourceListResponse {
    pub resources: Vec<ResourceResponse>,
}

#[derive(
    Clone,
    Copy,
    Debug,
    Deserialize,
    Serialize,
    Eq,
    PartialEq,
    strum::AsRefStr,
    strum::Display,
    strum::EnumString,
)]
#[typeshare::typeshare]
// is a flat enum instead of nested enum to allow typeshare
pub enum ResourceType {
    #[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,
}

#[cfg(test)]
mod test {
    use std::str::FromStr;

    use super::*;

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

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