Skip to content

Commit

Permalink
Merge pull request #129 from DFE-Digital/2134_txt_record_list
Browse files Browse the repository at this point in the history
[2134] Create DNS zones having multiple values TXT records
  • Loading branch information
saliceti authored Nov 8, 2024
2 parents bad5a09 + 83e78d5 commit b72b514
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
1 change: 0 additions & 1 deletion dns/records/tfdocs.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_azure_credentials"></a> [azure\_credentials](#input\_azure\_credentials) | n/a | `any` | `null` | no |
| <a name="input_hosted_zone"></a> [hosted\_zone](#input\_hosted\_zone) | n/a | `map(any)` | `{}` | no |

## Outputs
Expand Down
7 changes: 0 additions & 7 deletions dns/records/variables.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@

variable "azure_credentials" { default = null }

variable "hosted_zone" {
type = map(any)
default = {}
}

locals {
azure_credentials = try(jsondecode(var.azure_credentials), null)
}
41 changes: 39 additions & 2 deletions dns/zones/resources.tf
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,33 @@ resource "azurerm_dns_caa_record" "caa_record_list" {
}

# TXT record

locals {
# Create a list of maps. Each map contains the zone, the TXT record name and value, and other required fields
txt_records = flatten([
for zone_name, zone_cfg in var.hosted_zone : [
for record_name, record_cfg in zone_cfg["txt_records"] : {
for record_name, record_cfg in try(zone_cfg["txt_records"], []) : {
record_name = record_name
zone_name = zone_name
resource_group_name = zone_cfg["resource_group_name"]
value = record_cfg["value"]
}
]
])

# Create a list of maps. Each map contains the zone, the TXT record name and list of values, and other required fields
txt_record_lists = flatten([
for zone_name, zone_cfg in var.hosted_zone : [
for record_name, record_cfg in try(zone_cfg["txt_record_lists"], []) : {
record_name = record_name
zone_name = zone_name
resource_group_name = zone_cfg["resource_group_name"]
txt_record_list = record_cfg
}
]
])
}

# Create TXT records, each one with a record name a single value
resource "azurerm_dns_txt_record" "txt_records" {
for_each = {
for zone in local.txt_records : "${zone.zone_name}.${zone.record_name}" => zone
Expand All @@ -115,3 +128,27 @@ resource "azurerm_dns_txt_record" "txt_records" {
]

}

# Create TXT records, each one with a record name and a list of values
resource "azurerm_dns_txt_record" "txt_record_list" {
for_each = {
for zone in local.txt_record_lists : "${zone.zone_name}.${zone.record_name}" => zone
}

name = each.value.record_name
zone_name = each.value.zone_name
resource_group_name = each.value.resource_group_name
ttl = 300

dynamic "record" {
for_each = toset(each.value.txt_record_list)
content {
value = record.value
}
}

depends_on = [
azurerm_dns_zone.dns_zone
]

}
2 changes: 1 addition & 1 deletion dns/zones/tfdocs.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ No modules.
|------|------|
| [azurerm_dns_caa_record.caa_record_list](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dns_caa_record) | resource |
| [azurerm_dns_caa_record.caa_records](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dns_caa_record) | resource |
| [azurerm_dns_txt_record.txt_record_list](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dns_txt_record) | resource |
| [azurerm_dns_txt_record.txt_records](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dns_txt_record) | resource |
| [azurerm_dns_zone.dns_zone](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dns_zone) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_azure_credentials"></a> [azure\_credentials](#input\_azure\_credentials) | n/a | `any` | `null` | no |
| <a name="input_hosted_zone"></a> [hosted\_zone](#input\_hosted\_zone) | n/a | `map(any)` | `{}` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | n/a | `any` | `null` | no |

Expand Down
6 changes: 0 additions & 6 deletions dns/zones/variables.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

variable "azure_credentials" { default = null }

variable "hosted_zone" {
type = map(any)
default = {}
Expand All @@ -9,7 +7,3 @@ variable "hosted_zone" {
variable "tags" {
default = null
}

locals {
azure_credentials = try(jsondecode(var.azure_credentials), null)
}

0 comments on commit b72b514

Please sign in to comment.