@@ -17,41 +17,50 @@ locals {
17
17
# Merge the map of empty values, with the variable context, so that context_local always contains all map keys
18
18
context_local = " ${ merge (local. context_struct , var. context )} "
19
19
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
36
49
selected_attributes = [" ${ distinct (compact (concat (var. attributes , local. context_local [" attributes" ])))} " ]
37
50
attributes = " ${ lower (join (local. delimiter , local. selected_attributes ))} "
38
-
39
51
generated_tags = {
40
52
" Name" = " ${ local . id } "
41
53
" Namespace" = " ${ local . namespace } "
42
54
" Environment" = " ${ local . environment } "
43
55
" Stage" = " ${ local . stage } "
44
56
}
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 } " ]
50
60
label_order_default_list = " ${ list (" namespace" , " environment" , " stage" , " name" , " attributes" )} "
51
61
label_order_context_list = " ${ distinct (compact (local. context_local [" label_order" ]))} "
52
62
label_order_final_list = [" ${ distinct (compact (coalescelist (var. label_order , local. label_order_context_list , local. label_order_default_list )))} " ]
53
63
label_order_length = " ${ (length (local. label_order_final_list ))} "
54
-
55
64
# Context of this module to pass between other modules
56
65
output_context = {
57
66
name = [" ${ local . name } " ]
@@ -64,15 +73,13 @@ locals {
64
73
delimiter = [" ${ local . delimiter } " ]
65
74
label_order = [" ${ local . label_order_final_list } " ]
66
75
}
67
-
68
76
id_context = {
69
77
name = " ${ local . name } "
70
78
namespace = " ${ local . namespace } "
71
79
environment = " ${ local . environment } "
72
80
stage = " ${ local . stage } "
73
81
attributes = " ${ local . attributes } "
74
82
}
75
-
76
83
id = " ${ lower (join (local. delimiter , compact (list (
77
84
" ${ local . label_order_length > 0 ? local . id_context [element (local. label_order_final_list , 0 )] : " " } " ,
78
85
" ${ local . label_order_length > 1 ? local . id_context [element (local. label_order_final_list , 1 )] : " " } " ,
0 commit comments