Skip to content

Commit

Permalink
fix(DMVP-fix-var): Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aramkarapetian committed Sep 2, 2023
1 parent 2c0c627 commit c6282b9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ git config core.hooksPath githooks
| Name | Description |
|------|-------------|
| <a name="output_project_id"></a> [project\_id](#output\_project\_id) | The ID of terraform cloud project |
| <a name="output_test-data"></a> [test-data](#output\_test-data) | n/a |
| <a name="output_workspace_id"></a> [workspace\_id](#output\_workspace\_id) | The ID of created terraform cloud workspace |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
35 changes: 26 additions & 9 deletions locals.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
locals {
name_specials_clean = replace(var.name, "/[^a-zA-Z0-9_-]+/", "_")
project_name_specials_clean = var.workspace.project != null ? replace(var.workspace.project, "/[^a-zA-Z0-9 _-]+/", "") : null
linked_workspaces_mapping = { for workspace in coalesce(var.linked_workspaces, []) : workspace => "data.tfe_outputs.this[\"${workspace}\"].values.results" }
linked_workspaces_mapping = { for workspace in coalesce(var.linked_workspaces, []) : workspace => "data.tfe_outputs.this[\\\"${workspace}\\\"].values.results" }
note = "This file and its content are generated based on config, pleas check README.md for more details"
module_nested_provider = { for provider in var.module_providers : "${provider.name}${try(provider.alias, "") != "" ? ".${provider.alias}" : ""}" => "${provider.name}${try(provider.alias, "") != "" ? ".${provider.alias}" : ""}" if try(provider.module_nested_provider, false) }

Expand All @@ -14,10 +14,10 @@ locals {
version = var.module_version
module_nested_provider = local.module_nested_provider == {} ? null : local.module_nested_provider
variables = { for key, value in var.module_vars : key =>
format(
jsondecode(format(
replace(jsonencode(value), "/(${join("|", keys(local.linked_workspaces_mapping))})/", "%s"),
[for key in flatten(regexall("(${join("|", keys(local.linked_workspaces_mapping))})", jsonencode(value))) : try(local.linked_workspaces_mapping[key], "")]...
) }
)) }
linked_workspaces = jsonencode(coalesce(var.linked_workspaces, []))
workspace = var.workspace
}
Expand Down Expand Up @@ -54,13 +54,18 @@ locals {
{
note = local.note
providers = [for provider in var.module_providers : merge(provider, {
nested_block = try(provider.nested_block, null)
alias = try(provider.alias, null)
custom_vars = { for key, value in try(provider.custom_vars, {}) : key =>
format(
alias = try(provider.alias, null)
variables = { for key, value in try(provider.variables, {}) : key =>
jsondecode(format(
replace(jsonencode(value), "/(${join("|", keys(local.linked_workspaces_mapping))})/", "%s"),
[for key in flatten(regexall("(${join("|", keys(local.linked_workspaces_mapping))})", jsonencode(value))) : try(local.linked_workspaces_mapping[key], "")]...
) if !try(contains(keys(local.provider_custom_var_blocks[provider.name]), key), false)
)) if !try(contains(keys(local.provider_custom_var_blocks[provider.name]), key), false)
}
blocks = { for key, value in try(provider.blocks, {}) : key =>
jsondecode(format(
replace(jsonencode(value), "/(${join("|", keys(local.linked_workspaces_mapping))})/", "%s"),
[for key in flatten(regexall("(${join("|", keys(local.linked_workspaces_mapping))})", jsonencode(value))) : try(local.linked_workspaces_mapping[key], "")]...
)) if !try(contains(keys(local.provider_custom_var_blocks[provider.name]), key), false)
}
custom_var_blocks = { for key, value in try(module.provider_custom_vars_default_merged["${provider.name}${try(provider.alias, null) == null ? "" : "-${provider.alias}"}"].merged) : key => value if try(contains(keys(local.provider_custom_var_blocks[provider.name]), key), false) }
})]
Expand Down Expand Up @@ -133,7 +138,19 @@ module "provider_custom_vars_default_merged" {
for_each = { for provider in var.module_providers : "${provider.name}${try(provider.alias, null) == null ? "" : "-${provider.alias}"}" => provider }

maps = [
try(each.value.custom_vars, {}),
try(each.value.variables, {}),
try(local.provider_custom_var_blocks[each.value.name], {})
]
}


output "test-data" {
value = [for provider in var.module_providers : merge(provider, {
blocks = { for key, value in try(provider.blocks, {}) : key =>
format(
replace(jsonencode(value), "/(${join("|", keys(local.linked_workspaces_mapping))})/", "%s"),
[for key in flatten(regexall("(${join("|", keys(local.linked_workspaces_mapping))})", jsonencode(value))) : try(local.linked_workspaces_mapping[key], "")]...
) if !try(contains(keys(local.provider_custom_var_blocks[provider.name]), key), false)
}
})]
}
2 changes: 1 addition & 1 deletion templates/main.tf.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module "this" {
source = "${source}"
version = "${version}"
%{ for key, value in variables }
${key} = ${value}%{ endfor ~}
${key} = ${jsonencode(value)}%{ endfor ~}

%{ if module_nested_provider != null ~}
providers = ${jsonencode(module_nested_provider)}%{ endif ~}
Expand Down
17 changes: 0 additions & 17 deletions templates/providers-old.tf.tftpl

This file was deleted.

29 changes: 23 additions & 6 deletions templates/providers.tf.tftpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
## ${note}

%{ for provider_key, provider_value in providers }provider "${provider_value.name}" {
%{ if provider_value.nested_block != null ~}
${provider_value.nested_block} { %{ endif ~}
%{ if provider_value.alias != null ~}
alias = "${provider_value.alias}"%{ endif ~}
%{ for key, value in provider_value.custom_vars }
${key} = ${value}%{ endfor ~}

%{ for key, value in provider_value.variables }
${key} = ${jsonencode(value)}%{ endfor ~}

%{ for key, value in provider_value.blocks }
${key} {
%{ for nested_key, nested_value in value }

%{ if nested_key != "blocks" ~}
${nested_key} = ${jsonencode(nested_value)}%{ endif ~}%{ endfor ~}

%{ if try(value.blocks,null) != null ~}

%{ for key1, value1 in value.blocks }
${key1} {
%{ for nested_key1, nested_value1 in value1 }
${nested_key1} = ${jsonencode(nested_value1)}
%{ endfor ~}
}
%{ endfor ~}
%{ endif ~}

}%{ endfor ~}

%{ for key, value in provider_value.custom_var_blocks }
${key} {
Expand All @@ -16,6 +35,4 @@ ${provider_value.nested_block} { %{ endif ~}
}%{ endfor ~}

}
%{ if provider_value.nested_block != null ~}
} %{ endif ~}
%{ endfor ~}

0 comments on commit c6282b9

Please sign in to comment.