mirror of
https://github.com/thegeeklab/wp-opentofu.git
synced 2024-11-24 23:30:39 +00:00
Changed implementation to make the code simpler by setting the env var
This commit is contained in:
parent
baff3b84aa
commit
5f01ae24b6
92
plugin.go
92
plugin.go
@ -87,6 +87,12 @@ func (p Plugin) Exec() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var terraformDataDir string = ".terraform"
|
||||||
|
if p.Config.TerraformDataDir != "" {
|
||||||
|
terraformDataDir = p.Config.TerraformDataDir
|
||||||
|
os.Setenv("TF_DATA_DIR", p.Config.TerraformDataDir)
|
||||||
|
}
|
||||||
|
|
||||||
var commands []*exec.Cmd
|
var commands []*exec.Cmd
|
||||||
|
|
||||||
commands = append(commands, exec.Command("terraform", "version"))
|
commands = append(commands, exec.Command("terraform", "version"))
|
||||||
@ -97,8 +103,8 @@ func (p Plugin) Exec() error {
|
|||||||
commands = append(commands, installCaCert(p.Config.Cacert))
|
commands = append(commands, installCaCert(p.Config.Cacert))
|
||||||
}
|
}
|
||||||
|
|
||||||
commands = append(commands, deleteCache(p.Config))
|
commands = append(commands, deleteCache(terraformDataDir))
|
||||||
commands = append(commands, initCommand(p.Config))
|
commands = append(commands, initCommand(p.Config.InitOptions))
|
||||||
commands = append(commands, getModules())
|
commands = append(commands, getModules())
|
||||||
|
|
||||||
// Add commands listed from Actions
|
// Add commands listed from Actions
|
||||||
@ -121,7 +127,7 @@ func (p Plugin) Exec() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
commands = append(commands, deleteCache(p.Config))
|
commands = append(commands, deleteCache(terraformDataDir))
|
||||||
|
|
||||||
for _, c := range commands {
|
for _, c := range commands {
|
||||||
if c.Dir == "" {
|
if c.Dir == "" {
|
||||||
@ -184,8 +190,7 @@ func assumeRole(roleArn string) {
|
|||||||
os.Setenv("AWS_SESSION_TOKEN", value.SessionToken)
|
os.Setenv("AWS_SESSION_TOKEN", value.SessionToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteCache(config Config) *exec.Cmd {
|
func deleteCache(terraformDataDir string) *exec.Cmd {
|
||||||
terraformDataDir := getTerraformDataDir(config)
|
|
||||||
return exec.Command(
|
return exec.Command(
|
||||||
"rm",
|
"rm",
|
||||||
"-rf",
|
"-rf",
|
||||||
@ -200,30 +205,30 @@ func getModules() *exec.Cmd {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initCommand(config Config) *exec.Cmd {
|
func initCommand(config InitOptions) *exec.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"init",
|
"init",
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range config.InitOptions.BackendConfig {
|
for _, v := range config.BackendConfig {
|
||||||
args = append(args, fmt.Sprintf("-backend-config=%s", v))
|
args = append(args, fmt.Sprintf("-backend-config=%s", v))
|
||||||
}
|
}
|
||||||
|
|
||||||
// True is default in TF
|
// True is default in TF
|
||||||
if config.InitOptions.Lock != nil {
|
if config.Lock != nil {
|
||||||
args = append(args, fmt.Sprintf("-lock=%t", *config.InitOptions.Lock))
|
args = append(args, fmt.Sprintf("-lock=%t", *config.Lock))
|
||||||
}
|
}
|
||||||
|
|
||||||
// "0s" is default in TF
|
// "0s" is default in TF
|
||||||
if config.InitOptions.LockTimeout != "" {
|
if config.LockTimeout != "" {
|
||||||
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout))
|
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.LockTimeout))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fail Terraform execution on prompt
|
// Fail Terraform execution on prompt
|
||||||
args = append(args, "-input=false")
|
args = append(args, "-input=false")
|
||||||
|
|
||||||
return createTerraformCommand(
|
return exec.Command(
|
||||||
config,
|
"terraform",
|
||||||
args...,
|
args...,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -255,10 +260,10 @@ func tfApply(config Config) *exec.Cmd {
|
|||||||
if config.InitOptions.LockTimeout != "" {
|
if config.InitOptions.LockTimeout != "" {
|
||||||
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout))
|
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout))
|
||||||
}
|
}
|
||||||
args = append(args, getTfoutPath(config))
|
args = append(args, getTfoutPath())
|
||||||
|
|
||||||
return createTerraformCommand(
|
return exec.Command(
|
||||||
config,
|
"terraform",
|
||||||
args...,
|
args...,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -282,8 +287,8 @@ func tfDestroy(config Config) *exec.Cmd {
|
|||||||
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout))
|
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout))
|
||||||
}
|
}
|
||||||
args = append(args, "-force")
|
args = append(args, "-force")
|
||||||
return createTerraformCommand(
|
return exec.Command(
|
||||||
config,
|
"terraform",
|
||||||
args...,
|
args...,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -296,7 +301,7 @@ func tfPlan(config Config, destroy bool) *exec.Cmd {
|
|||||||
if destroy {
|
if destroy {
|
||||||
args = append(args, "-destroy")
|
args = append(args, "-destroy")
|
||||||
} else {
|
} else {
|
||||||
args = append(args, fmt.Sprintf("-out=%s", getTfoutPath(config)))
|
args = append(args, fmt.Sprintf("-out=%s", getTfoutPath()))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range config.Targets {
|
for _, v := range config.Targets {
|
||||||
@ -313,8 +318,8 @@ func tfPlan(config Config, destroy bool) *exec.Cmd {
|
|||||||
if config.InitOptions.LockTimeout != "" {
|
if config.InitOptions.LockTimeout != "" {
|
||||||
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout))
|
args = append(args, fmt.Sprintf("-lock-timeout=%s", config.InitOptions.LockTimeout))
|
||||||
}
|
}
|
||||||
return createTerraformCommand(
|
return exec.Command(
|
||||||
config,
|
"terraform",
|
||||||
args...,
|
args...,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -329,8 +334,8 @@ func tfValidate(config Config) *exec.Cmd {
|
|||||||
for k, v := range config.Vars {
|
for k, v := range config.Vars {
|
||||||
args = append(args, "-var", fmt.Sprintf("%s=%s", k, v))
|
args = append(args, "-var", fmt.Sprintf("%s=%s", k, v))
|
||||||
}
|
}
|
||||||
return createTerraformCommand(
|
return exec.Command(
|
||||||
config,
|
"terraform",
|
||||||
args...,
|
args...,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -351,46 +356,15 @@ func tfFmt(config Config) *exec.Cmd {
|
|||||||
if config.FmtOptions.Check != nil {
|
if config.FmtOptions.Check != nil {
|
||||||
args = append(args, fmt.Sprintf("-check=%t", *config.FmtOptions.Check))
|
args = append(args, fmt.Sprintf("-check=%t", *config.FmtOptions.Check))
|
||||||
}
|
}
|
||||||
return createTerraformCommand(
|
return exec.Command(
|
||||||
config,
|
"terraform",
|
||||||
args...,
|
args...,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTerraformDataDir(config Config) string {
|
func getTfoutPath() string {
|
||||||
// Override terraform data dir
|
terraformDataDir := os.Getenv("TF_DATA_DIR")
|
||||||
var terraformDataDir string
|
if terraformDataDir == ".terraform" || terraformDataDir == "" {
|
||||||
if config.TerraformDataDir != "" {
|
|
||||||
terraformDataDir = config.TerraformDataDir
|
|
||||||
} else if os.Getenv("TF_DATA_DIR") != "" {
|
|
||||||
terraformDataDir = os.Getenv("TF_DATA_DIR")
|
|
||||||
} else {
|
|
||||||
terraformDataDir = ".terraform"
|
|
||||||
}
|
|
||||||
return terraformDataDir
|
|
||||||
}
|
|
||||||
|
|
||||||
func createEnvironmentVariables(config Config) []string {
|
|
||||||
var environmentVariables []string = []string{}
|
|
||||||
|
|
||||||
terraformDataDir := getTerraformDataDir(config)
|
|
||||||
if terraformDataDir != ".terraform" {
|
|
||||||
environmentVariables = append(environmentVariables, fmt.Sprintf("TF_DATA_DIR=%s", terraformDataDir))
|
|
||||||
}
|
|
||||||
return environmentVariables
|
|
||||||
}
|
|
||||||
|
|
||||||
func createTerraformCommand(config Config, args ...string) *exec.Cmd {
|
|
||||||
command := exec.Command("terraform", args...)
|
|
||||||
environmentVariables := createEnvironmentVariables(config)
|
|
||||||
if len(environmentVariables) > 0 {
|
|
||||||
command.Env = append(os.Environ(), environmentVariables...)
|
|
||||||
}
|
|
||||||
return command
|
|
||||||
}
|
|
||||||
func getTfoutPath(config Config) string {
|
|
||||||
terraformDataDir := getTerraformDataDir(config)
|
|
||||||
if terraformDataDir == ".terraform" {
|
|
||||||
return "plan.tfout"
|
return "plan.tfout"
|
||||||
} else {
|
} else {
|
||||||
return fmt.Sprintf("%s.plan.tfout", terraformDataDir)
|
return fmt.Sprintf("%s.plan.tfout", terraformDataDir)
|
||||||
|
@ -212,50 +212,33 @@ func TestPlugin(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
args args
|
args args
|
||||||
want *exec.Cmd
|
want *exec.Cmd
|
||||||
expectedEnvVars []string
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"with TerraformDataDir",
|
"with TerraformDataDir",
|
||||||
args{config: Config{TerraformDataDir: ".overriden_terraform_dir"}},
|
args{config: Config{TerraformDataDir: ".overriden_terraform_dir"}},
|
||||||
exec.Command("terraform", "apply", ".overriden_terraform_dir.plan.tfout"),
|
exec.Command("terraform", "apply", ".overriden_terraform_dir.plan.tfout"),
|
||||||
[]string{"TF_DATA_DIR=.overriden_terraform_dir"},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"with TerraformDataDir value as .terraform",
|
"with TerraformDataDir value as .terraform",
|
||||||
args{config: Config{TerraformDataDir: ".terraform"}},
|
args{config: Config{TerraformDataDir: ".terraform"}},
|
||||||
exec.Command("terraform", "apply", "plan.tfout"),
|
exec.Command("terraform", "apply", "plan.tfout"),
|
||||||
[]string{},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"without TerraformDataDir",
|
"without TerraformDataDir",
|
||||||
args{config: Config{}},
|
args{config: Config{}},
|
||||||
exec.Command("terraform", "apply", "plan.tfout"),
|
exec.Command("terraform", "apply", "plan.tfout"),
|
||||||
[]string{},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
os.Setenv("TF_DATA_DIR", tt.args.config.TerraformDataDir)
|
||||||
applied := tfApply(tt.args.config)
|
applied := tfApply(tt.args.config)
|
||||||
appliedEnv := applied.Env
|
|
||||||
applied.Env = nil
|
|
||||||
|
|
||||||
g.Assert(applied).Equal(tt.want)
|
g.Assert(applied).Equal(tt.want)
|
||||||
|
|
||||||
var found int = 0
|
|
||||||
|
|
||||||
for _, expectedEnvVar := range tt.expectedEnvVars {
|
|
||||||
for _, env := range appliedEnv {
|
|
||||||
if expectedEnvVar == env {
|
|
||||||
found += 1
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
g.Assert(found).Equal(len(tt.expectedEnvVars))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user