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:
committed by
GitHub
parent
b481d9baff
commit
5001eb49a4
@@ -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(string)">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(object({ name = string type = string ttl = number records = list(string) }))">list(object({...}))</code> | | <code title="">[]</code> |
|
||||
| *recordsets* | None | <code title="map(object({ ttl = number records = list(string) }))">map(object({...}))</code> | | <code title="{} 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\"." }">...</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 validation { condition = contains(["public", "private", "forwarding", "peering", "service-directory"], var.type) error_message = "Zone must be one of 'public', 'private', 'forwarding', 'peering', 'service-directory'." }">...</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> |
|
||||
|
||||
@@ -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
|
||||
]
|
||||
|
||||
@@ -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" {
|
||||
|
||||
Reference in New Issue
Block a user