Skip to content

Commit

Permalink
Use deep-merge for imports at the same level (cloudposse#8)
Browse files Browse the repository at this point in the history
* Use deep-merge

* Use deep-merge
  • Loading branch information
aknysh authored Jan 17, 2021
1 parent 1afa46c commit 0b88ac5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
9 changes: 7 additions & 2 deletions examples/imports-local/config/imports-level-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ vars:
environment: ue2

terraform:
vars: {}
vars:
var_1: 1_override
var_2: 2_override

helmfile:
vars: {}
vars:
var_1: 1
var_2: 2

4 changes: 3 additions & 1 deletion examples/imports-local/config/imports-level-3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ vars:
namespace: eg

terraform:
vars: {}
vars:
var_1: 1
var_3: 3

helmfile:
vars: {}
Expand Down
5 changes: 4 additions & 1 deletion examples/imports-local/config/imports-level-3a.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ vars:
level: 3

terraform:
vars: {}
vars:
var_1: 1a
var_2: 2a
var_3: 3a

helmfile:
vars: {}
34 changes: 17 additions & 17 deletions modules/yaml-config/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@ locals {
] : []

# Terraform maps from local YAML configuration templates
local_map_configs = merge(
flatten(
[
for path in local.local_map_config_paths : [
for f in fileset(var.map_config_local_base_path, path) : {
for k, v in yamldecode(templatefile(format("%s/%s", var.map_config_local_base_path, f), var.parameters)) : k => v
}
]
local_map_configs = flatten(
[
for path in local.local_map_config_paths : [
for f in fileset(var.map_config_local_base_path, path) : {
for k, v in yamldecode(templatefile(format("%s/%s", var.map_config_local_base_path, f), var.parameters)) : k => v
}
]
)
...)
]
)

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

# Terraform maps from remote YAML configuration templates
remote_map_configs = merge(
remote_map_configs = flatten(
[
for path in local.remote_map_config_paths : {
for k, v in yamldecode(data.template_file.remote_config[base64encode(path)].rendered) : k => v
}
]
...)
)

# Local YAML paths with configs of type list
local_list_config_paths = module.this.enabled ? [
Expand Down Expand Up @@ -67,6 +65,11 @@ locals {
} : {}
}

module "all_map_configs" {
source = "../deepmerge"
maps = concat([{}], local.remote_map_configs, local.local_map_configs)
}

# Download all remote configs
data "http" "remote_config" {
for_each = module.this.enabled ? local.all_remote_config_paths_map : {}
Expand All @@ -81,20 +84,17 @@ data "template_file" "remote_config" {
}

locals {
# Final map configs
all_map_configs = merge({}, local.local_map_configs, local.remote_map_configs)

# Final list configs
all_list_configs = concat([], local.local_list_configs, local.remote_list_configs)

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

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

# Combined imports from local and remote map configs
Expand Down
2 changes: 1 addition & 1 deletion modules/yaml-config/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
output "map_configs" {
value = local.all_map_configs
value = module.all_map_configs.merged
description = "Terraform maps from YAML configurations"
}

Expand Down

0 comments on commit 0b88ac5

Please sign in to comment.