Skip to content

Commit efa0b10

Browse files
committed
Module functionality
This allows zones including records to be defined as individual modules when using this provider as a source. The logic and data structure is mostly taken over from mineiros-io/terraform-aws-route53, allowing the management of PowerDNS zones using a similar representation as Route53 ones. Signed-off-by: Georg Pfuetzenreuter <[email protected]>
1 parent 990dc8f commit efa0b10

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

main.tf

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
locals {
2+
zones = var.zones
3+
nameservers = var.nameservers
4+
}
5+
6+
resource "powerdns_zone" "zone" {
7+
for_each = toset(local.zones)
8+
name = each.value
9+
kind = "Native"
10+
nameservers = local.nameservers
11+
}
12+
13+
locals {
14+
records_expanded = {
15+
for i, record in var.records : join("-", compact([
16+
lower(record.type),
17+
try(lower(record.name), ""),
18+
])) => {
19+
type = record.type
20+
name = try(record.name, "")
21+
ttl = try(record.ttl, null)
22+
idx = i
23+
}
24+
}
25+
26+
records_by_name = {
27+
for product in setproduct(local.zones, keys(local.records_expanded)) : "${product[1]}-${product[0]}" => {
28+
zone = powerdns_zone.zone[product[0]].name
29+
type = local.records_expanded[product[1]].type
30+
name = local.records_expanded[product[1]].name
31+
ttl = local.records_expanded[product[1]].ttl
32+
idx = local.records_expanded[product[1]].idx
33+
}
34+
}
35+
36+
records = local.records_by_name
37+
}
38+
39+
resource "powerdns_record" "record" {
40+
for_each = local.records
41+
name = each.value.name == "" ? each.value.zone : join(".", [each.value.name, each.value.zone])
42+
zone = each.value.zone
43+
type = each.value.type
44+
ttl = each.value.ttl
45+
records = can(var.records[each.value.idx].records) ? [for r in var.records[each.value.idx].records :
46+
each.value.type == "TXT" && length(regexall("(\\\"\\\")", r)) == 0 ?
47+
format("\"%s\"", r) : r
48+
] : null
49+
}

variables.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
variable "zones" {
2+
description = "List of zones to configure."
3+
type = list
4+
default = []
5+
}
6+
7+
variable "nameservers" {
8+
description = "List of nameservers to configure in the given zones."
9+
type = list
10+
default = []
11+
}
12+
13+
variable "records" {
14+
description = "List of records to configure in the given zones."
15+
type = any
16+
default = []
17+
}

0 commit comments

Comments
 (0)