Skip to content

Commit

Permalink
Implement terragrunt-dont-check-dependent-modules flag (#3534)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kbcz1989 authored Nov 7, 2024
1 parent 932d174 commit a61a3d8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
9 changes: 9 additions & 0 deletions cli/commands/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/terraform/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions docs/_docs/04_reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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`<br/>
**Environment Variable**: `TERRAGRUNT_NO_DESTROY_DEPENDENCIES_CHECK`<br/>

If specified, Terragrunt will not check dependent modules when running `destroy` command. By default, Terragrunt checks dependent modules when running `destroy` command.
4 changes: 4 additions & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions test/integration_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit a61a3d8

Please sign in to comment.