-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
redesign the module for better utility
* Support all record types * Suport multiple records per service * Support variable expansion in the record_name and record_content attributes * Simplify configuration by using a nested module
- Loading branch information
Showing
9 changed files
with
293 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
terraform { | ||
required_providers { | ||
dnsimple = { | ||
source = "dnsimple/dnsimple" | ||
version = ">= 1.0" | ||
} | ||
|
||
util = { | ||
source = "poseidon/util" | ||
version = "0.2.2" | ||
} | ||
} | ||
} | ||
|
||
locals { | ||
zone_records_with_defaults = [ | ||
for record in var.zone_records : | ||
{ | ||
"zone_name" = record["zone_name"] == null ? var.defaults["zone_name"] : record["zone_name"], | ||
"record_name" = record["record_name"] | ||
"record_content" = record["record_content"], | ||
"record_type" = record["record_type"] == null ? var.defaults["record_type"] : record["record_type"], | ||
"record_ttl" = record["record_ttl"] == null ? var.defaults["record_ttl"] : record["record_ttl"], | ||
} | ||
] | ||
|
||
zone_records = { | ||
for record in local.zone_records_with_defaults : | ||
join("", [record["record_name"], record["record_type"], record["record_content"], record["zone_name"]]) => record | ||
} | ||
} | ||
|
||
output "defaults" { | ||
value = var.defaults | ||
} | ||
|
||
output "recs" { | ||
value = local.zone_records | ||
} | ||
|
||
data "util_replace" "record_names" { | ||
for_each = local.zone_records | ||
|
||
content = each.value["record_name"] | ||
replacements = { for match in regexall("\\$\\w+_?\\w+", each.value["record_name"]) : match => try(var.consul_service[replace(match, "$", "")], null) } | ||
} | ||
|
||
data "util_replace" "record_contents" { | ||
for_each = local.zone_records | ||
|
||
content = each.value["record_content"] | ||
replacements = { for match in regexall("\\$\\w+_?\\w+", each.value["record_content"]) : match => try(var.consul_service[replace(match, "$", "")], null) } | ||
} | ||
|
||
resource "dnsimple_zone_record" "consul_service_records" { | ||
for_each = local.zone_records | ||
|
||
zone_name = each.value.zone_name | ||
|
||
name = data.util_replace.record_names[each.key].replaced | ||
value = data.util_replace.record_contents[each.key].replaced | ||
|
||
type = each.value.record_type | ||
ttl = each.value.record_ttl | ||
} | ||
|
||
|
||
output "service_record_map" { | ||
value = [ | ||
for record in dnsimple_zone_record.consul_service_records : | ||
"${record.zone_name}:${record.name}:${record.type}:${record.ttl}:${record.value}" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
variable "zone_records" { | ||
type = list(object({ | ||
zone_name = optional(string) | ||
record_name = string | ||
record_content = string | ||
record_type = optional(string) | ||
record_ttl = optional(number) | ||
})) | ||
} | ||
|
||
variable "consul_service" { | ||
type = object({ | ||
id = string | ||
name = string | ||
address = string | ||
port = number | ||
meta = map(string) | ||
tags = list(string) | ||
namespace = string | ||
status = string | ||
|
||
node = string | ||
node_id = string | ||
node_address = string | ||
node_datacenter = string | ||
node_tagged_addresses = map(string) | ||
node_meta = map(string) | ||
}) | ||
|
||
} | ||
|
||
variable "defaults" { | ||
type = object({ | ||
zone_name = optional(string) | ||
record_type = optional(string) | ||
record_ttl = optional(number) | ||
}) | ||
default = { | ||
record_ttl = 3600 | ||
record_type = "A" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
output "service_map" { | ||
value = [for id, s in local.consul_services : | ||
"${id}:${s.record_name}.${s.zone_name}:A:${s.record_ttl}:${s.address}" | ||
] | ||
output "service_records" { | ||
value = module.service_records | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.