Some checks reported errors
continuous-integration/drone/push Build encountered an error
Signed-off-by: kjuulh <contact@kjuulh.io>
132 lines
3.5 KiB
Rust
132 lines
3.5 KiB
Rust
use std::path::Path;
|
|
|
|
use minijinja::{value::Object, Value};
|
|
|
|
use crate::{catalog::cuddle_vars::CuddleVariable, Component};
|
|
|
|
use super::cuddle_vars::{load_cuddle_file, CuddleVariables};
|
|
|
|
pub struct CockroachDB {
|
|
variables: CuddleVariables,
|
|
}
|
|
|
|
impl CockroachDB {
|
|
pub async fn new(path: &Path) -> anyhow::Result<Self> {
|
|
let variables = load_cuddle_file(path).await?;
|
|
|
|
Ok(Self { variables })
|
|
}
|
|
}
|
|
|
|
impl Component for CockroachDB {
|
|
fn name(&self) -> String {
|
|
"cuddle/crdb".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("crdb"))
|
|
.and_then(|o| match o {
|
|
CuddleVariable::String(o) => {
|
|
if o == "true" {
|
|
Some(true)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
_ => None,
|
|
})
|
|
{
|
|
return Some(Ok(minijinja::Value::from_object(CockroachDBValues {
|
|
name: self.name(),
|
|
enabled: true,
|
|
})));
|
|
}
|
|
|
|
Some(Ok(minijinja::Value::from_object(CockroachDBValues {
|
|
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("crdb"))
|
|
.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#"
|
|
apiVersion: v1
|
|
kind: ConfigMap
|
|
metadata:
|
|
name: {{ vars.cuddle_crdb.file_name(vars.cuddle_vars.service) }}
|
|
namespace: {{ vars.cluster_vars.namespace }}
|
|
data:
|
|
DATABASE_URL: postgresql://root@{{environment}}-cluster:26257/{{ vars.cuddle_vars.service | replace("-", "_") }}
|
|
"#
|
|
.into(),
|
|
)));
|
|
}
|
|
|
|
None
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
struct CockroachDBValues {
|
|
name: String,
|
|
enabled: bool,
|
|
}
|
|
|
|
impl Object for CockroachDBValues {
|
|
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("DATABASE_URL")),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|