use crate::config::{normalize_fqdn, relative_name};
use crate::error::{Error, Result};
use crate::provider::DnsProvider;
use crate::record::{ExistingRecord, Record, RecordKind, RecordValue, txt_key};
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Change {
Noop,
Create,
Update {
id: u64,
prev: RecordValue,
prev_ttl: Option<u32>,
},
Ambiguous {
reason: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Action {
pub record: Record,
pub fqdn: String,
pub change: Change,
}
impl fmt::Display for Action {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (kind, fqdn, value, ttl) = (
self.record.kind(),
&self.fqdn,
&self.record.value,
self.record.ttl,
);
match &self.change {
Change::Noop => write!(f, "no change: {kind} {fqdn} = {value}"),
Change::Create => write!(f, "create {kind} {fqdn} = {value} (ttl={ttl})"),
Change::Update { id, prev, .. } => {
write!(
f,
"update {kind} {fqdn}: {prev} -> {value} (ttl={ttl}) [record_id={id}]"
)
}
Change::Ambiguous { reason } => write!(f, "ambiguous {kind} {fqdn}: {reason}"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Plan {
pub zone: String,
pub actions: Vec<Action>,
}
impl Plan {
pub fn is_noop(&self) -> bool {
self.actions.iter().all(|a| a.change == Change::Noop)
}
pub fn changes(&self) -> impl Iterator<Item = &Action> {
self.actions
.iter()
.filter(|a| matches!(a.change, Change::Create | Change::Update { .. }))
}
pub fn ambiguous(&self) -> impl Iterator<Item = &Action> {
self.actions
.iter()
.filter(|a| matches!(a.change, Change::Ambiguous { .. }))
}
}
impl fmt::Display for Plan {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, a) in self.actions.iter().enumerate() {
if i > 0 {
writeln!(f)?;
}
write!(f, "{a}")?;
}
Ok(())
}
}
pub async fn plan<P: DnsProvider + ?Sized>(
provider: &P,
zone: &str,
want: &[Record],
) -> Result<Plan> {
let zone = zone.trim().trim_end_matches('.').to_string();
if zone.is_empty() {
return Err(Error::InvalidRecord("zone must be set".into()));
}
let mut wanted = Vec::with_capacity(want.len());
for r in want {
r.validate(&zone)?;
let mut r = r.clone();
r.name = relative_name(&zone, &r.name).to_ascii_lowercase();
if wanted
.iter()
.any(|w: &Record| w.name == r.name && w.value.same_data(&r.value))
{
return Err(Error::InvalidRecord(format!(
"{} {} is listed twice",
r.kind(),
normalize_fqdn(&zone, &r.name)
)));
}
if r.kind() == RecordKind::Cname
&& wanted
.iter()
.any(|w: &Record| w.name == r.name && w.kind() == RecordKind::Cname)
{
return Err(Error::InvalidRecord(format!(
"CNAME {} is listed twice with different targets; a name has one CNAME",
normalize_fqdn(&zone, &r.name)
)));
}
wanted.push(r);
}
let mut existing: HashMap<(String, RecordKind), Vec<ExistingRecord>> = HashMap::new();
for r in &wanted {
let key = (r.name.clone(), r.kind());
if let std::collections::hash_map::Entry::Vacant(slot) = existing.entry(key) {
let mut recs = provider.list(&zone, &r.name, r.kind()).await?;
recs.sort_by_key(|e| e.id);
slot.insert(recs);
}
}
let group_of = |r: &Record| (r.name.clone(), r.kind(), r.txt_key().map(str::to_string));
let mut changes: Vec<Option<Change>> = vec![None; wanted.len()];
let mut seen_groups: Vec<(String, RecordKind, Option<String>)> = Vec::new();
for (i, r) in wanted.iter().enumerate() {
let g = group_of(r);
if seen_groups.contains(&g) {
continue;
}
seen_groups.push(g.clone());
let members: Vec<usize> = (i..wanted.len())
.filter(|&j| group_of(&wanted[j]) == g)
.collect();
let pool_all = &existing[&(g.0.clone(), g.1)];
let mut pool: Vec<&ExistingRecord> = pool_all
.iter()
.filter(|e| match (&g.2, &e.value) {
(Some(key), RecordValue::Txt(t)) => txt_key(t) == key,
_ => true,
})
.collect();
let fqdn = normalize_fqdn(&zone, &g.0);
if g.1 != RecordKind::Txt && pool.len() > members.len() {
let reason = format!(
"found {} {} records for {fqdn}, expected at most {}",
pool.len(),
g.1,
members.len()
);
for &j in &members {
changes[j] = Some(Change::Ambiguous {
reason: reason.clone(),
});
}
continue;
}
let mut rest = Vec::new();
for &j in &members {
let w = &wanted[j];
if let Some(pos) = pool.iter().position(|e| e.value.same_data(&w.value)) {
let e = pool.remove(pos);
changes[j] = Some(if e.ttl.is_none_or(|t| t == w.ttl) {
Change::Noop
} else {
Change::Update {
id: e.id,
prev: e.value.clone(),
prev_ttl: e.ttl,
}
});
} else {
rest.push(j);
}
}
if !rest.is_empty() && pool.len() > rest.len() {
let reason = format!(
"found {} other {} records{} for {fqdn}; not sure which to replace",
pool.len(),
g.1,
g.2.as_deref()
.map(|k| format!(" starting {k:?}"))
.unwrap_or_default()
);
for &j in &rest {
changes[j] = Some(Change::Ambiguous {
reason: reason.clone(),
});
}
continue;
}
for (n, &j) in rest.iter().enumerate() {
changes[j] = Some(match pool.get(n) {
Some(e) => Change::Update {
id: e.id,
prev: e.value.clone(),
prev_ttl: e.ttl,
},
None => Change::Create,
});
}
}
let mut cname_names = BTreeSet::new();
let mut other_names: BTreeMap<String, BTreeSet<RecordKind>> = BTreeMap::new();
for r in &wanted {
if r.kind() == RecordKind::Cname {
cname_names.insert(r.name.clone());
} else {
other_names
.entry(r.name.clone())
.or_default()
.insert(r.kind());
}
}
let mut conflicts: HashMap<String, String> = HashMap::new();
for name in &cname_names {
let fqdn = normalize_fqdn(&zone, name);
if let Some(kinds) = other_names.get(name) {
let kinds: Vec<&str> = kinds.iter().map(|k| k.as_str()).collect();
conflicts.insert(
name.clone(),
format!(
"a CNAME and {} records are both wanted at {fqdn}; a CNAME must be alone at its name",
kinds.join("/")
),
);
continue;
}
let others: BTreeSet<String> = provider
.types_at(&zone, name)
.await?
.into_iter()
.map(|t| t.to_ascii_uppercase())
.filter(|t| t != RecordKind::Cname.as_str())
.collect();
if !others.is_empty() {
let others: Vec<String> = others.into_iter().collect();
conflicts.insert(
name.clone(),
format!(
"{fqdn} already has {} records; a CNAME must be alone at its name (nothing is deleted)",
others.join("/")
),
);
}
}
for name in other_names.keys() {
if cname_names.contains(name) {
continue;
}
let cnames = provider.list(&zone, name, RecordKind::Cname).await?;
if let Some(c) = cnames.first() {
let fqdn = normalize_fqdn(&zone, name);
conflicts.insert(
name.clone(),
format!(
"{fqdn} is a CNAME to {}; it cannot also have other records (nothing is deleted)",
c.value.content()
),
);
}
}
for (r, c) in wanted.iter().zip(changes.iter_mut()) {
if let Some(reason) = conflicts.get(&r.name) {
*c = Some(Change::Ambiguous {
reason: reason.clone(),
});
}
}
let actions = wanted
.into_iter()
.zip(changes)
.map(|(record, change)| Action {
fqdn: normalize_fqdn(&zone, &record.name),
record,
change: change.expect("every wanted record is planned"),
})
.collect();
Ok(Plan { zone, actions })
}
pub async fn apply<P: DnsProvider + ?Sized>(provider: &P, plan: &Plan) -> Result<Vec<Action>> {
let reasons: Vec<String> = plan.ambiguous().map(|a| a.to_string()).collect();
if !reasons.is_empty() {
return Err(Error::Ambiguous(reasons.join("; ")));
}
let mut done = Vec::new();
for a in plan.changes() {
match &a.change {
Change::Create => {
provider
.create(&plan.zone, &a.record.name, &a.record.value, a.record.ttl)
.await?
}
Change::Update { id, .. } => {
provider
.update(&plan.zone, *id, &a.record.value, a.record.ttl)
.await?
}
_ => continue,
}
done.push(a.clone());
}
Ok(done)
}