use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::{http::ApiClient, resources::DatabaseResource};
#[derive(Debug, Serialize, Deserialize)]
pub struct DatabaseMetrics {
pub date: DateTime<Utc>,
pub cpu: f32,
pub ram: f32,
pub net: [u32; 2],
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DatabaseType {
Redis,
Postgres,
Mongo,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DatabaseInfo {
pub id: String,
pub name: String,
pub owner: String,
pub cluster: String,
pub ram: u32,
#[serde(rename = "type")]
pub db_type: String,
pub port: u32,
pub created_at: DateTime<Utc>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DatabaseSummary {
pub id: String,
pub name: String,
pub ram: u32,
#[serde(rename = "type")]
pub db_type: String,
pub cluster: String,
pub created_at: DateTime<Utc>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Database {
pub id: String,
pub name: String,
pub memory: u32,
pub cpu: u8,
#[serde(rename = "type")]
pub db_type: String,
pub password: String,
pub certificate: String,
pub connection_url: String,
}
pub enum CredentialType {
Certificate,
Password,
}
impl CredentialType {
pub fn as_str(&self) -> &str {
match self {
CredentialType::Certificate => "certificate",
CredentialType::Password => "password",
}
}
}
pub struct Credential {
pub credential_type: CredentialType,
pub value: String,
}
impl Database {
pub fn into_resource(&self, api: ApiClient) -> DatabaseResource {
DatabaseResource::new(api, &self.id)
}
}
#[cfg(test)]
mod tests {
use super::Database;
use crate::http::ApiClient;
#[test]
fn into_resource_binds_correct_id() {
unsafe { std::env::set_var("API_TOKEN", "test") };
let db = Database {
id: "db-abc".to_string(),
name: "test".to_string(),
memory: 512,
cpu: 1,
db_type: "postgres".to_string(),
password: "pass".to_string(),
certificate: "cert".to_string(),
connection_url: "postgres://localhost/test".to_string(),
};
let resource = db.into_resource(ApiClient::new());
assert_eq!(resource.id, "db-abc");
}
}