From a61a3d82ed44ce03c51fa5185da8c1728e358829 Mon Sep 17 00:00:00 2001
From: kbcz1989 <58665245+kbcz1989@users.noreply.github.com>
Date: Thu, 7 Nov 2024 18:18:08 +0100
Subject: [PATCH] Implement terragrunt-dont-check-dependent-modules flag
(#3534)
* check-dependent-modules: update docs
* check-dependent-modules: add check flag
* check-dependent-modules: add test
* check-dependent-modules: rename var for consistency
* check-dependent-modules: fix wrong test
* check-dependent-modules: rename properly
---
cli/commands/flags.go | 9 +++++++
cli/commands/terraform/command.go | 2 +-
docs/_docs/04_reference/cli-options.md | 9 +++++++
options/options.go | 4 ++++
test/integration_destroy_test.go | 33 ++++++++++++++++++++++++++
5 files changed, 56 insertions(+), 1 deletion(-)
diff --git a/cli/commands/flags.go b/cli/commands/flags.go
index b17f602f84..3ae65a6d60 100644
--- a/cli/commands/flags.go
+++ b/cli/commands/flags.go
@@ -127,6 +127,9 @@ const (
TerragruntJSONOutDirFlagEnvName = "TERRAGRUNT_JSON_OUT_DIR"
TerragruntJSONOutDirFlagName = "terragrunt-json-out-dir"
+ TerragruntNoDestroyDependenciesCheckFlagEnvName = "TERRAGRUNT_NO_DESTROY_DEPENDENCIES_CHECK"
+ TerragruntNoDestroyDependenciesCheckFlagName = "terragrunt-no-destroy-dependencies-check"
+
// Logs related flags/envs
TerragruntLogLevelFlagName = "terragrunt-log-level"
@@ -465,6 +468,12 @@ func NewGlobalFlags(opts *options.TerragruntOptions) cli.Flags {
Destination: &opts.DisableCommandValidation,
Usage: "When this flag is set, Terragrunt will not validate the terraform command.",
},
+ &cli.BoolFlag{
+ Name: TerragruntNoDestroyDependenciesCheckFlagName,
+ EnvVar: TerragruntNoDestroyDependenciesCheckFlagEnvName,
+ Destination: &opts.NoDestroyDependenciesCheck,
+ Usage: "When this flag is set, Terragrunt will not check for dependent modules when destroying.",
+ },
// Strict Mode flags
&cli.BoolFlag{
Name: TerragruntStrictModeFlagName,
diff --git a/cli/commands/terraform/command.go b/cli/commands/terraform/command.go
index 72c6fa9b80..786756ec2c 100644
--- a/cli/commands/terraform/command.go
+++ b/cli/commands/terraform/command.go
@@ -32,7 +32,7 @@ func NewCommand(opts *options.TerragruntOptions) *cli.Command {
func Action(opts *options.TerragruntOptions) cli.ActionFunc {
return func(ctx *cli.Context) error {
if opts.TerraformCommand == terraform.CommandNameDestroy {
- opts.CheckDependentModules = true
+ opts.CheckDependentModules = !opts.NoDestroyDependenciesCheck
}
if !opts.DisableCommandValidation && !collections.ListContainsElement(nativeTerraformCommands, opts.TerraformCommand) {
diff --git a/docs/_docs/04_reference/cli-options.md b/docs/_docs/04_reference/cli-options.md
index 00fbfd25d7..68c042aea8 100644
--- a/docs/_docs/04_reference/cli-options.md
+++ b/docs/_docs/04_reference/cli-options.md
@@ -89,6 +89,7 @@ This page documents the CLI commands and options available with Terragrunt:
- [terragrunt-json-out-dir](#terragrunt-json-out-dir)
- [terragrunt-disable-log-formatting](#terragrunt-disable-log-formatting)
- [terragrunt-forward-tf-stdout](#terragrunt-forward-tf-stdout)
+ - [terragrunt-no-destroy-dependencies-check](#terragrunt-no-destroy-dependencies-check)
## CLI commands
@@ -793,6 +794,7 @@ prefix `--terragrunt-` (e.g., `--terragrunt-config`). The currently available op
- [terragrunt-json-out-dir](#terragrunt-json-out-dir)
- [terragrunt-disable-log-formatting](#terragrunt-disable-log-formatting)
- [terragrunt-forward-tf-stdout](#terragrunt-forward-tf-stdout)
+ - [terragrunt-no-destroy-dependencies-check](#terragrunt-no-destroy-dependencies-check)
### terragrunt-config
@@ -1522,3 +1524,10 @@ plan. Resource actions are indicated with the following symbols:
OpenTofu will perform the following actions:
```
+
+### terragrunt-no-destroy-dependencies-check
+
+**CLI Arg**: `--terragrunt-no-destroy-dependencies-check`
+**Environment Variable**: `TERRAGRUNT_NO_DESTROY_DEPENDENCIES_CHECK`
+
+If specified, Terragrunt will not check dependent modules when running `destroy` command. By default, Terragrunt checks dependent modules when running `destroy` command.
diff --git a/options/options.go b/options/options.go
index 42f45c09ed..beac4518b0 100644
--- a/options/options.go
+++ b/options/options.go
@@ -263,6 +263,9 @@ type TerragruntOptions struct {
// True if is required to show dependent modules and confirm action
CheckDependentModules bool
+ // True if is required not to show dependent modules and confirm action
+ NoDestroyDependenciesCheck bool
+
// This is an experimental feature, used to speed up dependency processing by getting the output from the state
FetchDependencyOutputFromState bool
@@ -586,6 +589,7 @@ func (opts *TerragruntOptions) Clone(terragruntConfigPath string) (*TerragruntOp
JSONLogFormat: opts.JSONLogFormat,
Check: opts.Check,
CheckDependentModules: opts.CheckDependentModules,
+ NoDestroyDependenciesCheck: opts.NoDestroyDependenciesCheck,
FetchDependencyOutputFromState: opts.FetchDependencyOutputFromState,
UsePartialParseConfigCache: opts.UsePartialParseConfigCache,
ForwardTFStdout: opts.ForwardTFStdout,
diff --git a/test/integration_destroy_test.go b/test/integration_destroy_test.go
index f9cae42d69..004c466495 100644
--- a/test/integration_destroy_test.go
+++ b/test/integration_destroy_test.go
@@ -155,6 +155,39 @@ func TestShowWarningWithDependentModulesBeforeDestroy(t *testing.T) {
assert.Equal(t, 1, strings.Count(output, appV2Path))
}
+func TestNoShowWarningWithDependentModulesBeforeDestroy(t *testing.T) {
+ t.Parallel()
+
+ rootPath := copyEnvironment(t, testFixtureDestroyWarning)
+
+ rootPath = util.JoinPath(rootPath, testFixtureDestroyWarning)
+ vpcPath := util.JoinPath(rootPath, "vpc")
+ appV1Path := util.JoinPath(rootPath, "app-v1")
+ appV2Path := util.JoinPath(rootPath, "app-v2")
+
+ cleanupTerraformFolder(t, rootPath)
+ cleanupTerraformFolder(t, vpcPath)
+
+ stdout := bytes.Buffer{}
+ stderr := bytes.Buffer{}
+
+ err := runTerragruntCommand(t, "terragrunt run-all init --terragrunt-non-interactive --terragrunt-working-dir "+rootPath, &stdout, &stderr)
+ require.NoError(t, err)
+ err = runTerragruntCommand(t, "terragrunt run-all apply --terragrunt-non-interactive --terragrunt-working-dir "+rootPath, &stdout, &stderr)
+ require.NoError(t, err)
+
+ // try to destroy vpc module and check if warning is not printed in output
+ stdout = bytes.Buffer{}
+ stderr = bytes.Buffer{}
+
+ err = runTerragruntCommand(t, "terragrunt destroy --terragrunt-non-interactive --terragrunt-no-destroy-dependencies-check --terragrunt-working-dir "+vpcPath, &stdout, &stderr)
+ require.NoError(t, err)
+
+ output := stderr.String()
+ assert.Equal(t, 0, strings.Count(output, appV1Path))
+ assert.Equal(t, 0, strings.Count(output, appV2Path))
+}
+
func TestPreventDestroyDependenciesIncludedConfig(t *testing.T) {
t.Parallel()