Skip to content

Commit 68066ed

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 68066ed

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

main.tf

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

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
variable "name" {
2+
description = "Name of the zone or list of zone names."
3+
type = any
4+
5+
default = null
6+
}
7+
8+
variable "records" {
9+
description = "List of records to create in the zone."
10+
type = any
11+
default = []
12+
}

0 commit comments

Comments
 (0)