Skip to content

Commit 456cc76

Browse files
feat: Skip legacy modules (with provider block) in terraform_wrapper_module_for_each hook (antonbabenko#560)
1 parent 0e2b151 commit 456cc76

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

hooks/terraform_wrapper_module_for_each.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,6 @@ EOF
291291
# Wrappers will be stored in "wrappers/{module_name}"
292292
output_dir="${root_dir}/${wrapper_dir}/${module_name}"
293293

294-
[[ ! -d "$output_dir" ]] && mkdir -p "$output_dir"
295-
296294
# Calculate relative depth for module source by number of slashes
297295
module_depth="${module_dir//[^\/]/}"
298296

@@ -329,6 +327,14 @@ EOF
329327
# shellcheck disable=SC2207
330328
module_outputs=($(echo "$all_tf_content" | hcledit block list | { grep output. | cut -d'.' -f 2 || true; }))
331329

330+
# Get names of module providers in all terraform files
331+
module_providers=$(echo "$all_tf_content" | hcledit block list | { grep provider. || true; })
332+
333+
if [[ $module_providers ]]; then
334+
common::colorify "yellow" "Skipping ${full_module_dir} because it is a legacy module which contains its own local provider configurations and so calls to it may not use the for_each argument."
335+
break
336+
fi
337+
332338
# Looking for sensitive output
333339
local wrapper_output_sensitive="# sensitive = false # No sensitive module output found"
334340
for module_output in "${module_outputs[@]}"; do
@@ -381,6 +387,9 @@ EOF
381387
if [[ "$dry_run" == "false" ]]; then
382388
common::colorify "green" "Saving files into \"${output_dir}\""
383389

390+
# Create output dir
391+
[[ ! -d "$output_dir" ]] && mkdir -p "$output_dir"
392+
384393
mv "$tmp_file_tf" "${output_dir}/main.tf"
385394

386395
echo "$CONTENT_VARIABLES_TF" > "${output_dir}/variables.tf"

0 commit comments

Comments
 (0)