|
| 1 | +use k8s_openapi::api::core::v1::EnvVar; |
| 2 | +use schemars::JsonSchema; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +use url::Url; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + builder::pod::container::ContainerBuilder, crd::database::helpers::username_and_password_envs, |
| 8 | +}; |
| 9 | + |
| 10 | +/// TODO docs |
| 11 | +pub trait JDBCDatabaseConnection { |
| 12 | + /// TODO docs |
| 13 | + fn jdbc_connection_details( |
| 14 | + &self, |
| 15 | + unique_database_name: &str, |
| 16 | + ) -> Result<JDBCDatabaseConnectionDetails, crate::crd::database::Error>; |
| 17 | +} |
| 18 | + |
| 19 | +pub struct JDBCDatabaseConnectionDetails { |
| 20 | + /// The Java class name of the driver, e.g. `org.postgresql.Driver` |
| 21 | + pub driver: String, |
| 22 | + |
| 23 | + /// The connection URI (without user and password), e.g. |
| 24 | + /// `jdbc:postgresql://airflow-postgresql:5432/airflow` |
| 25 | + pub connection_uri: Url, |
| 26 | + |
| 27 | + /// The [`EnvVar`] that mounts the credentials Secret and provides the username. |
| 28 | + pub username_env: Option<EnvVar>, |
| 29 | + |
| 30 | + /// The [`EnvVar`] that mounts the credentials Secret and provides the password. |
| 31 | + pub password_env: Option<EnvVar>, |
| 32 | +} |
| 33 | + |
| 34 | +impl JDBCDatabaseConnectionDetails { |
| 35 | + pub fn add_to_container(&self, cb: &mut ContainerBuilder) { |
| 36 | + let env_vars = self.username_env.iter().chain(self.password_env.iter()); |
| 37 | + cb.add_env_vars(env_vars); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// TODO docs |
| 42 | +#[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq, Serialize)] |
| 43 | +#[serde(rename_all = "camelCase")] |
| 44 | +pub struct GenericJDBCDatabaseConnection { |
| 45 | + /// TODO docs |
| 46 | + pub driver: String, |
| 47 | + |
| 48 | + /// TODO docs |
| 49 | + pub uri: Url, |
| 50 | + |
| 51 | + /// TODO docs |
| 52 | + pub credentials_secret: String, |
| 53 | +} |
| 54 | + |
| 55 | +impl JDBCDatabaseConnection for GenericJDBCDatabaseConnection { |
| 56 | + fn jdbc_connection_details( |
| 57 | + &self, |
| 58 | + unique_database_name: &str, |
| 59 | + ) -> Result<JDBCDatabaseConnectionDetails, crate::crd::database::Error> { |
| 60 | + let (username_env, password_env) = |
| 61 | + username_and_password_envs(unique_database_name, &self.credentials_secret); |
| 62 | + |
| 63 | + Ok(JDBCDatabaseConnectionDetails { |
| 64 | + driver: self.driver.clone(), |
| 65 | + connection_uri: self.uri.clone(), |
| 66 | + username_env: Some(username_env), |
| 67 | + password_env: Some(password_env), |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
0 commit comments