Skip to content

Commit efcfeeb

Browse files
Gerard Lynchaknysh
authored andcommitted
fix precedence when default value is non-empty (#38)
The only case this actually applies to is the delimiter, which has a default value of '-', however all others changed to make them consistent. This fixes the case where a module label is instantiated with a non-default delimiter, and then another label is generated based off the context but uses the default delimiter as the local var takes precendance.
1 parent f139b6b commit efcfeeb

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

main.tf

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,50 @@ locals {
1717
# Merge the map of empty values, with the variable context, so that context_local always contains all map keys
1818
context_local = "${merge(local.context_struct, var.context)}"
1919

20-
# Provided variables take precedence over the variables from the provided context
21-
selected_name = ["${coalescelist(compact(list(var.name)), compact(local.context_local["name"]), list(""))}"]
22-
name = "${lower(replace(local.selected_name[0], "/[^a-zA-Z0-9]/", ""))}"
23-
24-
selected_namespace = ["${coalescelist(compact(list(var.namespace)), compact(local.context_local["namespace"]), list(""))}"]
25-
namespace = "${lower(replace(local.selected_namespace[0], "/[^a-zA-Z0-9]/", ""))}"
26-
27-
selected_environment = ["${coalescelist(compact(list(var.environment)), compact(local.context_local["environment"]), list(""))}"]
28-
environment = "${lower(replace(local.selected_environment[0], "/[^a-zA-Z0-9]/", ""))}"
29-
30-
selected_stage = ["${coalescelist(compact(list(var.stage)), compact(local.context_local["stage"]), list(""))}"]
31-
stage = "${lower(replace(local.selected_stage[0], "/[^a-zA-Z0-9]/", ""))}"
32-
33-
selected_delimiter = ["${coalescelist(compact(list(var.delimiter)), compact(local.context_local["delimiter"]), list(""))}"]
34-
delimiter = "${local.selected_delimiter[0]}"
35-
20+
# Provided variables take precedence over the variables from the provided context _if_ they're not the default
21+
# if thing == default and if local_context[thing] != ""
22+
# local_context[thing]
23+
# else
24+
# thing
25+
26+
# Workaround Terraform's inability to figure out a type if it's empty
27+
names = "${concat(local.context_local["name"], list(""))}"
28+
# Workaround Terraform's inability to handle nested conditionals
29+
name_context_or_default = "${length(local.names) > 0? local.names[0] : var.name}"
30+
name_or_context = "${var.name != ""? var.name : local.name_context_or_default}"
31+
# Normalise
32+
name = "${lower(replace(local.name_or_context, "/[^a-zA-Z0-9]/", ""))}"
33+
namespaces = "${concat(local.context_local["namespace"], list(""))}"
34+
namespace_context_or_default = "${length(local.namespaces[0]) > 0? local.namespaces[0] : var.namespace}"
35+
namespace_or_context = "${var.namespace != ""? var.namespace : local.namespace_context_or_default}"
36+
namespace = "${lower(replace(local.namespace_or_context, "/[^a-zA-Z0-9]/", ""))}"
37+
environments = "${concat(local.context_local["environment"], list(""))}"
38+
environment_context_or_default = "${length(local.environments[0]) > 0? local.environments[0] : var.environment}"
39+
environment_or_context = "${var.environment != ""? var.environment : local.environment_context_or_default}"
40+
environment = "${lower(replace(local.environment_or_context, "/[^a-zA-Z0-9]/", ""))}"
41+
stages = "${concat(local.context_local["stage"], list(""))}"
42+
stage_context_or_default = "${length(local.stages[0]) > 0? local.stages[0] : var.stage}"
43+
stage_or_context = "${var.stage != ""? var.stage : local.stage_context_or_default}"
44+
stage = "${lower(replace(local.stage_or_context, "/[^a-zA-Z0-9]/", ""))}"
45+
delimiters = "${concat(local.context_local["delimiter"], list(""))}"
46+
delimiter_context_or_default = "${length(local.delimiters[0]) > 0? local.delimiters[0] : var.delimiter}"
47+
delimiter = "${var.delimiter != "-"? var.delimiter : local.delimiter_context_or_default}"
48+
# Merge attributes
3649
selected_attributes = ["${distinct(compact(concat(var.attributes, local.context_local["attributes"])))}"]
3750
attributes = "${lower(join(local.delimiter, local.selected_attributes))}"
38-
3951
generated_tags = {
4052
"Name" = "${local.id}"
4153
"Namespace" = "${local.namespace}"
4254
"Environment" = "${local.environment}"
4355
"Stage" = "${local.stage}"
4456
}
45-
46-
tags = "${merge(zipmap(local.context_local["tags_keys"], local.context_local["tags_values"]), local.generated_tags, var.tags)}"
47-
tags_count = "${length(keys(local.tags))}"
48-
tags_as_list_of_maps = ["${data.null_data_source.tags_as_list_of_maps.*.outputs}"]
49-
57+
tags = "${merge(zipmap(local.context_local["tags_keys"], local.context_local["tags_values"]), local.generated_tags, var.tags)}"
58+
tags_count = "${length(keys(local.tags))}"
59+
tags_as_list_of_maps = ["${data.null_data_source.tags_as_list_of_maps.*.outputs}"]
5060
label_order_default_list = "${list("namespace", "environment", "stage", "name", "attributes")}"
5161
label_order_context_list = "${distinct(compact(local.context_local["label_order"]))}"
5262
label_order_final_list = ["${distinct(compact(coalescelist(var.label_order, local.label_order_context_list, local.label_order_default_list)))}"]
5363
label_order_length = "${(length(local.label_order_final_list))}"
54-
5564
# Context of this module to pass between other modules
5665
output_context = {
5766
name = ["${local.name}"]
@@ -64,15 +73,13 @@ locals {
6473
delimiter = ["${local.delimiter}"]
6574
label_order = ["${local.label_order_final_list}"]
6675
}
67-
6876
id_context = {
6977
name = "${local.name}"
7078
namespace = "${local.namespace}"
7179
environment = "${local.environment}"
7280
stage = "${local.stage}"
7381
attributes = "${local.attributes}"
7482
}
75-
7683
id = "${lower(join(local.delimiter, compact(list(
7784
"${local.label_order_length > 0 ? local.id_context[element(local.label_order_final_list, 0)] : ""}",
7885
"${local.label_order_length > 1 ? local.id_context[element(local.label_order_final_list, 1)] : ""}",

0 commit comments

Comments
 (0)