locals {
  # Split hostname into DNS zone and record name.
  # "layover.sh"       -> zone = "layover.sh", name = "@"
  # "relay.layover.sh" -> zone = "layover.sh", name = "relay"
  domain_parts = split(".", var.hostname)
  zone         = join(".", slice(local.domain_parts, length(local.domain_parts) - 2, length(local.domain_parts)))
  record_name  = length(local.domain_parts) > 2 ? join(".", slice(local.domain_parts, 0, length(local.domain_parts) - 2)) : "@"
}

resource "gandi_livedns_record" "layover" {
  zone   = local.zone
  name   = local.record_name
  type   = "A"
  ttl    = 300
  values = [aws_eip.layover.public_ip]
}
