Skip to content

Commit

Permalink
Change -auto-approve to -force when applying
Browse files Browse the repository at this point in the history
Since an early version of Terraform, the `destroy` command has always
had the `-force` flag to allow an auto approval of the interactive
prompt. 0.11 introduced `-auto-approve` as default to `false` when using
the `apply` command.

People often use wrappers when automating commands in Terraform, and the
inconsistency between `apply` and `destroy` means that additional logic
must be added to the wrappers to do similar functions. Both commands are
more or less able to run with similar syntax, and I could not find a
reason for the inconsistency between the two commands despite the
functionality being the same.

This commit updates the command in `apply` to use the `-force` flag
making working with the Terraform CLI a more consistent experience.
  • Loading branch information
surminus committed Jan 27, 2018
1 parent 5f72c97 commit 3ae9b8c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
8 changes: 4 additions & 4 deletions command/apply.go
Expand Up @@ -28,7 +28,7 @@ type ApplyCommand struct {
}

func (c *ApplyCommand) Run(args []string) int {
var destroyForce, refresh, autoApprove bool
var destroyForce, refresh, applyForce bool
args, err := c.Meta.process(args, true)
if err != nil {
return 1
Expand All @@ -45,7 +45,7 @@ func (c *ApplyCommand) Run(args []string) int {
}
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
if !c.Destroy {
cmdFlags.BoolVar(&autoApprove, "auto-approve", false, "skip interactive approval of plan before applying")
cmdFlags.BoolVar(&applyForce, "force", false, "do not ask for input to approve plan before applying")
}
cmdFlags.IntVar(
&c.Meta.parallelism, "parallelism", DefaultParallelism, "parallelism")
Expand Down Expand Up @@ -155,7 +155,7 @@ func (c *ApplyCommand) Run(args []string) int {
opReq.Plan = plan
opReq.PlanRefresh = refresh
opReq.Type = backend.OperationTypeApply
opReq.AutoApprove = autoApprove
opReq.AutoApprove = applyForce
opReq.DestroyForce = destroyForce

// Perform the operation
Expand Down Expand Up @@ -250,7 +250,7 @@ Options:
-lock-timeout=0s Duration to retry a state lock.
-auto-approve Skip interactive approval of plan before applying.
-force Don't ask for input to approve plan before applying.
-input=true Ask for input for variables if not directly set.
Expand Down
48 changes: 24 additions & 24 deletions command/apply_test.go
Expand Up @@ -35,7 +35,7 @@ func TestApply(t *testing.T) {

args := []string{
"-state", statePath,
"-auto-approve",
"-force",
testFixturePath("apply"),
}
if code := c.Run(args); code != 0 {
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestApply_lockedState(t *testing.T) {

args := []string{
"-state", statePath,
"-auto-approve",
"-force",
testFixturePath("apply"),
}
if code := c.Run(args); code == 0 {
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestApply_lockedStateWait(t *testing.T) {
args := []string{
"-state", statePath,
"-lock-timeout", "4s",
"-auto-approve",
"-force",
testFixturePath("apply"),
}
if code := c.Run(args); code != 0 {
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestApply_parallelism(t *testing.T) {

args := []string{
"-state", statePath,
"-auto-approve",
"-force",
fmt.Sprintf("-parallelism=%d", par),
testFixturePath("parallelism"),
}
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestApply_configInvalid(t *testing.T) {

args := []string{
"-state", testTempFile(t),
"-auto-approve",
"-force",
testFixturePath("apply-config-invalid"),
}
if code := c.Run(args); code != 1 {
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestApply_defaultState(t *testing.T) {
serial := localState.State().Serial

args := []string{
"-auto-approve",
"-force",
testFixturePath("apply"),
}
if code := c.Run(args); code != 0 {
Expand Down Expand Up @@ -350,7 +350,7 @@ func TestApply_error(t *testing.T) {

args := []string{
"-state", statePath,
"-auto-approve",
"-force",
testFixturePath("apply-error"),
}
if code := c.Run(args); code != 1 {
Expand Down Expand Up @@ -392,7 +392,7 @@ func TestApply_input(t *testing.T) {

args := []string{
"-state", statePath,
"-auto-approve",
"-force",
testFixturePath("apply-input"),
}
if code := c.Run(args); code != 0 {
Expand Down Expand Up @@ -428,7 +428,7 @@ func TestApply_inputPartial(t *testing.T) {

args := []string{
"-state", statePath,
"-auto-approve",
"-force",
"-var", "foo=foovalue",
testFixturePath("apply-input-partial"),
}
Expand Down Expand Up @@ -469,7 +469,7 @@ func TestApply_noArgs(t *testing.T) {

args := []string{
"-state", statePath,
"-auto-approve",
"-force",
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
Expand Down Expand Up @@ -793,7 +793,7 @@ func TestApply_refresh(t *testing.T) {

args := []string{
"-state", statePath,
"-auto-approve",
"-force",
testFixturePath("apply"),
}
if code := c.Run(args); code != 0 {
Expand Down Expand Up @@ -877,7 +877,7 @@ func TestApply_shutdown(t *testing.T) {

args := []string{
"-state", statePath,
"-auto-approve",
"-force",
testFixturePath("apply-shutdown"),
}
if code := c.Run(args); code != 0 {
Expand Down Expand Up @@ -937,7 +937,7 @@ func TestApply_state(t *testing.T) {
// Run the apply command pointing to our existing state
args := []string{
"-state", statePath,
"-auto-approve",
"-force",
testFixturePath("apply"),
}
if code := c.Run(args); code != 0 {
Expand Down Expand Up @@ -1012,7 +1012,7 @@ func TestApply_sensitiveOutput(t *testing.T) {

args := []string{
"-state", statePath,
"-auto-approve",
"-force",
testFixturePath("apply-sensitive-output"),
}

Expand Down Expand Up @@ -1045,7 +1045,7 @@ func TestApply_stateFuture(t *testing.T) {

args := []string{
"-state", statePath,
"-auto-approve",
"-force",
testFixturePath("apply"),
}
if code := c.Run(args); code == 0 {
Expand Down Expand Up @@ -1077,7 +1077,7 @@ func TestApply_statePast(t *testing.T) {

args := []string{
"-state", statePath,
"-auto-approve",
"-force",
testFixturePath("apply"),
}
if code := c.Run(args); code != 0 {
Expand Down Expand Up @@ -1110,7 +1110,7 @@ func TestApply_vars(t *testing.T) {
}

args := []string{
"-auto-approve",
"-force",
"-var", "foo=bar",
"-state", statePath,
testFixturePath("apply-vars"),
Expand Down Expand Up @@ -1154,7 +1154,7 @@ func TestApply_varFile(t *testing.T) {
}

args := []string{
"-auto-approve",
"-force",
"-var-file", varFilePath,
"-state", statePath,
testFixturePath("apply-vars"),
Expand Down Expand Up @@ -1208,7 +1208,7 @@ func TestApply_varFileDefault(t *testing.T) {
}

args := []string{
"-auto-approve",
"-force",
"-state", statePath,
testFixturePath("apply-vars"),
}
Expand Down Expand Up @@ -1261,7 +1261,7 @@ func TestApply_varFileDefaultJSON(t *testing.T) {
}

args := []string{
"-auto-approve",
"-force",
"-state", statePath,
testFixturePath("apply-vars"),
}
Expand Down Expand Up @@ -1314,7 +1314,7 @@ func TestApply_backup(t *testing.T) {

// Run the apply command pointing to our existing state
args := []string{
"-auto-approve",
"-force",
"-state", statePath,
"-backup", backupPath,
testFixturePath("apply"),
Expand Down Expand Up @@ -1365,7 +1365,7 @@ func TestApply_disableBackup(t *testing.T) {

// Run the apply command pointing to our existing state
args := []string{
"-auto-approve",
"-force",
"-state", statePath,
"-backup", "-",
testFixturePath("apply"),
Expand Down Expand Up @@ -1424,7 +1424,7 @@ func TestApply_terraformEnv(t *testing.T) {
}

args := []string{
"-auto-approve",
"-force",
"-state", statePath,
testFixturePath("apply-terraform-env"),
}
Expand Down Expand Up @@ -1480,7 +1480,7 @@ func TestApply_terraformEnvNonDefault(t *testing.T) {
}

args := []string{
"-auto-approve",
"-force",
testFixturePath("apply-terraform-env"),
}
if code := c.Run(args); code != 0 {
Expand Down
2 changes: 1 addition & 1 deletion command/e2etest/automation_test.go
Expand Up @@ -149,7 +149,7 @@ func TestAutoApplyInAutomation(t *testing.T) {
}

//// APPLY
stdout, stderr, err = tf.Run("apply", "-input=false", "-auto-approve")
stdout, stderr, err = tf.Run("apply", "-input=false", "-force")
if err != nil {
t.Fatalf("unexpected apply error: %s\nstderr:\n%s", err, stderr)
}
Expand Down
2 changes: 1 addition & 1 deletion website/docs/commands/apply.html.markdown
Expand Up @@ -33,7 +33,7 @@ The command-line flags are all optional. The list of available flags are:

* `-input=true` - Ask for input for variables if not directly set.

* `-auto-approve` - Skip interactive approval of plan before applying.
* `-force` - Don't ask for input to approve plan before applying.

* `-no-color` - Disables output with coloring.

Expand Down
4 changes: 2 additions & 2 deletions website/guides/running-terraform-in-automation.html.md
Expand Up @@ -174,10 +174,10 @@ Where manual approval is not required, a simpler sequence of commands
can be used:

* `terraform init -input=false`
* `terraform apply -input=false -auto-approve`
* `terraform apply -input=false -force`

This variant of the `apply` command implicitly creates a new plan and then
immediately applies it. The `-auto-approve` option tells Terraform not
immediately applies it. The `-force` option tells Terraform not
to require interactive approval of the plan before applying it.

~> When Terraform is empowered to make destructive changes to infrastructure,
Expand Down
4 changes: 2 additions & 2 deletions website/upgrade-guides/0-11.html.markdown
Expand Up @@ -44,7 +44,7 @@ default unless a plan file is provided on the command line. When
it is always recommended to separate plan from apply, but if existing automation
was running `terraform apply` with no arguments it may now be necessary to
update it to either generate an explicit plan using `terraform plan -out=...`
or to run `terraform apply -auto-approve` to bypass the interactive confirmation
or to run `terraform apply -force` to bypass the interactive confirmation
step. The latter should be done only in unimportant environments.

**Action:** For interactive use in a terminal, prefer to use `terraform apply`
Expand All @@ -53,7 +53,7 @@ followed by `terraform apply tfplan`.

**Action:** Update any automation scripts that run Terraform non-interactively
so that they either use separated plan and apply or override the confirmation
behavior using the `-auto-approve` option.
behavior using the `-force` option.

## Relative Paths in Module `source`

Expand Down

0 comments on commit 3ae9b8c

Please sign in to comment.