use std::path::PathBuf;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use auths_id::ports::registry::{RegistryError, ValidatedTenantId};
#[derive(Debug, Clone)]
pub struct RegistryConfig {
pub base_path: PathBuf,
pub tenant_id: Option<ValidatedTenantId>,
}
impl RegistryConfig {
pub fn single_tenant(base_path: impl Into<PathBuf>) -> Self {
Self {
base_path: base_path.into(),
tenant_id: None,
}
}
pub fn for_tenant(
base_path: impl Into<PathBuf>,
tenant_id: impl Into<String>,
) -> Result<Self, RegistryError> {
let validated = ValidatedTenantId::new(tenant_id)?;
Ok(Self {
base_path: base_path.into(),
tenant_id: Some(validated),
})
}
pub fn resolve_repo_path(&self) -> PathBuf {
match &self.tenant_id {
None => self.base_path.clone(),
Some(tid) => {
let s: &str = tid.as_ref();
self.base_path.join("tenants").join(s)
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TenantStatus {
Active,
Suspended,
Deprovisioned,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TenantMetadata {
pub version: u32,
pub tenant_id: String,
pub created_at: DateTime<Utc>,
pub status: TenantStatus,
pub plan: Option<String>,
}
impl TenantMetadata {
#[allow(clippy::disallowed_methods)] pub fn new_active(tenant_id: impl Into<String>) -> Self {
Self {
version: 1,
tenant_id: tenant_id.into(),
created_at: Utc::now(),
status: TenantStatus::Active,
plan: None,
}
}
}