mirror of
https://github.com/thegeeklab/wp-opentofu.git
synced 2024-11-21 14:20:40 +00:00
Provide actions to run
This commit is contained in:
parent
082b4073ad
commit
a3571a2b10
19
main.go
19
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=<value>",
|
||||
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"),
|
||||
|
48
plugin.go
48
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",
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user