Search code examples
terraformopenstack

Transform Openstack v2 DNS record to get FQDN with Terraform


In Terraform using openstack_v2 provider, handling dns records is really complex, since dns records have a trailing point, and there's no way to easily handle theses cases with Terraform (0.11).

See first comment. I did not specify at first that fqdn can be used with trailing point almost everywhere: browser, terminal, ansible... This use case is when the trailing point causes issues. For example in a system that badly handles fqdn...

Here is an example of a dns-record:

module.runners.openstack_dns_recordset_v2.dns-record.0:
  id = b84346ec-240b-4517-8da5-0715ed196bc2/234acad2-909a-490c-8aec-b9655fa4bc41
  description = 
  name = runner-1.dns-zone.domain.net.
  records.# = 1
  records.0 = 192.168.0.57
  region = RegionOne
  ttl = 3000
  type = A
  zone_id = b84346ec-240b-4517-8da5-0715ed196bc2

Terraform does not provide any way to transform strings in list. How can I do this?


Solution

  • The solution is to use data_null_source that can bear a count value. The count variable is the only way to apply a string transformation to the list values.

    Here is the code sample to handle this use-case. It will remove the last character for each dns record name, allowing us to use the FQDN without the trailing point later.

    data "null_data_source" "fqdns" {
      count = "${var.instance_count}"
      inputs = {
        dns = "${substr(element(openstack_dns_recordset_v2.dns-record.*.name, count.index),0,length(element(openstack_dns_recordset_v2.dns-record.*.name, count.index))-1)}"
      }
    }
    
    output "fqdns" {
      value = ["${data.null_data_source.fqdns.*.outputs.dns}"]
    }
    

    Now we can use the fqdns object. Yay!