Browse Source

Add support for terraform destroy command

pull/50/head
marcin.suterski 7 years ago
parent
commit
ff7e2e547e
No known key found for this signature in database GPG Key ID: A6CF8B51D99C0C92
  1. 6
      main.go
  2. 39
      plugin.go
  3. 133
      plugin_test.go

6
main.go

@ -85,6 +85,11 @@ func main() {
Usage: "a list of var files to use. Each value is passed as -var-file=<value>", Usage: "a list of var files to use. Each value is passed as -var-file=<value>",
EnvVar: "PLUGIN_VAR_FILES", EnvVar: "PLUGIN_VAR_FILES",
}, },
cli.BoolFlag{
Name: "destroy",
Usage: "destory all resurces",
EnvVar: "PLUGIN_DESTROY",
},
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
@ -130,6 +135,7 @@ func run(c *cli.Context) error {
Parallelism: c.Int("parallelism"), Parallelism: c.Int("parallelism"),
Targets: c.StringSlice("targets"), Targets: c.StringSlice("targets"),
VarFiles: c.StringSlice("var_files"), VarFiles: c.StringSlice("var_files"),
Destroy: c.Bool("destroy"),
}, },
} }

39
plugin.go

@ -16,6 +16,7 @@ import (
) )
type ( type (
// Config holds input parameters for the plugin
Config struct { Config struct {
Plan bool Plan bool
Vars map[string]string Vars map[string]string
@ -28,19 +29,23 @@ type (
Parallelism int Parallelism int
Targets []string Targets []string
VarFiles []string VarFiles []string
Destroy bool
} }
// InitOptions include options for the Terraform's init command
InitOptions struct { InitOptions struct {
BackendConfig []string `json:"backend-config"` BackendConfig []string `json:"backend-config"`
Lock *bool `json:"lock"` Lock *bool `json:"lock"`
LockTimeout string `json:"lock-timeout"` LockTimeout string `json:"lock-timeout"`
} }
// Plugin represents the plugin instance to be executed
Plugin struct { Plugin struct {
Config Config Config Config
} }
) )
// Exec executes the plugin
func (p Plugin) Exec() error { func (p Plugin) Exec() error {
if p.Config.RoleARN != "" { if p.Config.RoleARN != "" {
assumeRole(p.Config.RoleARN) assumeRole(p.Config.RoleARN)
@ -64,7 +69,7 @@ func (p Plugin) Exec() error {
commands = append(commands, validateCommand()) commands = append(commands, validateCommand())
commands = append(commands, planCommand(p.Config)) commands = append(commands, planCommand(p.Config))
if !p.Config.Plan { if !p.Config.Plan {
commands = append(commands, applyCommand(p.Config)) commands = append(commands, terraformCommand(p.Config))
} }
commands = append(commands, deleteCache()) commands = append(commands, deleteCache())
@ -165,8 +170,13 @@ func validateCommand() *exec.Cmd {
func planCommand(config Config) *exec.Cmd { func planCommand(config Config) *exec.Cmd {
args := []string{ args := []string{
"plan", "plan",
"-out=plan.tfout",
} }
if config.Destroy {
args = append(args, "-destroy")
} else {
args = append(args, "-out=plan.tfout")
}
for _, v := range config.Targets { for _, v := range config.Targets {
args = append(args, "--target", fmt.Sprintf("%s", v)) args = append(args, "--target", fmt.Sprintf("%s", v))
} }
@ -190,6 +200,14 @@ func planCommand(config Config) *exec.Cmd {
) )
} }
func terraformCommand(config Config) *exec.Cmd {
if config.Destroy {
return destroyCommand(config)
}
return applyCommand(config)
}
func applyCommand(config Config) *exec.Cmd { func applyCommand(config Config) *exec.Cmd {
args := []string{ args := []string{
"apply", "apply",
@ -207,6 +225,23 @@ func applyCommand(config Config) *exec.Cmd {
) )
} }
func destroyCommand(config Config) *exec.Cmd {
args := []string{
"destroy",
}
for _, v := range config.Targets {
args = append(args, fmt.Sprintf("-target=%s", v))
}
if config.Parallelism > 0 {
args = append(args, fmt.Sprintf("-parallelism=%d", config.Parallelism))
}
args = append(args, "-force")
return exec.Command(
"terraform",
args...,
)
}
func assumeRole(roleArn string) { func assumeRole(roleArn string) {
client := sts.New(session.New()) client := sts.New(session.New())
duration := time.Hour * 1 duration := time.Hour * 1

133
plugin_test.go

@ -0,0 +1,133 @@
package main
import (
"os/exec"
"reflect"
"testing"
)
func Test_destroyCommand(t *testing.T) {
type args struct {
config Config
}
tests := []struct {
name string
args args
want *exec.Cmd
}{
{
"default",
args{config: Config{}},
exec.Command("terraform", "destroy", "-force"),
},
{
"with targets",
args{config: Config{Targets: []string{"target1", "target2"}}},
exec.Command("terraform", "destroy", "-target=target1", "-target=target2", "-force"),
},
{
"with parallelism",
args{config: Config{Parallelism: 5}},
exec.Command("terraform", "destroy", "-parallelism=5", "-force"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := destroyCommand(tt.args.config); !reflect.DeepEqual(got, tt.want) {
t.Errorf("destroyCommand() = %v, want %v", got, tt.want)
}
})
}
}
func Test_applyCommand(t *testing.T) {
type args struct {
config Config
}
tests := []struct {
name string
args args
want *exec.Cmd
}{
{
"default",
args{config: Config{}},
exec.Command("terraform", "apply", "plan.tfout"),
},
{
"with targets",
args{config: Config{Targets: []string{"target1", "target2"}}},
exec.Command("terraform", "apply", "--target", "target1", "--target", "target2", "plan.tfout"),
},
{
"with parallelism",
args{config: Config{Parallelism: 5}},
exec.Command("terraform", "apply", "-parallelism=5", "plan.tfout"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := applyCommand(tt.args.config); !reflect.DeepEqual(got, tt.want) {
t.Errorf("applyCommand() = %v, want %v", got, tt.want)
}
})
}
}
func Test_terraformCommand(t *testing.T) {
type args struct {
config Config
}
tests := []struct {
name string
args args
want *exec.Cmd
}{
{
"default",
args{config: Config{}},
exec.Command("terraform", "apply", "plan.tfout"),
},
{
"destroy",
args{config: Config{Destroy: true}},
exec.Command("terraform", "destroy", "-force"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := terraformCommand(tt.args.config); !reflect.DeepEqual(got, tt.want) {
t.Errorf("terraformCommand() = %v, want %v", got, tt.want)
}
})
}
}
func Test_planCommand(t *testing.T) {
type args struct {
config Config
}
tests := []struct {
name string
args args
want *exec.Cmd
}{
{
"default",
args{config: Config{}},
exec.Command("terraform", "plan", "-out=plan.tfout"),
},
{
"destroy",
args{config: Config{Destroy: true}},
exec.Command("terraform", "plan", "-destroy"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := planCommand(tt.args.config); !reflect.DeepEqual(got, tt.want) {
t.Errorf("planCommand() = %v, want %v", got, tt.want)
}
})
}
}
Loading…
Cancel
Save