1
use serde::Deserialize;
2

            
3
use crate::canyon_database_connector::DatabaseType;
4

            
5
/// ```
6
#[test]
7
fn load_ds_config_from_array() {
8
    #[cfg(feature = "postgres")]
9
    {
10
        const CONFIG_FILE_MOCK_ALT_PG: &str = r#"
11
        [canyon_sql]
12
        datasources = [
13
            {name = 'PostgresDS', auth = { postgresql = { basic = { username = "postgres", password = "postgres" } } }, properties.host = 'localhost', properties.db_name = 'triforce', properties.migrations='enabled' },
14
        ]
15
        "#;
16
        let config: CanyonSqlConfig = toml::from_str(CONFIG_FILE_MOCK_ALT_PG)
17
            .expect("A failure happened retrieving the [canyon_sql] section");
18

            
19
        let ds_0 = &config.canyon_sql.datasources[0];
20

            
21
        assert_eq!(ds_0.name, "PostgresDS");
22
        assert_eq!(ds_0.get_db_type(), DatabaseType::PostgreSql);
23
        assert_eq!(
24
            ds_0.auth,
25
            Auth::Postgres(PostgresAuth::Basic {
26
                username: "postgres".to_string(),
27
                password: "postgres".to_string()
28
            })
29
        );
30
        assert_eq!(ds_0.properties.host, "localhost");
31
        assert_eq!(ds_0.properties.port, None);
32
        assert_eq!(ds_0.properties.db_name, "triforce");
33
        assert_eq!(ds_0.properties.migrations, Some(Migrations::Enabled));
34
    }
35

            
36
    #[cfg(feature = "mssql")]
37
    {
38
        const CONFIG_FILE_MOCK_ALT_MSSQL: &str = r#"
39
        [canyon_sql]
40
        datasources = [
41
            {name = 'SqlServerDS', auth = { sqlserver = { basic = { username = "sa", password = "SqlServer-10" } } }, properties.host = '192.168.0.250.1', properties.port = 3340, properties.db_name = 'triforce2', properties.migrations='disabled' },
42
            {name = 'SqlServerDS', auth = { sqlserver = { integrated = {} } }, properties.host = '192.168.0.250.1', properties.port = 3340, properties.db_name = 'triforce2', properties.migrations='disabled' }
43
        ]
44
        "#;
45
        let config: CanyonSqlConfig = toml::from_str(CONFIG_FILE_MOCK_ALT_MSSQL)
46
            .expect("A failure happened retrieving the [canyon_sql] section");
47

            
48
        let ds_1 = &config.canyon_sql.datasources[0];
49
        let ds_2 = &config.canyon_sql.datasources[1];
50

            
51
        assert_eq!(ds_1.name, "SqlServerDS");
52
        assert_eq!(ds_1.get_db_type(), DatabaseType::SqlServer);
53
        assert_eq!(
54
            ds_1.auth,
55
            Auth::SqlServer(SqlServerAuth::Basic {
56
                username: "sa".to_string(),
57
                password: "SqlServer-10".to_string()
58
            })
59
        );
60
        assert_eq!(ds_1.properties.host, "192.168.0.250.1");
61
        assert_eq!(ds_1.properties.port, Some(3340));
62
        assert_eq!(ds_1.properties.db_name, "triforce2");
63
        assert_eq!(ds_1.properties.migrations, Some(Migrations::Disabled));
64

            
65
        assert_eq!(ds_2.auth, Auth::SqlServer(SqlServerAuth::Integrated));
66
    }
67
    #[cfg(feature = "mysql")]
68
    {
69
        const CONFIG_FILE_MOCK_ALT_MYSQL: &str = r#"
70
        [canyon_sql]
71
        datasources = [
72
            {name = 'MysqlDS', auth = { mysql = { basic = { username = "root", password = "root" } } }, properties.host = '192.168.0.250.1', properties.port = 3340, properties.db_name = 'triforce2', properties.migrations='disabled' }
73
        ]
74
        "#;
75
        let config: CanyonSqlConfig = toml::from_str(CONFIG_FILE_MOCK_ALT_MYSQL)
76
            .expect("A failure happened retrieving the [canyon_sql] section");
77

            
78
        let ds_1 = &config.canyon_sql.datasources[0];
79

            
80
        assert_eq!(ds_1.name, "MysqlDS");
81
        assert_eq!(ds_1.get_db_type(), DatabaseType::MySQL);
82
        assert_eq!(
83
            ds_1.auth,
84
            Auth::MySQL(MySQLAuth::Basic {
85
                username: "root".to_string(),
86
                password: "root".to_string()
87
            })
88
        );
89
        assert_eq!(ds_1.properties.host, "192.168.0.250.1");
90
        assert_eq!(ds_1.properties.port, Some(3340));
91
        assert_eq!(ds_1.properties.db_name, "triforce2");
92
        assert_eq!(ds_1.properties.migrations, Some(Migrations::Disabled));
93
    }
94
}
95
///
96
280
#[derive(Deserialize, Debug, Clone)]
97
pub struct CanyonSqlConfig {
98
    pub canyon_sql: Datasources,
99
}
100

            
101
280
#[derive(Deserialize, Debug, Clone)]
102
pub struct Datasources {
103
    pub datasources: Vec<DatasourceConfig>,
104
}
105

            
106
2184
#[derive(Deserialize, Debug, Clone)]
107
pub struct DatasourceConfig {
108
168
    pub name: String,
109
168
    pub auth: Auth,
110
168
    pub properties: DatasourceProperties,
111
}
112

            
113
impl DatasourceConfig {
114
168
    pub fn get_db_type(&self) -> DatabaseType {
115
168
        match self.auth {
116
            #[cfg(feature = "postgres")]
117
56
            Auth::Postgres(_) => DatabaseType::PostgreSql,
118
            #[cfg(feature = "mssql")]
119
56
            Auth::SqlServer(_) => DatabaseType::SqlServer,
120
            #[cfg(feature = "mysql")]
121
56
            Auth::MySQL(_) => DatabaseType::MySQL,
122
        }
123
168
    }
124
}
125

            
126
840
#[derive(Deserialize, Debug, Clone, PartialEq)]
127
pub enum Auth {
128
    #[serde(alias = "PostgresSQL", alias = "postgresql", alias = "postgres")]
129
    #[cfg(feature = "postgres")]
130
112
    Postgres(PostgresAuth),
131
    #[serde(alias = "SqlServer", alias = "sqlserver", alias = "mssql")]
132
    #[cfg(feature = "mssql")]
133
112
    SqlServer(SqlServerAuth),
134
    #[serde(alias = "MYSQL", alias = "mysql", alias = "MySQL")]
135
    #[cfg(feature = "mysql")]
136
112
    MySQL(MySQLAuth),
137
}
138

            
139
728
#[derive(Deserialize, Debug, Clone, PartialEq)]
140
#[cfg(feature = "postgres")]
141
pub enum PostgresAuth {
142
    #[serde(alias = "Basic", alias = "basic")]
143
56
    Basic { username: String, password: String },
144
}
145

            
146
672
#[derive(Deserialize, Debug, Clone, PartialEq)]
147
#[cfg(feature = "mssql")]
148
pub enum SqlServerAuth {
149
    #[serde(alias = "Basic", alias = "basic")]
150
56
    Basic { username: String, password: String },
151
    #[serde(alias = "Integrated", alias = "integrated")]
152
    Integrated,
153
}
154

            
155
728
#[derive(Deserialize, Debug, Clone, PartialEq)]
156
#[cfg(feature = "mysql")]
157
pub enum MySQLAuth {
158
    #[serde(alias = "Basic", alias = "basic")]
159
56
    Basic { username: String, password: String },
160
}
161

            
162
2184
#[derive(Deserialize, Debug, Clone)]
163
pub struct DatasourceProperties {
164
168
    pub host: String,
165
168
    pub port: Option<u16>,
166
168
    pub db_name: String,
167
168
    pub migrations: Option<Migrations>,
168
}
169

            
170
/// Represents the enabled or disabled migrations for a whole datasource
171
#[derive(Deserialize, Debug, Clone, Copy, PartialEq)]
172
pub enum Migrations {
173
    #[serde(alias = "Enabled", alias = "enabled")]
174
    Enabled,
175
    #[serde(alias = "Disabled", alias = "disabled")]
176
    Disabled,
177
}