Skip to content

Module functionality #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
terraform-provider-dns
terraform-provider-powerdns

*.dll
*.exe
Expand Down
46 changes: 46 additions & 0 deletions examples/zones/example_com.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module "example_com" {
source = "../../terraform-provider-powerdns"
zones = [
"example.com.",
]

soa_edit_api = "INCREASE"

# preferably declare NS records instead of this
# https://github.com/pan-net/terraform-provider-powerdns/issues/63
nameservers = [
"ns1.example.com.",
"ns2.example.com.",
]

records = [
{
type = "SOA"
ttl = 43200
rname = "admin.opensuse.org."
refresh = 7200
retry = 600
expire = 1209600
minimum = 6400
# this can be used to set an initial serial number for new zones
# serial number changes to existing zones will be ignored, the user is expected to use SOA-EDIT-API
serial = 1
},
{
type = "SOA",
ttl = 300,
records = [
"ns1.example.com. hostmaster.example.com. 0 10800 3600 604800 3600"
]
},
{
name = "www",
type = "AAAA",
ttl = 300,
records = [
"::1",
]
}
]
}

13 changes: 13 additions & 0 deletions examples/zones/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_providers {
powerdns = {
source = "pan-net/powerdns"
#version = "1.5.0"
}
}
}

provider "powerdns" {
api_key = var.pdns_api_key
server_url = var.pdns_server_url
}
6 changes: 6 additions & 0 deletions examples/zones/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
variable "pdns_api_key" {
type = string
}
variable "pdns_server_url" {
type = string
}
120 changes: 120 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
locals {
zones = var.zones
nameservers = var.nameservers
nameservers_records_data = flatten([ for r in var.records : [ for rd in r.records : rd ] if r.type == "NS" ])
non_soa_records = [ for r in var.records : r if r.type != "SOA" ]
soa_records = [ for r in var.records : r if r.type == "SOA" ]
}

resource "powerdns_zone" "zone" {
for_each = toset(local.zones)
name = each.value
kind = "Native"
nameservers = length(var.nameservers) == 0 ? local.nameservers_records_data : var.nameservers
soa_edit_api = var.soa_edit_api

lifecycle {
ignore_changes = [
# https://github.com/pan-net/terraform-provider-powerdns/issues/63
# users of the module are expected to use NS records for tracking nameservers
nameservers,
]
}
}

locals {
records_expanded = {
for i, record in local.non_soa_records : join("-", compact([
lower(record.type),
try(lower(record.name), ""),
])) => {
type = record.type
name = try(record.name, "")
ttl = try(record.ttl, null)
idx = i
}
}

records_expanded_soa = {
for i, record in local.soa_records : join("-", compact([
lower(record.type),
try(lower(record.name), ""),
])) => {
type = record.type
name = try(record.name, "")
ttl = try(record.ttl, null)
mname = try(record.mname, element(local.nameservers_records_data, 0)),
rname = record.rname,
serial = try(record.serial, 0),
refresh = record.refresh,
retry = record.retry,
expire = record.expire,
minimum = record.minimum,
idx = i
}
}

records_by_name = {
for product in setproduct(local.zones, keys(local.records_expanded)) : "${product[1]}-${product[0]}" => {
zone = powerdns_zone.zone[product[0]].name
type = local.records_expanded[product[1]].type
name = local.records_expanded[product[1]].name
ttl = local.records_expanded[product[1]].ttl
idx = local.records_expanded[product[1]].idx
}
}

records_by_name_soa = {
for product in setproduct(local.zones, keys(local.records_expanded_soa)) : "${product[1]}-${product[0]}" => {
zone = powerdns_zone.zone[product[0]].name
type = local.records_expanded_soa[product[1]].type
name = local.records_expanded_soa[product[1]].name
ttl = local.records_expanded_soa[product[1]].ttl
mname = local.records_expanded_soa[product[1]].mname,
rname = local.records_expanded_soa[product[1]].rname,
serial = local.records_expanded_soa[product[1]].serial,
refresh = local.records_expanded_soa[product[1]].refresh,
retry = local.records_expanded_soa[product[1]].retry,
expire = local.records_expanded_soa[product[1]].expire,
minimum = local.records_expanded_soa[product[1]].minimum,
idx = local.records_expanded_soa[product[1]].idx
}
}

records = local.records_by_name
records_soa = local.records_by_name_soa
}

resource "powerdns_record_soa" "record_soa" {
for_each = local.records_soa
name = each.value.name == "" ? each.value.zone : join(".", [each.value.name, each.value.zone])
zone = each.value.zone
type = each.value.type
ttl = each.value.ttl
mname = each.value.mname
rname = each.value.rname
serial = each.value.serial
refresh = each.value.refresh
retry = each.value.retry
expire = each.value.expire
minimum = each.value.minimum

lifecycle {
ignore_changes = [
serial,
]
}

}

resource "powerdns_record" "record" {
for_each = local.records
name = each.value.name == "" ? each.value.zone : join(".", [each.value.name, each.value.zone])
zone = each.value.zone
type = each.value.type
ttl = each.value.ttl
records = can(local.non_soa_records[each.value.idx].records) ? [for r in local.non_soa_records[each.value.idx].records :
each.value.type == "TXT" && length(regexall("(\\\"\\\")", r)) == 0 ?
format("\"%s\"", r) : r
] : null
}
1 change: 1 addition & 0 deletions powerdns/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"powerdns_zone": resourcePDNSZone(),
"powerdns_record": resourcePDNSRecord(),
"powerdns_record_soa": resourcePDNSRecordSOA(),
},

ConfigureFunc: providerConfigure,
Expand Down
Loading