0
0
mirror of https://github.com/thegeeklab/wp-opentofu.git synced 2024-11-13 23:40:39 +00:00

Provide actions to run

This commit is contained in:
Jacob McCann 2018-02-14 09:35:07 -06:00
parent 082b4073ad
commit a3571a2b10
3 changed files with 41 additions and 66 deletions

19
main.go
View File

@ -23,11 +23,6 @@ func main() {
// plugin args // plugin args
// //
cli.BoolFlag{
Name: "plan",
Usage: "calculates a plan but does NOT apply it",
EnvVar: "PLUGIN_PLAN",
},
cli.StringFlag{ cli.StringFlag{
Name: "init_options", Name: "init_options",
Usage: "options for the init command. See https://www.terraform.io/docs/commands/init.html", 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=<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",
},
cli.StringFlag{ cli.StringFlag{
Name: "tf.version", Name: "tf.version",
Usage: "terraform version to use", Usage: "terraform version to use",
EnvVar: "PLUGIN_TF_VERSION", 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 { if err := app.Run(os.Args); err != nil {
@ -129,7 +125,7 @@ func run(c *cli.Context) error {
plugin := Plugin{ plugin := Plugin{
Config: Config{ Config: Config{
Plan: c.Bool("plan"), Actions: c.StringSlice("actions"),
Vars: vars, Vars: vars,
Secrets: secrets, Secrets: secrets,
InitOptions: initOptions, InitOptions: initOptions,
@ -140,7 +136,6 @@ 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"),
}, },
Terraform: Terraform{ Terraform: Terraform{
Version: c.String("tf.version"), Version: c.String("tf.version"),

View File

@ -19,7 +19,7 @@ import (
type ( type (
// Config holds input parameters for the plugin // Config holds input parameters for the plugin
Config struct { Config struct {
Plan bool Actions []string
Vars map[string]string Vars map[string]string
Secrets map[string]string Secrets map[string]string
InitOptions InitOptions InitOptions InitOptions
@ -30,7 +30,6 @@ 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 include options for the Terraform's init command
@ -73,15 +72,29 @@ func (p Plugin) Exec() error {
} }
commands = append(commands, deleteCache()) commands = append(commands, deleteCache())
commands = append(commands, initCommand(p.Config.InitOptions)) commands = append(commands, initCommand(p.Config.InitOptions))
commands = append(commands, getModules()) commands = append(commands, getModules())
commands = append(commands, validateCommand(p.Config))
commands = append(commands, planCommand(p.Config)) // Add commands listed from Actions
if !p.Config.Plan { for _, action := range p.Config.Actions {
commands = append(commands, terraformCommand(p.Config)) 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()) commands = append(commands, deleteCache())
for _, c := range commands { 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{ args := []string{
"validate", "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{ args := []string{
"plan", "plan",
} }
if config.Destroy {
if destroy {
args = append(args, "-destroy") args = append(args, "-destroy")
} else { } else {
args = append(args, "-out=plan.tfout") args = append(args, "-out=plan.tfout")
@ -225,15 +239,7 @@ func planCommand(config Config) *exec.Cmd {
) )
} }
func terraformCommand(config Config) *exec.Cmd { func tfApply(config Config) *exec.Cmd {
if config.Destroy {
return destroyCommand(config)
}
return applyCommand(config)
}
func applyCommand(config Config) *exec.Cmd {
args := []string{ args := []string{
"apply", "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{ args := []string{
"destroy", "destroy",
} }

View File

@ -36,7 +36,7 @@ func Test_destroyCommand(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) t.Errorf("destroyCommand() = %v, want %v", got, tt.want)
} }
}) })
@ -70,42 +70,13 @@ func Test_applyCommand(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) 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) { func Test_planCommand(t *testing.T) {
type args struct { type args struct {
config Config config Config
@ -113,22 +84,25 @@ func Test_planCommand(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
args args args args
destroy bool
want *exec.Cmd want *exec.Cmd
}{ }{
{ {
"default", "default",
args{config: Config{}}, args{config: Config{}},
false,
exec.Command("terraform", "plan", "-out=plan.tfout"), exec.Command("terraform", "plan", "-out=plan.tfout"),
}, },
{ {
"destroy", "destroy",
args{config: Config{Destroy: true}}, args{config: Config{}},
true,
exec.Command("terraform", "plan", "-destroy"), exec.Command("terraform", "plan", "-destroy"),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) t.Errorf("planCommand() = %v, want %v", got, tt.want)
} }
}) })