Skip to content

Commit 0b88ac5

Browse files
authored
Use deep-merge for imports at the same level (#8)
* Use deep-merge * Use deep-merge
1 parent 1afa46c commit 0b88ac5

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

examples/imports-local/config/imports-level-2.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ vars:
77
environment: ue2
88

99
terraform:
10-
vars: {}
10+
vars:
11+
var_1: 1_override
12+
var_2: 2_override
1113

1214
helmfile:
13-
vars: {}
15+
vars:
16+
var_1: 1
17+
var_2: 2
18+

examples/imports-local/config/imports-level-3.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ vars:
55
namespace: eg
66

77
terraform:
8-
vars: {}
8+
vars:
9+
var_1: 1
10+
var_3: 3
911

1012
helmfile:
1113
vars: {}

examples/imports-local/config/imports-level-3a.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ vars:
22
level: 3
33

44
terraform:
5-
vars: {}
5+
vars:
6+
var_1: 1a
7+
var_2: 2a
8+
var_3: 3a
69

710
helmfile:
811
vars: {}

modules/yaml-config/main.tf

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,29 @@ locals {
55
] : []
66

77
# Terraform maps from local YAML configuration templates
8-
local_map_configs = merge(
9-
flatten(
10-
[
11-
for path in local.local_map_config_paths : [
12-
for f in fileset(var.map_config_local_base_path, path) : {
13-
for k, v in yamldecode(templatefile(format("%s/%s", var.map_config_local_base_path, f), var.parameters)) : k => v
14-
}
15-
]
8+
local_map_configs = flatten(
9+
[
10+
for path in local.local_map_config_paths : [
11+
for f in fileset(var.map_config_local_base_path, path) : {
12+
for k, v in yamldecode(templatefile(format("%s/%s", var.map_config_local_base_path, f), var.parameters)) : k => v
13+
}
1614
]
17-
)
18-
...)
15+
]
16+
)
1917

2018
# Remote YAML paths with configs of type map
2119
remote_map_config_paths = module.this.enabled ? [
2220
for path in var.map_config_paths : path if replace(path, var.remote_config_selector, "") != path
2321
] : []
2422

2523
# Terraform maps from remote YAML configuration templates
26-
remote_map_configs = merge(
24+
remote_map_configs = flatten(
2725
[
2826
for path in local.remote_map_config_paths : {
2927
for k, v in yamldecode(data.template_file.remote_config[base64encode(path)].rendered) : k => v
3028
}
3129
]
32-
...)
30+
)
3331

3432
# Local YAML paths with configs of type list
3533
local_list_config_paths = module.this.enabled ? [
@@ -67,6 +65,11 @@ locals {
6765
} : {}
6866
}
6967

68+
module "all_map_configs" {
69+
source = "../deepmerge"
70+
maps = concat([{}], local.remote_map_configs, local.local_map_configs)
71+
}
72+
7073
# Download all remote configs
7174
data "http" "remote_config" {
7275
for_each = module.this.enabled ? local.all_remote_config_paths_map : {}
@@ -81,20 +84,17 @@ data "template_file" "remote_config" {
8184
}
8285

8386
locals {
84-
# Final map configs
85-
all_map_configs = merge({}, local.local_map_configs, local.remote_map_configs)
86-
8787
# Final list configs
8888
all_list_configs = concat([], local.local_list_configs, local.remote_list_configs)
8989

9090
# Imports from local map configs
9191
local_map_imports = [
92-
for import in lookup(merge({}, local.local_map_configs), "import", []) : format("%s.yaml", import)
92+
for import in lookup(merge({}, local.local_map_configs...), "import", []) : format("%s.yaml", import)
9393
]
9494

9595
# Imports from remote map configs
9696
remote_map_imports = [
97-
for import in lookup(merge({}, local.remote_map_configs), "import", []) : format("%s/%s.yaml", var.map_config_remote_base_path, import)
97+
for import in lookup(merge({}, local.remote_map_configs...), "import", []) : format("%s/%s.yaml", var.map_config_remote_base_path, import)
9898
]
9999

100100
# Combined imports from local and remote map configs

modules/yaml-config/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
output "map_configs" {
2-
value = local.all_map_configs
2+
value = module.all_map_configs.merged
33
description = "Terraform maps from YAML configurations"
44
}
55

0 commit comments

Comments
 (0)