Add a versioned backend catalog and YAML deployment configuration
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
use std::{collections::BTreeMap, path::Path};
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{ConfigError, DeploymentConfig, Resource};
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum ConfigFormat {
|
||||
Yaml,
|
||||
Toml,
|
||||
/// Compatibility with previously published deployment files.
|
||||
Json,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum ImportFormat {
|
||||
Catalog(ConfigFormat),
|
||||
/// The bedrock_resources output of terraform output -json.
|
||||
TerraformOutputs,
|
||||
/// A ConfigMap carrying a catalog in data["bedrock-resources.yaml"].
|
||||
KubernetesConfigMap,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum FormatError {
|
||||
#[error("configuration filename must end in .yaml, .yml, .toml or .json")]
|
||||
FileExtension,
|
||||
#[error("could not read the deployment configuration: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
// Parser errors may quote connection strings or credentials.
|
||||
#[error("invalid {0} configuration document")]
|
||||
Parse(&'static str),
|
||||
#[error("could not serialize the deployment configuration as YAML")]
|
||||
Serialize,
|
||||
#[error("import does not contain a Bedrock resource catalog")]
|
||||
MissingCatalog,
|
||||
#[error("publishable deployment configuration requires a valid catalog: {0}")]
|
||||
Config(#[from] ConfigError),
|
||||
}
|
||||
|
||||
impl ConfigFormat {
|
||||
fn parse<T: DeserializeOwned>(self, input: &str) -> Result<T, FormatError> {
|
||||
match self {
|
||||
Self::Yaml => serde_saphyr::from_str(input).map_err(|_| FormatError::Parse("YAML")),
|
||||
Self::Toml => toml::from_str(input).map_err(|_| FormatError::Parse("TOML")),
|
||||
Self::Json => serde_json::from_str(input).map_err(|_| FormatError::Parse("JSON")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DeploymentConfig {
|
||||
pub fn parse(input: &str, format: ConfigFormat) -> Result<Self, FormatError> {
|
||||
format.parse(input)
|
||||
}
|
||||
|
||||
pub fn load(path: impl AsRef<Path>) -> Result<Self, FormatError> {
|
||||
let path = path.as_ref();
|
||||
let format = match path.extension().and_then(|value| value.to_str()) {
|
||||
Some("yaml" | "yml") => ConfigFormat::Yaml,
|
||||
Some("toml") => ConfigFormat::Toml,
|
||||
Some("json") => ConfigFormat::Json,
|
||||
_ => return Err(FormatError::FileExtension),
|
||||
};
|
||||
Self::parse(&std::fs::read_to_string(path)?, format)
|
||||
}
|
||||
|
||||
/// Serializes the configuration, including any credentials in connection strings.
|
||||
pub fn to_yaml(&self) -> Result<String, FormatError> {
|
||||
self.resolve()?;
|
||||
serde_saphyr::to_string(self).map_err(|_| FormatError::Serialize)
|
||||
}
|
||||
|
||||
/// Imports an inventory atomically. Deployment roles are assigned separately.
|
||||
pub fn import_resources(
|
||||
&mut self,
|
||||
input: &str,
|
||||
format: ImportFormat,
|
||||
) -> Result<(), FormatError> {
|
||||
let resources: BTreeMap<String, Resource> = match format {
|
||||
ImportFormat::Catalog(format) => format.parse(input)?,
|
||||
ImportFormat::TerraformOutputs => {
|
||||
let exports: TerraformOutputs = ConfigFormat::Json.parse(input)?;
|
||||
exports
|
||||
.bedrock_resources
|
||||
.ok_or(FormatError::MissingCatalog)?
|
||||
.value
|
||||
}
|
||||
ImportFormat::KubernetesConfigMap => {
|
||||
let manifest: ConfigMap = ConfigFormat::Yaml.parse(input)?;
|
||||
if manifest.kind != "ConfigMap" || manifest.api_version != "v1" {
|
||||
return Err(FormatError::MissingCatalog);
|
||||
}
|
||||
let catalog = manifest
|
||||
.data
|
||||
.get("bedrock-resources.yaml")
|
||||
.ok_or(FormatError::MissingCatalog)?;
|
||||
ConfigFormat::Yaml.parse(catalog)?
|
||||
}
|
||||
};
|
||||
let mut candidate = self.clone();
|
||||
for (name, resource) in resources {
|
||||
candidate.insert_resource(name, resource)?;
|
||||
}
|
||||
*self = candidate;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct TerraformOutputs {
|
||||
bedrock_resources: Option<TerraformOutput>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct TerraformOutput {
|
||||
value: BTreeMap<String, Resource>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct ConfigMap {
|
||||
api_version: String,
|
||||
kind: String,
|
||||
data: BTreeMap<String, String>,
|
||||
}
|
||||
Reference in New Issue
Block a user