Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Check planfile and put it in the end of an arguments list #1740

Merged
merged 9 commits into from Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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