refactor: move crdb to own file
Some checks reported errors
continuous-integration/drone/push Build encountered an error

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-05-27 20:57:15 +02:00
parent c2b13dab9f
commit 252d0852ae
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
2 changed files with 132 additions and 136 deletions

View File

@ -1,139 +1,4 @@
pub mod cluster_vars; pub mod cluster_vars;
pub mod crdb_database;
pub mod cuddle_vars; pub mod cuddle_vars;
pub mod vault_secret; pub mod vault_secret;
pub mod crdb_database {
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,
}
}
}
}

View File

@ -0,0 +1,131 @@
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,
}
}
}