Skip to content

Commit 355d22a

Browse files
mdimicelijpalermo
authored andcommitted
add the ability to disable the '-auto-approve' option with terraform
1 parent 832246a commit 355d22a

File tree

7 files changed

+73
-44
lines changed

7 files changed

+73
-44
lines changed

application/configuration.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package application
33
import "github.com/cloudfoundry/bosh-bootloader/storage"
44

55
type GlobalConfiguration struct {
6-
StateDir string
7-
Debug bool
8-
Name string
9-
TerraformBinary bool
6+
StateDir string
7+
Debug bool
8+
Name string
9+
TerraformBinary bool
10+
DisableTfAutoApprove bool
1011
}
1112

1213
type StringSlice []string

bbl/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,15 @@ func main() {
9494
// Terraform
9595
terraformOutputBuffer := bytes.NewBuffer([]byte{})
9696
dotTerraformDir := filepath.Join(appConfig.Global.StateDir, "terraform", ".terraform")
97-
bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir, globals.TerraformBinary)
97+
bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir, globals.TerraformBinary, globals.DisableTfAutoApprove)
98+
9899
var (
99100
terraformCLI terraform.CLI
100101
out io.Writer
101102
)
102103
if appConfig.Global.Debug {
103104
errBuffer := io.MultiWriter(os.Stderr, terraformOutputBuffer)
104-
terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir, globals.TerraformBinary)
105+
terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir, globals.TerraformBinary, globals.DisableTfAutoApprove)
105106
out = os.Stdout
106107
} else {
107108
terraformCLI = bufferingCLI

commands/usage.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ Usage:
1313
bbl [GLOBAL OPTIONS] %s [OPTIONS]
1414
1515
Global Options:
16-
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
17-
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
18-
--debug [-d] Prints debugging output env:"BBL_DEBUG"
19-
--version [-v] Prints version
20-
--no-confirm [-n] No confirm
21-
--terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY"
16+
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
17+
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
18+
--debug [-d] Prints debugging output env:"BBL_DEBUG"
19+
--version [-v] Prints version
20+
--no-confirm [-n] No confirm
21+
--terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY"
22+
--disable-tf-auto-approve Do not use the '-auto-approve' option with terraform (debug mode required) env:"BBL_DISABLE_TF_AUTO_APPROVE"
23+
2224
%s
2325
`
2426
CommandUsage = `

commands/usage_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ Usage:
3939
bbl [GLOBAL OPTIONS] COMMAND [OPTIONS]
4040
4141
Global Options:
42-
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
43-
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
44-
--debug [-d] Prints debugging output env:"BBL_DEBUG"
45-
--version [-v] Prints version
46-
--no-confirm [-n] No confirm
47-
--terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY"
42+
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
43+
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
44+
--debug [-d] Prints debugging output env:"BBL_DEBUG"
45+
--version [-v] Prints version
46+
--no-confirm [-n] No confirm
47+
--terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY"
48+
--disable-tf-auto-approve Do not use the '-auto-approve' option with terraform (debug mode required) env:"BBL_DISABLE_TF_AUTO_APPROVE"
4849
4950
Basic Commands: A good place to start
5051
up Deploys BOSH director on an IAAS, creates CF/Concourse load balancers. Updates existing director.
@@ -85,12 +86,13 @@ Troubleshooting Commands:
8586
bbl [GLOBAL OPTIONS] my-command [OPTIONS]
8687
8788
Global Options:
88-
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
89-
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
90-
--debug [-d] Prints debugging output env:"BBL_DEBUG"
91-
--version [-v] Prints version
92-
--no-confirm [-n] No confirm
93-
--terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY"
89+
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
90+
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
91+
--debug [-d] Prints debugging output env:"BBL_DEBUG"
92+
--version [-v] Prints version
93+
--no-confirm [-n] No confirm
94+
--terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY"
95+
--disable-tf-auto-approve Do not use the '-auto-approve' option with terraform (debug mode required) env:"BBL_DISABLE_TF_AUTO_APPROVE"
9496
9597
[my-command command options]
9698
some message

config/global_flags.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package config
22

33
type GlobalFlags struct {
4-
Help bool `short:"h" long:"help"`
5-
Debug bool `short:"d" long:"debug" env:"BBL_DEBUG"`
6-
Version bool `short:"v" long:"version"`
7-
NoConfirm bool `short:"n" long:"no-confirm"`
8-
StateDir string `short:"s" long:"state-dir" env:"BBL_STATE_DIRECTORY"`
9-
StateBucket string ` long:"state-bucket" env:"BBL_STATE_BUCKET"`
10-
EnvID string ` long:"name"`
11-
IAAS string ` long:"iaas" env:"BBL_IAAS"`
12-
TerraformBinary string ` long:"terraform-binary" env:"BBL_TERRAFORM_BINARY"`
4+
Help bool `short:"h" long:"help"`
5+
Debug bool `short:"d" long:"debug" env:"BBL_DEBUG"`
6+
Version bool `short:"v" long:"version"`
7+
NoConfirm bool `short:"n" long:"no-confirm"`
8+
StateDir string `short:"s" long:"state-dir" env:"BBL_STATE_DIRECTORY"`
9+
StateBucket string ` long:"state-bucket" env:"BBL_STATE_BUCKET"`
10+
EnvID string ` long:"name"`
11+
IAAS string ` long:"iaas" env:"BBL_IAAS"`
12+
TerraformBinary string ` long:"terraform-binary" env:"BBL_TERRAFORM_BINARY"`
13+
DisableTfAutoApprove bool ` long:"disable-tf-auto-approve" env:"BBL_DISABLE_TF_AUTO_APPROVE"`
1314

1415
AWSAccessKeyID string `long:"aws-access-key-id" env:"BBL_AWS_ACCESS_KEY_ID"`
1516
AWSSecretAccessKey string `long:"aws-secret-access-key" env:"BBL_AWS_SECRET_ACCESS_KEY"`

terraform/cli.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
package terraform
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
67
"os"
78
"os/exec"
89
)
910

1011
type CLI struct {
11-
errorBuffer io.Writer
12-
outputBuffer io.Writer
13-
tfDataDir string
14-
terraformBinary string
12+
errorBuffer io.Writer
13+
outputBuffer io.Writer
14+
tfDataDir string
15+
terraformBinary string
16+
disableTfAutoApprove bool
1517
}
1618

17-
func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string, terraformBinary string) CLI {
19+
func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string, terraformBinary string, disableTfAutoApprove bool) CLI {
1820
return CLI{
19-
errorBuffer: errorBuffer,
20-
outputBuffer: outputBuffer,
21-
tfDataDir: tfDataDir,
22-
terraformBinary: terraformBinary,
21+
errorBuffer: errorBuffer,
22+
outputBuffer: outputBuffer,
23+
tfDataDir: tfDataDir,
24+
terraformBinary: terraformBinary,
25+
disableTfAutoApprove: disableTfAutoApprove,
2326
}
2427
}
2528

@@ -40,9 +43,14 @@ func (c CLI) RunWithEnv(stdout io.Writer, workingDirectory string, args []string
4043

4144
command.Stdout = io.MultiWriter(stdout, c.outputBuffer)
4245
command.Stderr = c.errorBuffer
46+
command.Stdin = os.Stdin
4347

4448
err = command.Run()
4549
if err != nil {
50+
_, isBuffer := c.errorBuffer.(*bytes.Buffer)
51+
if !isBuffer {
52+
return fmt.Errorf("command execution failed got: %s", err)
53+
}
4654
return fmt.Errorf("command execution failed got: %s stderr:\n %s", err, c.errorBuffer)
4755
}
4856

terraform/executor.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,14 @@ func (e Executor) Init() error {
177177
}
178178

179179
func (e Executor) Apply(credentials map[string]string) error {
180-
args := []string{"apply", "--auto-approve"}
180+
args := []string{"apply"}
181+
cli, ok := e.cli.(CLI)
182+
if !ok || !cli.disableTfAutoApprove || !e.debug {
183+
args = append(args, "--auto-approve")
184+
}
185+
if cli.disableTfAutoApprove && !e.debug {
186+
return fmt.Errorf("%s", "Debug mode is mandatory when terraform auto approve is disabled.")
187+
}
181188
for key, value := range credentials {
182189
arg := fmt.Sprintf("%s=%s", key, value)
183190
args = append(args, "-var", arg)
@@ -231,7 +238,14 @@ func (e Executor) Validate(credentials map[string]string) error {
231238
}
232239

233240
func (e Executor) Destroy(credentials map[string]string) error {
234-
args := []string{"destroy", "-auto-approve"}
241+
args := []string{"destroy"}
242+
cli, ok := e.cli.(CLI)
243+
if !ok || !cli.disableTfAutoApprove || !e.debug {
244+
args = append(args, "-auto-approve")
245+
}
246+
if cli.disableTfAutoApprove && !e.debug {
247+
return fmt.Errorf("%s", "Debug mode is mandatory when terraform auto approve is disabled.")
248+
}
235249
for key, value := range credentials {
236250
arg := fmt.Sprintf("%s=%s", key, value)
237251
args = append(args, "-var", arg)

0 commit comments

Comments
 (0)