Skip to content

Commit

Permalink
fix: Check planfile and put it in the end of an arguments list (#1740)
Browse files Browse the repository at this point in the history
* check planfile and put it in the end of an args list

* fix naming

* replace IsFile check

* rename planfile function

* add planfile fixture

* remove remote state; fix test and clarify args

* add fixture gitignore

* add special check for tfplan extension

* optimize planfile check

Co-authored-by: Roman Pertsev <roman.pertsev@nordigy.ru>
  • Loading branch information
PertsevRoman and Roman Pertsev committed Jul 20, 2021
1 parent 9ddac0b commit c145252
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cli/cli_app.go
Expand Up @@ -631,7 +631,8 @@ func runTerragruntWithConfig(originalTerragruntOptions *options.TerragruntOption

// Add extra_arguments to the command
if terragruntConfig.Terraform != nil && terragruntConfig.Terraform.ExtraArgs != nil && len(terragruntConfig.Terraform.ExtraArgs) > 0 {
terragruntOptions.InsertTerraformCliArgs(filterTerraformExtraArgs(terragruntOptions, terragruntConfig)...)
args := filterTerraformExtraArgs(terragruntOptions, terragruntConfig)
terragruntOptions.InsertTerraformCliArgs(args...)
for k, v := range filterTerraformEnvVarsFromExtraArgs(terragruntOptions, terragruntConfig) {
terragruntOptions.Env[k] = v
}
Expand Down
34 changes: 33 additions & 1 deletion options/options.go
Expand Up @@ -302,8 +302,34 @@ func (terragruntOptions *TerragruntOptions) Clone(terragruntConfigPath string) *
}
}

// Check if argument is planfile TODO check file format
func checkIfPlanFile(arg string) bool {
return util.IsFile(arg) && filepath.Ext(arg) == ".tfplan"
}

// Extract planfile from arguments list
func extractPlanFile(argsToInsert []string) (*string, []string) {
planFile := ""
var filteredArgs []string

for _, arg := range argsToInsert {
if checkIfPlanFile(arg) {
planFile = arg
} else {
filteredArgs = append(filteredArgs, arg)
}
}

if planFile != "" {
return &planFile, filteredArgs
}

return nil, filteredArgs
}

// Inserts the given argsToInsert after the terraform command argument, but before the remaining args
func (terragruntOptions *TerragruntOptions) InsertTerraformCliArgs(argsToInsert ...string) {
planFile, restArgs := extractPlanFile(argsToInsert)

commandLength := 1
if util.ListContainsElement(TERRAFORM_COMMANDS_WITH_SUBCOMMAND, terragruntOptions.TerraformCliArgs[0]) {
Expand All @@ -316,8 +342,14 @@ func (terragruntOptions *TerragruntOptions) InsertTerraformCliArgs(argsToInsert
// command is either 1 word or 2 words
var args []string
args = append(args, terragruntOptions.TerraformCliArgs[:commandLength]...)
args = append(args, argsToInsert...)
args = append(args, restArgs...)
args = append(args, terragruntOptions.TerraformCliArgs[commandLength:]...)

// check if planfile was extracted
if planFile != nil {
args = append(args, *planFile)
}

terragruntOptions.TerraformCliArgs = args
}

Expand Down
1 change: 1 addition & 0 deletions test/fixture-planfile-order-test/.gitignore
@@ -0,0 +1 @@
test-provider.tf
3 changes: 3 additions & 0 deletions test/fixture-planfile-order-test/inputs.tf
@@ -0,0 +1,3 @@
variable "resource_count" {
type = number
}
3 changes: 3 additions & 0 deletions test/fixture-planfile-order-test/resource.tf
@@ -0,0 +1,3 @@
resource "null_resource" "test-resources" {
count = var.resource_count
}
36 changes: 36 additions & 0 deletions test/fixture-planfile-order-test/terragrunt.hcl
@@ -0,0 +1,36 @@
# Configure Terragrunt to automatically store tfstate files in an S3 bucket
generate "test-null-provider" {
path = "test-provider.tf"
if_exists = "overwrite_terragrunt"

contents = <<EOF
provider "null" {
}
EOF
}

terraform {
extra_arguments "plan_vars" {
commands = [
"plan",
]

arguments = [
"-out=${get_terragrunt_dir()}/default.tfplan",
"-var-file",
"${get_terragrunt_dir()}/vars/variables.tfvars",
"-no-color",
]
}

extra_arguments "apply_vars" {
commands = [
"apply",
]

arguments = [
"${get_terragrunt_dir()}/default.tfplan",
"-no-color",
]
}
}
1 change: 1 addition & 0 deletions test/fixture-planfile-order-test/vars/variables.tfvars
@@ -0,0 +1 @@
resource_count = 3
14 changes: 14 additions & 0 deletions test/integration_test.go
Expand Up @@ -121,6 +121,7 @@ const (
TEST_FIXTURE_GET_TERRAGRUNT_SOURCE_HCL = "fixture-get-terragrunt-source-hcl"
TEST_FIXTURE_GET_TERRAGRUNT_SOURCE_CLI = "fixture-get-terragrunt-source-cli"
TEST_FIXTURE_REGRESSIONS = "fixture-regressions"
TEST_FIXTURE_PLANFILE_ORDER = "fixture-planfile-order-test"
TEST_FIXTURE_DIRS_PATH = "fixture-dirs"
TEST_FIXTURE_PARALLELISM = "fixture-parallelism"
TEST_FIXTURE_SOPS = "fixture-sops"
Expand Down Expand Up @@ -1221,6 +1222,19 @@ func TestAutoRetrySkip(t *testing.T) {
assert.NotContains(t, out.String(), "Apply complete!")
}

func TestPlanfileOrder(t *testing.T) {
t.Parallel()

rootPath := copyEnvironment(t, TEST_FIXTURE_PLANFILE_ORDER)
modulePath := util.JoinPath(rootPath, TEST_FIXTURE_PLANFILE_ORDER)

err := runTerragruntCommand(t, fmt.Sprintf("terragrunt plan --terragrunt-working-dir %s", modulePath), os.Stdout, os.Stderr)
assert.Nil(t, err)

err = runTerragruntCommand(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-working-dir %s", modulePath), os.Stdout, os.Stderr)
assert.Nil(t, err)
}

func TestAutoRetryExhaustRetries(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit c145252

Please sign in to comment.