Skip to content

Commit d258d44

Browse files
committed
Add records submodule
1 parent f2a84b1 commit d258d44

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

modules/records/main.tf

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
variable "primary" {
2+
type = string
3+
}
4+
5+
variable "domains" {
6+
type = list(string)
7+
}
8+
9+
output "mapped_zones" {
10+
value = local.mapped_zones
11+
}
12+
13+
output "zones" {
14+
value = local.zones
15+
}
16+
17+
output "redirects" {
18+
value = [
19+
for domain in var.domains : domain if domain != var.primary
20+
]
21+
}
22+
23+
output "domains" {
24+
value = var.domains
25+
}
26+
27+
locals {
28+
split_domains = [for domain in var.domains : split(".", domain)]
29+
_zones = toset([
30+
for domain in local.split_domains :
31+
join(".", slice(domain, length(domain) - 2, length(domain)))
32+
if length(domain) >= 2
33+
])
34+
_mapped_zones = {
35+
for zone in local._zones :
36+
zone => [
37+
for domain in local.split_domains :
38+
join(".", domain) if join(".", slice(domain, length(domain) - 2, length(domain))) == zone
39+
]
40+
}
41+
mapped_zones = {
42+
for zone, domains in local._mapped_zones :
43+
zone => {
44+
CNAME = [for domain in domains : domain if domain != zone],
45+
A = [for domain in domains : domain if domain == zone]
46+
}
47+
}
48+
zones = flatten([
49+
for zone, record_types in local.mapped_zones :
50+
[
51+
for record_type, records in record_types :
52+
[
53+
for record in records :
54+
{
55+
"zone" = zone,
56+
"record_type" = record_type
57+
"record" = record
58+
}
59+
]
60+
]
61+
])
62+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
run "parses_variables" {
2+
variables {
3+
primary = "www.example.com"
4+
domains = [
5+
"www.example.com",
6+
"foo.example.com",
7+
"bar.example.com",
8+
"example.com",
9+
"www.example.org",
10+
"example.net"
11+
]
12+
}
13+
14+
assert {
15+
condition = output.mapped_zones == {
16+
"example.com" = {
17+
a = ["example.com"]
18+
cname = [
19+
"www.example.com",
20+
"foo.example.com",
21+
"bar.example.com",
22+
]
23+
}
24+
"example.net" = {
25+
a = ["example.net"]
26+
cname = []
27+
}
28+
"example.org" = {
29+
a = [],
30+
cname = ["www.example.org"]
31+
}
32+
}
33+
error_message = "Mapped zones output should split input domains by top-level domain, then by CNAME and A records"
34+
}
35+
36+
assert {
37+
condition = output.zones == [
38+
{
39+
record = "example.com"
40+
record_type = "a"
41+
zone = "example.com"
42+
},
43+
{
44+
record = "www.example.com"
45+
record_type = "cname"
46+
zone = "example.com"
47+
},
48+
{
49+
record = "foo.example.com"
50+
record_type = "cname"
51+
zone = "example.com"
52+
},
53+
{
54+
record = "bar.example.com"
55+
record_type = "cname"
56+
zone = "example.com"
57+
},
58+
{
59+
record = "example.net"
60+
record_type = "a"
61+
zone = "example.net"
62+
},
63+
{
64+
record = "www.example.org"
65+
record_type = "cname"
66+
zone = "example.org"
67+
}
68+
]
69+
error_message = "Zones output should be flat list of { zone, record_type, record } objects"
70+
}
71+
72+
assert {
73+
condition = output.domains == var.domains
74+
error_message = "Domains output should pass domains input directly"
75+
}
76+
77+
assert {
78+
condition = !contains(output.redirects, var.primary)
79+
error_message = "Redirects output should exclude primary input"
80+
}
81+
}
82+
83+
run "empty_output_is_ok" {
84+
variables {
85+
primary = "www.example.com"
86+
domains = ["www.example.com"]
87+
}
88+
89+
assert {
90+
condition = output.mapped_zones == {
91+
"example.com" = {
92+
cname = ["www.example.com"],
93+
a = []
94+
}
95+
}
96+
error_message = "Mapped zones output should split input domains by top-level domain, then by CNAME and A records"
97+
}
98+
99+
assert {
100+
condition = output.zones == [
101+
{
102+
zone = "example.com",
103+
record_type = "cname",
104+
record = "www.example.com"
105+
}
106+
]
107+
error_message = "Zones output should be flat list of { zone, record_type, record } objects"
108+
}
109+
110+
assert {
111+
condition = length(output.redirects) == 0
112+
error_message = "Redirects output should be empty if only one domain is provided"
113+
}
114+
}

0 commit comments

Comments
 (0)