0
0
mirror of https://github.com/thegeeklab/wp-opentofu.git synced 2024-06-03 04:49:42 +02:00

Add support for terraform destroy command

This commit is contained in:
marcin.suterski 2017-08-08 17:16:20 -04:00
parent 917f1cc288
commit ff7e2e547e
No known key found for this signature in database
GPG Key ID: A6CF8B51D99C0C92
3 changed files with 176 additions and 2 deletions

View File

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

View File

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

133
plugin_test.go Normal file
View File

@ -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)
}
})
}
}