Unify backend resources and role bindings in clusters
This commit is contained in:
+48
-98
@@ -2,19 +2,21 @@
|
||||
|
||||
mod catalog;
|
||||
mod driver;
|
||||
mod error;
|
||||
mod format;
|
||||
mod object_storage;
|
||||
mod resource;
|
||||
mod sql;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
pub use catalog::{Resource, ResourceRef};
|
||||
pub use driver::*;
|
||||
pub use error::ConfigError;
|
||||
pub use format::{ConfigFormat, FormatError, ImportFormat};
|
||||
pub use object_storage::ObjectStorage;
|
||||
pub use resource::{ResolvedResource, Resource, ResourceRef};
|
||||
/// Compatibility name for the SQL cluster configuration.
|
||||
pub use sql::SqlCluster as Cluster;
|
||||
pub use sql::{Address, ResolvedSql, SqlCluster, Tls};
|
||||
@@ -22,19 +24,14 @@ pub use sql::{Address, ResolvedSql, SqlCluster, Tls};
|
||||
pub const DOCUMENTS: &str = "documents";
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
#[serde(try_from = "format::DeploymentInput")]
|
||||
pub struct DeploymentConfig {
|
||||
pub deployment: String,
|
||||
pub config_version: String,
|
||||
/// Legacy shorthand for sql.documents.
|
||||
pub cluster: Option<String>,
|
||||
/// Named SQL targets that can be bound to entries in `sql`.
|
||||
pub clusters: BTreeMap<String, SqlCluster>,
|
||||
/// SQL role to named cluster.
|
||||
pub sql: BTreeMap<String, String>,
|
||||
pub object_storage: BTreeMap<String, ObjectStorage>,
|
||||
pub resources: BTreeMap<String, Resource>,
|
||||
/// Role to named resource.
|
||||
pub bindings: BTreeMap<String, String>,
|
||||
pub read_only: bool,
|
||||
pub sqlite_path: Address,
|
||||
}
|
||||
|
||||
impl Default for DeploymentConfig {
|
||||
@@ -42,12 +39,9 @@ impl Default for DeploymentConfig {
|
||||
Self {
|
||||
deployment: "default".into(),
|
||||
config_version: "local".into(),
|
||||
cluster: None,
|
||||
clusters: BTreeMap::new(),
|
||||
sql: BTreeMap::new(),
|
||||
object_storage: BTreeMap::new(),
|
||||
resources: BTreeMap::new(),
|
||||
bindings: BTreeMap::new(),
|
||||
read_only: false,
|
||||
sqlite_path: "bedrock.sqlite3".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,106 +51,62 @@ impl Default for DeploymentConfig {
|
||||
pub struct ResolvedDeployment {
|
||||
pub deployment: String,
|
||||
pub config_version: String,
|
||||
pub sql: BTreeMap<String, ResolvedSql>,
|
||||
pub object_storage: BTreeMap<String, ObjectStorage>,
|
||||
resources: BTreeMap<String, ResolvedResource>,
|
||||
bindings: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
impl ResolvedDeployment {
|
||||
pub fn documents(&self) -> Option<&ResolvedSql> {
|
||||
self.sql.get(DOCUMENTS)
|
||||
pub fn resource(&self, name: &str) -> Option<&ResolvedResource> {
|
||||
self.resources.get(name)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ConfigError {
|
||||
#[error("invalid {0} name")]
|
||||
Name(&'static str),
|
||||
#[error("the selected database cluster is not configured")]
|
||||
UnknownCluster,
|
||||
#[error("a cluster must be selected when clusters are configured")]
|
||||
ClusterRequired,
|
||||
#[error("legacy cluster and sql.documents refer to different resources")]
|
||||
ConflictingDocuments,
|
||||
#[error("invalid object storage configuration")]
|
||||
ObjectStorage,
|
||||
#[error("resource already exists")]
|
||||
DuplicateResource,
|
||||
#[error("resource does not exist")]
|
||||
MissingResource,
|
||||
#[error("resource is referenced by a SQL binding")]
|
||||
ResourceInUse,
|
||||
#[error("a resource update must preserve its category")]
|
||||
ResourceCategory,
|
||||
#[error("the database pool size must be greater than zero")]
|
||||
PoolSize,
|
||||
#[error("SQLite does not support a replica list")]
|
||||
SqliteReplicas,
|
||||
#[error("invalid SQLite file path")]
|
||||
SqlitePath,
|
||||
#[error("invalid {0} database URL")]
|
||||
Url(&'static str),
|
||||
pub fn binding(&self, role: &str) -> Option<&ResolvedResource> {
|
||||
self.resource(self.bindings.get(role)?)
|
||||
}
|
||||
|
||||
pub fn bound_resources(&self) -> impl Iterator<Item = (&str, &ResolvedResource)> {
|
||||
self.bindings
|
||||
.iter()
|
||||
.map(|(role, target)| (role.as_str(), &self.resources[target]))
|
||||
}
|
||||
}
|
||||
|
||||
impl DeploymentConfig {
|
||||
pub fn resolve(&self) -> Result<ResolvedDeployment, ConfigError> {
|
||||
validate_name(&self.deployment, "deployment")?;
|
||||
validate_name(&self.config_version, "config version")?;
|
||||
let mut targets = BTreeMap::new();
|
||||
for (name, cluster) in &self.clusters {
|
||||
validate_name(name, "cluster")?;
|
||||
targets.insert(
|
||||
name.as_str(),
|
||||
cluster.resolve(&self.deployment, Some(name), self.read_only)?,
|
||||
let mut resources = BTreeMap::new();
|
||||
let mut bindings = self.bindings.clone();
|
||||
for (name, resource) in &self.resources {
|
||||
validate_name(name, "resource")?;
|
||||
resources.insert(
|
||||
name.clone(),
|
||||
resource.resolve(&self.deployment, Some(name), self.read_only)?,
|
||||
);
|
||||
}
|
||||
for (name, store) in &self.object_storage {
|
||||
validate_name(name, "object store")?;
|
||||
if self.clusters.contains_key(name) {
|
||||
return Err(ConfigError::DuplicateResource);
|
||||
}
|
||||
store.validate()?;
|
||||
}
|
||||
|
||||
let mut sql = BTreeMap::new();
|
||||
for (role, target) in &self.sql {
|
||||
validate_name(role, "SQL role")?;
|
||||
validate_name(target, "cluster")?;
|
||||
let settings = targets
|
||||
.get(target.as_str())
|
||||
.ok_or(ConfigError::UnknownCluster)?;
|
||||
sql.insert(role.clone(), settings.clone());
|
||||
}
|
||||
if let Some(name) = &self.cluster {
|
||||
validate_name(name, "cluster")?;
|
||||
if self.sql.get(DOCUMENTS).is_some_and(|target| target != name) {
|
||||
return Err(ConfigError::ConflictingDocuments);
|
||||
}
|
||||
let settings = targets
|
||||
.get(name.as_str())
|
||||
.ok_or(ConfigError::UnknownCluster)?;
|
||||
sql.insert(DOCUMENTS.into(), settings.clone());
|
||||
}
|
||||
if sql.is_empty() {
|
||||
if !targets.is_empty() {
|
||||
return Err(ConfigError::ClusterRequired);
|
||||
}
|
||||
let local = SqlCluster {
|
||||
driver: SqlDriver::Sqlite,
|
||||
primary: self.sqlite_path.clone(),
|
||||
replicas: vec![],
|
||||
tls: Tls::Disabled,
|
||||
max_connections: 1,
|
||||
};
|
||||
sql.insert(
|
||||
DOCUMENTS.into(),
|
||||
local.resolve(&self.deployment, None, self.read_only)?,
|
||||
if resources.is_empty() && bindings.is_empty() {
|
||||
let default = Resource::default_documents();
|
||||
resources.insert(
|
||||
"local".into(),
|
||||
default.resolve(&self.deployment, None, self.read_only)?,
|
||||
);
|
||||
bindings.insert(DOCUMENTS.into(), "local".into());
|
||||
}
|
||||
for (role, target) in &bindings {
|
||||
validate_name(role, "role")?;
|
||||
validate_name(target, "resource")?;
|
||||
if !resources.contains_key(target) {
|
||||
return Err(ConfigError::UnknownBinding {
|
||||
role: role.clone(),
|
||||
target: target.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(ResolvedDeployment {
|
||||
deployment: self.deployment.clone(),
|
||||
config_version: self.config_version.clone(),
|
||||
sql,
|
||||
object_storage: self.object_storage.clone(),
|
||||
resources,
|
||||
bindings,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user