denoland / terraform-provider-deno

Terraform provider for hosted Deno APIs
Mozilla Public License 2.0
15 stars 2 forks source link

fix(domain): a little more ergonomic DNS record configuration #39

Open magurotuna opened 11 months ago

magurotuna commented 11 months ago

This commit adds dns_record_a, dns_record_aaaa, and dns_record_cname fields to the output of deno_domain resource so that the following steps where DNS records are added can reference them by the record types like cname instead of indexes.

Currently a typical configuration looks like this, where DNS records registration uses indexes such as [0] to get values to be set. This works, but definitely is not best because the length of dns_records is unclear from the type definition, plus we cannot tell which record corresponds to which record type.

# Add a new domain to your organization.
resource "deno_domain" "example" {
  domain = "foo.example.com"
}

# Add DNS records to the nameserver.
resource "cloudflare_record" "my_record_0" {
  zone_id = "<put your zone ID>"
  name    = deno_domain.example.dns_records[0].name
  type    = upper(deno_domain.example.dns_records[0].type)
  value   = deno_domain.example.dns_records[0].content
  proxied = false
  ttl     = 120
}

resource "cloudflare_record" "my_record_1" {
  zone_id = "<put your zone ID>"
  name    = deno_domain.example.dns_records[1].name
  type    = upper(deno_domain.example.dns_records[1].type)
  value   = deno_domain.example.dns_records[1].content
  proxied = false
  ttl     = 120
}

resource "cloudflare_record" "my_record_2" {
  zone_id = "<put your zone ID>"
  name    = deno_domain.example.dns_records[2].name
  type    = upper(deno_domain.example.dns_records[2].type)
  value   = deno_domain.example.dns_records[2].content
  proxied = false
  ttl     = 120
}

With this commit, these issues are resolved and now we can write like below. One can also choose not to use for_each if they wish.

# Add a new domain to your organization.
resource "deno_domain" "example" {
  domain = "foo.example.com"
}

# Add DNS records to the nameserver.
resource "cloudflare_record" "dns_record" {
  for_each = {
    A     = deno_domain.example.dns_record_a
    AAAA  = deno_domain.example.dns_record_aaaa
    CNAME = deno_domain.example.dns_record_cname
  }

  zone_id = local.zone_id
  name    = each.value.name
  type    = upper(each.key)
  value   = each.value.content
  proxied = false
  ttl     = 120
}

The old attribute dns_record is now marked as deprecated but remains unchanged for compatibility. We may delete it before stabilizing the provider.

Fixes #17