All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
147 lines
3.9 KiB
Rust
147 lines
3.9 KiB
Rust
use std::path::Path;
|
|
|
|
use minijinja::{value::Object, Value};
|
|
|
|
use crate::Component;
|
|
|
|
use super::cuddle_vars::{load_cuddle_file, CuddleVariable, CuddleVariables};
|
|
|
|
pub struct PostgresDatabase {
|
|
variables: CuddleVariables,
|
|
}
|
|
|
|
impl PostgresDatabase {
|
|
pub async fn new(path: &Path) -> anyhow::Result<Self> {
|
|
let variables = load_cuddle_file(path).await?;
|
|
|
|
Ok(Self { variables })
|
|
}
|
|
}
|
|
|
|
impl Component for PostgresDatabase {
|
|
fn name(&self) -> String {
|
|
"cuddle/postgres".into()
|
|
}
|
|
|
|
fn render_value(
|
|
&self,
|
|
environment: &str,
|
|
_value: &serde_yaml::Value,
|
|
) -> Option<anyhow::Result<minijinja::Value>> {
|
|
if let Some(true) = self
|
|
.variables
|
|
.0
|
|
.get("database")
|
|
.and_then(|v| match v {
|
|
CuddleVariable::Object(o) => Some(o),
|
|
_ => None,
|
|
})
|
|
.and_then(|o| o.0.get("postgres"))
|
|
.and_then(|o| match o {
|
|
CuddleVariable::String(o) => {
|
|
if o == "true" {
|
|
Some(true)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
_ => None,
|
|
})
|
|
{
|
|
return Some(Ok(minijinja::Value::from_object(PostgresDatabaseValues {
|
|
name: self.name(),
|
|
enabled: true,
|
|
})));
|
|
}
|
|
|
|
Some(Ok(minijinja::Value::from_object(PostgresDatabaseValues {
|
|
name: self.name(),
|
|
enabled: false,
|
|
})))
|
|
}
|
|
|
|
fn render(
|
|
&self,
|
|
environment: &str,
|
|
_value: &serde_yaml::Value,
|
|
) -> Option<anyhow::Result<(String, String)>> {
|
|
if let Some(true) = self
|
|
.variables
|
|
.0
|
|
.get("database")
|
|
.and_then(|v| match v {
|
|
CuddleVariable::Object(o) => Some(o),
|
|
_ => None,
|
|
})
|
|
.and_then(|o| o.0.get("postgres"))
|
|
.and_then(|o| match o {
|
|
CuddleVariable::String(o) => {
|
|
if o == "true" {
|
|
Some(true)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
_ => None,
|
|
})
|
|
{
|
|
return Some(Ok((
|
|
format!("{}.yaml", self.name().replace("/", "-")),
|
|
r#"
|
|
{%- if environment == "dev" %}
|
|
{%- set port = 5433 %}
|
|
{%- else %}
|
|
{%- set port = 5432 %}
|
|
{%- endif %}
|
|
apiVersion: v1
|
|
kind: ConfigMap
|
|
metadata:
|
|
name: {{ vars.cuddle_postgres.file_name(vars.cuddle_vars.service) }}
|
|
namespace: {{ vars.cluster_vars.namespace }}
|
|
data:
|
|
DATABASE_TYPE: postgresql
|
|
DATABASE_HOST: {{ environment }}.postgresql.kjuulh.app
|
|
DATABASE_PORT: {{ port }}
|
|
DATABASE_USER: {{ vars.cuddle_vars.service | replace("-", "_") }}
|
|
DATABASE_DB: {{ vars.cuddle_vars.service | replace("-", "_") }}
|
|
|
|
"#
|
|
.into(),
|
|
)));
|
|
}
|
|
|
|
None
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
struct PostgresDatabaseValues {
|
|
name: String,
|
|
enabled: bool,
|
|
}
|
|
|
|
impl Object for PostgresDatabaseValues {
|
|
fn get_value(self: &std::sync::Arc<Self>, key: &minijinja::Value) -> Option<minijinja::Value> {
|
|
let name = self.name.clone();
|
|
match key.as_str()? {
|
|
"has_values" => {
|
|
if self.enabled {
|
|
Some(minijinja::Value::from_serialize(true))
|
|
} else {
|
|
Some(minijinja::Value::from_serialize(false))
|
|
}
|
|
}
|
|
"file_name" => Some(Value::from_function(move |file_name: String| {
|
|
format!("{}-{}", file_name, name.replace("/", "-"))
|
|
})),
|
|
"env" => Some(Value::from_serialize(vec![
|
|
"DATABASE_HOST",
|
|
"DATABASE_PORT",
|
|
"DATABASE_USER",
|
|
"DATABASE_DB",
|
|
])),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|