From a3571a2b101ce45f6430961924981e9468328879 Mon Sep 17 00:00:00 2001 From: Jacob McCann Date: Wed, 14 Feb 2018 09:35:07 -0600 Subject: [PATCH] Provide actions to run --- main.go | 19 +++++++------------ plugin.go | 48 +++++++++++++++++++++++++++--------------------- plugin_test.go | 40 +++++++--------------------------------- 3 files changed, 41 insertions(+), 66 deletions(-) diff --git a/main.go b/main.go index 65bc527..c1b97e9 100644 --- a/main.go +++ b/main.go @@ -23,11 +23,6 @@ func main() { // plugin args // - cli.BoolFlag{ - Name: "plan", - Usage: "calculates a plan but does NOT apply it", - EnvVar: "PLUGIN_PLAN", - }, cli.StringFlag{ Name: "init_options", Usage: "options for the init command. See https://www.terraform.io/docs/commands/init.html", @@ -85,16 +80,17 @@ func main() { Usage: "a list of var files to use. Each value is passed as -var-file=", EnvVar: "PLUGIN_VAR_FILES", }, - cli.BoolFlag{ - Name: "destroy", - Usage: "destory all resurces", - EnvVar: "PLUGIN_DESTROY", - }, cli.StringFlag{ Name: "tf.version", Usage: "terraform version to use", EnvVar: "PLUGIN_TF_VERSION", }, + cli.StringSliceFlag{ + Name: "actions", + Usage: "a list of actions to have terraform perform", + EnvVar: "PLUGIN_ACTIONS", + Value: &cli.StringSlice{"validate", "apply"}, + }, } if err := app.Run(os.Args); err != nil { @@ -129,7 +125,7 @@ func run(c *cli.Context) error { plugin := Plugin{ Config: Config{ - Plan: c.Bool("plan"), + Actions: c.StringSlice("actions"), Vars: vars, Secrets: secrets, InitOptions: initOptions, @@ -140,7 +136,6 @@ func run(c *cli.Context) error { Parallelism: c.Int("parallelism"), Targets: c.StringSlice("targets"), VarFiles: c.StringSlice("var_files"), - Destroy: c.Bool("destroy"), }, Terraform: Terraform{ Version: c.String("tf.version"), diff --git a/plugin.go b/plugin.go index 81c4d08..3be899b 100644 --- a/plugin.go +++ b/plugin.go @@ -19,7 +19,7 @@ import ( type ( // Config holds input parameters for the plugin Config struct { - Plan bool + Actions []string Vars map[string]string Secrets map[string]string InitOptions InitOptions @@ -30,7 +30,6 @@ type ( Parallelism int Targets []string VarFiles []string - Destroy bool } // InitOptions include options for the Terraform's init command @@ -73,15 +72,29 @@ func (p Plugin) Exec() error { } commands = append(commands, deleteCache()) - commands = append(commands, initCommand(p.Config.InitOptions)) - commands = append(commands, getModules()) - commands = append(commands, validateCommand(p.Config)) - commands = append(commands, planCommand(p.Config)) - if !p.Config.Plan { - commands = append(commands, terraformCommand(p.Config)) + + // Add commands listed from Actions + for _, action := range p.Config.Actions { + switch action { + case "validate": + commands = append(commands, tfValidate(p.Config)) + case "plan": + commands = append(commands, tfPlan(p.Config, false)) + case "plan-destroy": + commands = append(commands, tfPlan(p.Config, true)) + case "apply": + commands = append(commands, tfPlan(p.Config, false)) + commands = append(commands, tfApply(p.Config)) + case "destroy": + commands = append(commands, tfPlan(p.Config, true)) + commands = append(commands, tfDestroy(p.Config)) + default: + return fmt.Errorf("valid actions are: validate, plan, apply, destroy. You provided %s", action) + } } + commands = append(commands, deleteCache()) for _, c := range commands { @@ -173,7 +186,7 @@ func getModules() *exec.Cmd { ) } -func validateCommand(config Config) *exec.Cmd { +func tfValidate(config Config) *exec.Cmd { args := []string{ "validate", } @@ -190,11 +203,12 @@ func validateCommand(config Config) *exec.Cmd { ) } -func planCommand(config Config) *exec.Cmd { +func tfPlan(config Config, destroy bool) *exec.Cmd { args := []string{ "plan", } - if config.Destroy { + + if destroy { args = append(args, "-destroy") } else { args = append(args, "-out=plan.tfout") @@ -225,15 +239,7 @@ 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 tfApply(config Config) *exec.Cmd { args := []string{ "apply", } @@ -256,7 +262,7 @@ func applyCommand(config Config) *exec.Cmd { ) } -func destroyCommand(config Config) *exec.Cmd { +func tfDestroy(config Config) *exec.Cmd { args := []string{ "destroy", } diff --git a/plugin_test.go b/plugin_test.go index 6073d9c..4c6a48f 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -36,7 +36,7 @@ func Test_destroyCommand(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := destroyCommand(tt.args.config); !reflect.DeepEqual(got, tt.want) { + if got := tfDestroy(tt.args.config); !reflect.DeepEqual(got, tt.want) { t.Errorf("destroyCommand() = %v, want %v", got, tt.want) } }) @@ -70,42 +70,13 @@ func Test_applyCommand(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := applyCommand(tt.args.config); !reflect.DeepEqual(got, tt.want) { + if got := tfApply(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 @@ -113,22 +84,25 @@ func Test_planCommand(t *testing.T) { tests := []struct { name string args args + destroy bool want *exec.Cmd }{ { "default", args{config: Config{}}, + false, exec.Command("terraform", "plan", "-out=plan.tfout"), }, { "destroy", - args{config: Config{Destroy: true}}, + args{config: Config{}}, + 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) { + if got := tfPlan(tt.args.config, tt.destroy); !reflect.DeepEqual(got, tt.want) { t.Errorf("planCommand() = %v, want %v", got, tt.want) } })