forked from Invicton-Labs/terraform-null-deepmerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
62 lines (56 loc) · 2.34 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
locals {
// Find the greatest depth through the maps
greatest_depth = max(concat([flatten([
for mod in local.modules:
concat([flatten([
for i in range(0, length(var.maps)):
[
for f in mod[i].fields:
length(f["path"])
]
])]...)
])]...)...)
// For each input map, convert it to a single-level map with a unique key for every nested value
fields_json = [
for i in range(0, length(var.maps)):
merge([
for j in range(0, local.greatest_depth):
{
for f in local.modules[j][i].fields:
jsonencode(f.path) => f
}
]...)
]
// Merge the maps using the standard merge function, which will cause higher-precedence map values to overwrite lower-precedence values
merged_map = merge(flatten([local.fields_json])...)
// Split the merged fields into segments for each depth
merged_fields_by_depth = {
for depth in range(0, local.greatest_depth):
depth => {
for key in keys(local.merged_map):
key => local.merged_map[key]
if length(local.merged_map[key].path) == depth + 1
}
}
// The lowest level of the re-assembled map is special and not part of the auto-generated depth.tf file
m0 = {
for field in local.merged_fields_by_depth[0]:
field.path[0] => {final_val = field.value, sub_val = lookup(local.m1, field.key, null)}[field.is_final ? "final_val" : "sub_val"]
}
}
// Check to make sure the highest level module has no remaining values that weren't recursed through
module "asset_sufficient_levels" {
source = "git::ssh://[email protected]/onemedical/terraform-null-assertion"
error_message = "Deepmerge has recursed to insufficient depth (${length(local.modules)} levels is not enough)"
condition = concat([flatten([
for i in range(0, length(var.maps)):
local.modules[length(local.modules) - 1][i].remaining
])]...) == []
}
// Use this from a DIFFERENT terraform project to generate a new file with a different max depth
/*
resource "local_file" "depth" {
content = templatefile("${path.module}/../deepmerge/depth.tmpl", {max_depth = 100})
filename = "${path.module}/../deepmerge/depth.tf"
}
*/