Skip to content

Commit

Permalink
Warn if using -target flag with apply.
Browse files Browse the repository at this point in the history
-target doesn't work with terraform apply when the apply is being run on
a planfile (which is how Atlantis runs plan). We should give the user an
error if they attempt this.

Fixes #399
  • Loading branch information
lkysow committed Dec 19, 2018
1 parent 0691f9f commit 58493f3
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
28 changes: 28 additions & 0 deletions server/events/runtime/apply_step_runner.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package runtime

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/events/models"
Expand All @@ -15,6 +17,10 @@ type ApplyStepRunner struct {
}

func (a *ApplyStepRunner) Run(ctx models.ProjectCommandContext, extraArgs []string, path string) (string, error) {
if a.hasTargetFlag(ctx, extraArgs) {
return "", errors.New("cannot run apply with -target because we are applying an already generated plan. Instead, run -target with atlantis plan")
}

planPath := filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectConfig))
stat, err := os.Stat(planPath)
if err != nil || stat.IsDir() {
Expand All @@ -39,3 +45,25 @@ func (a *ApplyStepRunner) Run(ctx models.ProjectCommandContext, extraArgs []stri
}
return out, tfErr
}

func (a *ApplyStepRunner) hasTargetFlag(ctx models.ProjectCommandContext, extraArgs []string) bool {
isTargetFlag := func(s string) bool {
if s == "-target" {
return true
}
split := strings.Split(s, "=")
return split[0] == "-target"
}

for _, arg := range ctx.CommentArgs {
if isTargetFlag(arg) {
return true
}
}
for _, arg := range extraArgs {
if isTargetFlag(arg) {
return true
}
}
return false
}
80 changes: 80 additions & 0 deletions server/events/runtime/apply_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -131,3 +132,82 @@ func TestRun_UsesConfiguredTFVersion(t *testing.T) {
_, err = os.Stat(planPath)
Assert(t, os.IsNotExist(err), "planfile should be deleted")
}

// Apply ignores the -target flag when used with a planfile so we should give
// an error if it's being used with -target.
func TestRun_UsingTarget(t *testing.T) {
cases := []struct {
commentFlags []string
extraArgs []string
expErr bool
}{
{
commentFlags: []string{"-target", "mytarget"},
expErr: true,
},
{
commentFlags: []string{"-target=mytarget"},
expErr: true,
},
{
extraArgs: []string{"-target", "mytarget"},
expErr: true,
},
{
extraArgs: []string{"-target=mytarget"},
expErr: true,
},
{
commentFlags: []string{"-target", "mytarget"},
extraArgs: []string{"-target=mytarget"},
expErr: true,
},
// Test false positives.
{
commentFlags: []string{"-targethahagotcha"},
expErr: false,
},
{
extraArgs: []string{"-targethahagotcha"},
expErr: false,
},
{
commentFlags: []string{"-targeted=weird"},
expErr: false,
},
{
extraArgs: []string{"-targeted=weird"},
expErr: false,
},
}

RegisterMockTestingT(t)

for _, c := range cases {
descrip := fmt.Sprintf("comments flags: %s extra args: %s",
strings.Join(c.commentFlags, ", "), strings.Join(c.extraArgs, ", "))
t.Run(descrip, func(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
planPath := filepath.Join(tmpDir, "workspace.tfplan")
err := ioutil.WriteFile(planPath, nil, 0644)
Ok(t, err)
terraform := mocks.NewMockClient()
step := runtime.ApplyStepRunner{
TerraformExecutor: terraform,
}

output, err := step.Run(models.ProjectCommandContext{
Workspace: "workspace",
RepoRelDir: ".",
CommentArgs: c.commentFlags,
}, c.extraArgs, tmpDir)
Equals(t, "", output)
if c.expErr {
ErrEquals(t, "cannot run apply with -target because we are applying an already generated plan. Instead, run -target with atlantis plan", err)
} else {
Ok(t, err)
}
})
}
}

0 comments on commit 58493f3

Please sign in to comment.