mod txt;
pub use txt::TxtRegistry;
use itertools::Itertools;
#[cfg(test)]
use mockall::automock;
use std::net::{Ipv4Addr, Ipv6Addr};
use thiserror::Error;
#[cfg_attr(test, automock)]
pub trait ARegistry {
fn enable_dry_run(&mut self) -> Result<(), RegistryError>;
fn set_tenant(&mut self, tenant: String);
fn all_domains(&self) -> Vec<Domain>;
fn owned_domains(&self) -> Vec<Domain> {
self.all_domains()
.into_iter()
.filter(|d| matches!(d.ownership(), Ownership::Owned))
.collect_vec()
}
fn taken_domains(&self) -> Vec<Domain> {
self.all_domains()
.into_iter()
.filter(|d| matches!(d.ownership(), Ownership::Taken))
.collect_vec()
}
fn available_domains(&self) -> Vec<Domain> {
self.all_domains()
.into_iter()
.filter(|d| matches!(d.ownership(), Ownership::Available))
.collect_vec()
}
#[allow(clippy::needless_lifetimes)] fn claim(&mut self, name: &str) -> Result<(), RegistryError>;
#[allow(clippy::needless_lifetimes)] fn release(&mut self, name: &str) -> Result<(), RegistryError>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Domain {
pub name: String,
pub a: Vec<Ipv4Addr>,
pub aaaa: Vec<Ipv6Addr>,
pub txt: Vec<String>,
#[cfg(test)]
pub a_ownership: Ownership,
#[cfg(not(test))]
a_ownership: Ownership,
}
impl Domain {
pub fn ownership(&self) -> Ownership {
self.a_ownership
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[doc(hidden)]
pub enum Ownership {
Owned,
Taken,
Available,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
pub enum RegistryError {
#[error("The selected registry does not support dry-run mode")]
DryRunNotSupported,
#[error("Could not claim domain {domain:?}: {reason:?}")]
ClaimError { domain: String, reason: String },
#[error("Could not release domain {domain:?}: {reason:?}")]
ReleaseError { domain: String, reason: String },
#[error("Internal registry Error: `{0}`")]
Internal(String),
}
impl From<String> for RegistryError {
fn from(s: String) -> Self {
RegistryError::Internal(s)
}
}