From 2607cff241737847fbeaf217db3e219d4deff851 Mon Sep 17 00:00:00 2001 From: AntonioCS Date: Mon, 15 Dec 2025 15:30:44 +0000 Subject: [PATCH] Fix terraform validate/init/output/state not supporting -var-file (v2.2.1) - Add skip_vars parameter (arg4) to tf_run function - Commands that don't support -var-file now pass $(mb_true) for skip_vars: init, validate, output, state list - Add test_modules_terraform_run_skip_vars test --- CHANGELOG.md | 7 +++++++ modules/infrastructure/terraform/terraform.mk | 17 ++++++++++------- .../terraform/terraform_test.mk | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 586c5ec..d621a52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [2.2.1] - 2025-12-15 + +### Fixed +- **Terraform module**: Added `skip_vars` parameter to `tf_run` function for commands that don't support `-var-file` + - Commands that now correctly skip `-var-file`: `init`, `validate`, `output`, `state list` + - New test: `test_modules_terraform_run_skip_vars` + ## [2.2.0] - 2025-12-15 ### Added diff --git a/modules/infrastructure/terraform/terraform.mk b/modules/infrastructure/terraform/terraform.mk index 79c1213..3a2e58e 100644 --- a/modules/infrastructure/terraform/terraform.mk +++ b/modules/infrastructure/terraform/terraform.mk @@ -78,15 +78,17 @@ endef ## @function tf_run ## @desc Execute terraform command in environment directory -## @desc Automatically includes shared tfvars if tf_shared_vars is set +## @desc Automatically includes shared tfvars if tf_shared_vars is set (unless skip_vars is true) ## @arg 1: env (required) - Environment name (e.g., local, dev, prod) ## @arg 2: command (required) - Terraform command (e.g., init, plan, apply) ## @arg 3: extra_args (optional) - Additional arguments +## @arg 4: skip_vars (optional) - Set to $(mb_true) to skip -var-file inclusion (for commands that don't support it) ## @returns Command output via mb_invoke ## @group terraform -## @example $(call tf_run,local,init) +## @example $(call tf_run,local,init,,,$(mb_true)) ## @example $(call tf_run,dev,plan,-detailed-exitcode) ## @example $(call tf_run,prod,apply,-auto-approve) +## @note Commands that don't support -var-file: init, validate, output, state define tf_run $(strip \ $(if $(value 1),,$(call mb_printf_error,$0: env required)) \ @@ -94,8 +96,9 @@ $(strip \ $(eval $0_arg1_env := $(strip $1)) \ $(eval $0_arg2_cmd := $(strip $2)) \ $(eval $0_arg3_extra := $(if $(value 3),$(strip $3))) \ + $(eval $0_arg4_skip_vars := $(if $(value 4),$(strip $4))) \ $(eval $0_chdir := $(call tf_build_chdir,$($0_arg1_env))) \ - $(eval $0_var_file := $(call tf_build_var_file)) \ + $(eval $0_var_file := $(if $(filter $(mb_true),$($0_arg4_skip_vars)),,$(call tf_build_var_file))) \ $(call mb_invoke,$(tf_bin) $($0_chdir) $($0_arg2_cmd) $($0_var_file) $($0_arg3_extra)) \ ) endef @@ -107,7 +110,7 @@ ifndef __MB_TEST_DISCOVERY__ terraform/init/%: ## Initialize Terraform for environment (usage: make terraform/init/local) $(call mb_printf_info,Initializing Terraform for $* environment...) - $(call tf_run,$*,init) + $(call tf_run,$*,init,,$(mb_true)) terraform/plan/%: ## Plan Terraform changes (usage: make terraform/plan/dev) $(call mb_printf_info,Planning Terraform changes for $* environment...) @@ -131,13 +134,13 @@ terraform/destroy/%: ## Destroy Terraform infrastructure (usage: make terraform/ terraform/validate/%: ## Validate Terraform configuration (usage: make terraform/validate/local) $(call mb_printf_info,Validating Terraform configuration for $*...) - $(call tf_run,$*,validate) + $(call tf_run,$*,validate,,$(mb_true)) terraform/output/%: ## Show Terraform outputs (usage: make terraform/output/local) - $(call tf_run,$*,output) + $(call tf_run,$*,output,,$(mb_true)) terraform/state/list/%: ## List Terraform state resources (usage: make terraform/state/list/local) - $(call tf_run,$*,state list) + $(call tf_run,$*,state list,,$(mb_true)) terraform/refresh/%: ## Refresh Terraform state (usage: make terraform/refresh/local) $(call mb_printf_info,Refreshing Terraform state for $*...) diff --git a/modules/infrastructure/terraform/terraform_test.mk b/modules/infrastructure/terraform/terraform_test.mk index ba31e26..cb8016c 100644 --- a/modules/infrastructure/terraform/terraform_test.mk +++ b/modules/infrastructure/terraform/terraform_test.mk @@ -170,6 +170,25 @@ define test_modules_terraform_run_with_shared_vars $(eval mb_invoke_silent := $(mb_off)) endef +define test_modules_terraform_run_skip_vars + $(eval mb_invoke_silent := $(mb_on)) + $(eval tf_bin := terraform) + $(eval tf_chdir_flag := $(mb_true)) + $(eval tf_env_dir := terraform/environments) + $(eval tf_shared_vars := ../../shared/common.tfvars) + + ## With skip_vars=$(mb_true), should NOT include -var-file + $(eval $0_result := $(call tf_run,local,validate,,$(mb_true))) + $(call mb_assert_contains,terraform,$($0_result)) + $(call mb_assert_contains,validate,$($0_result)) + $(call mb_assert_not_empty,$(findstring -chdir,$($0_result))) + ## Verify -var-file is NOT present + $(call mb_assert_empty,$(findstring -var-file,$($0_result)),skip_vars should exclude -var-file) + + $(eval tf_shared_vars :=) + $(eval mb_invoke_silent := $(mb_off)) +endef + define test_modules_terraform_run_error_without_env $(eval mb_invoke_silent := $(mb_on))