Make dns module resilient to dynamic values (#317)

* refactor module and fix tests

* account for wildcard records

* account for empty recordset names

* align tests

* align networking end to end examples

* fix behaviour with wildcard and empty names

* Update main.tf

* fix dumb online edit :)
This commit is contained in:
Ludovico Magnocavallo
2021-10-04 18:59:14 +02:00
committed by GitHub
parent b481d9baff
commit 5001eb49a4
15 changed files with 88 additions and 88 deletions

View File

@@ -16,9 +16,9 @@ module "private-dns" {
name = "test-example"
domain = "test.example."
client_networks = [var.vpc.self_link]
recordsets = [
{ name = "localhost", type = "A", ttl = 300, records = ["127.0.0.1"] }
]
recordsets = {
"A localhost" = { type = "A", ttl = 300, records = ["127.0.0.1"] }
}
}
# tftest:modules=1:resources=2
```
@@ -68,7 +68,7 @@ module "private-dns" {
| *dnssec_config* | DNSSEC configuration: kind, non_existence, state. | <code title="">any</code> | | <code title="">{}</code> |
| *forwarders* | Map of {IPV4_ADDRESS => FORWARDING_PATH} for 'forwarding' zone types. Path can be 'default', 'private', or null for provider default. | <code title="map&#40;string&#41;">map(string)</code> | | <code title="">{}</code> |
| *peer_network* | Peering network self link, only valid for 'peering' zone types. | <code title="">string</code> | | <code title="">null</code> |
| *recordsets* | List of DNS record objects to manage. | <code title="list&#40;object&#40;&#123;&#10;name &#61; string&#10;type &#61; string&#10;ttl &#61; number&#10;records &#61; list&#40;string&#41;&#10;&#125;&#41;&#41;">list(object({...}))</code> | | <code title="">[]</code> |
| *recordsets* | None | <code title="map&#40;object&#40;&#123;&#10;ttl &#61; number&#10;records &#61; list&#40;string&#41;&#10;&#125;&#41;&#41;">map(object({...}))</code> | | <code title="&#123;&#125;&#10;validation &#123;&#10;condition &#61; alltrue&#40;&#91;&#10;for k, v in var.recordsets &#61;&#61; null &#63; &#123;&#125; : var.recordsets :&#10;length&#40;split&#40;&#34; &#34;, k&#41;&#41; &#61;&#61; 2&#10;&#93;&#41;&#10;error_message &#61; &#34;Recordsets must have keys in the format &#92;&#34;type name&#92;&#34;.&#34;&#10;&#125;">...</code> |
| *service_directory_namespace* | Service directory namespace id (URL), only valid for 'service-directory' zone types. | <code title="">string</code> | | <code title="">null</code> |
| *type* | Type of zone to create, valid values are 'public', 'private', 'forwarding', 'peering', 'service-directory'. | <code title="">string</code> | | <code title="private&#10;validation &#123;&#10;condition &#61; contains&#40;&#91;&#34;public&#34;, &#34;private&#34;, &#34;forwarding&#34;, &#34;peering&#34;, &#34;service-directory&#34;&#93;, var.type&#41;&#10;error_message &#61; &#34;Zone must be one of &#39;public&#39;, &#39;private&#39;, &#39;forwarding&#39;, &#39;peering&#39;, &#39;service-directory&#39;.&#34;&#10;&#125;">...</code> |
| *zone_create* | Create zone. When set to false, uses a data source to reference existing zone. | <code title="">bool</code> | | <code title="">true</code> |

View File

@@ -15,9 +15,10 @@
*/
locals {
recordsets = var.recordsets == null ? {} : {
for record in var.recordsets :
join("/", [record.name, record.type]) => record
_recordsets = var.recordsets == null ? {} : var.recordsets
recordsets = {
for key, attrs in local._recordsets :
key => merge(attrs, zipmap(["type", "name"], split(" ", key)))
}
zone = (
var.zone_create
@@ -152,10 +153,18 @@ resource "google_dns_record_set" "cloud-static-records" {
)
project = var.project_id
managed_zone = var.name
name = each.value.name != "" ? "${each.value.name}.${var.domain}" : var.domain
type = each.value.type
ttl = each.value.ttl
rrdatas = each.value.records
name = (
each.value.name == ""
? var.domain
: (
substr(each.value.name, -1, 1) == "."
? each.value.name
: "${each.value.name}.${var.domain}"
)
)
type = each.value.type
ttl = each.value.ttl
rrdatas = each.value.records
depends_on = [
google_dns_managed_zone.non-public, google_dns_managed_zone.public
]

View File

@@ -76,14 +76,19 @@ variable "project_id" {
}
variable "recordsets" {
type = list(object({
name = string
type = string
description = "Map of DNS recordsets in \"type name\" => {ttl, [records]} format."
type = map(object({
ttl = number
records = list(string)
}))
description = "List of DNS record objects to manage."
default = []
default = {}
validation {
condition = alltrue([
for k, v in var.recordsets == null ? {} : var.recordsets :
length(split(" ", k)) == 2
])
error_message = "Recordsets must have keys in the format \"type name\"."
}
}
variable "service_directory_namespace" {