1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/terraformExecute.go
Christian Volk 7517873b62
feat(terraformExecute): fetch CLI config from vault or jenkins (#3225)
* feat(terraformExecute): run init prior to command

* Docs & fmt

* cleanup

* feat(terraformExecute): fetch cli config from vault or jenkins

* fix

* cleanup

* cleanup

* Update resources/metadata/terraformExecute.yaml

* rename config param from terraformConfigFile to cliConfigFile

* rename credential params

* cleanup

* fix

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2021-10-29 13:58:34 +02:00

87 lines
2.1 KiB
Go

package cmd
import (
"fmt"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/telemetry"
)
type terraformExecuteUtils interface {
command.ExecRunner
FileExists(filename string) (bool, error)
}
type terraformExecuteUtilsBundle struct {
*command.Command
*piperutils.Files
}
func newTerraformExecuteUtils() terraformExecuteUtils {
utils := terraformExecuteUtilsBundle{
Command: &command.Command{},
Files: &piperutils.Files{},
}
// Reroute command output to logging framework
utils.Stdout(log.Writer())
utils.Stderr(log.Writer())
return &utils
}
func terraformExecute(config terraformExecuteOptions, telemetryData *telemetry.CustomData) {
utils := newTerraformExecuteUtils()
err := runTerraformExecute(&config, telemetryData, utils)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func runTerraformExecute(config *terraformExecuteOptions, telemetryData *telemetry.CustomData, utils terraformExecuteUtils) error {
if len(config.CliConfigFile) > 0 {
utils.AppendEnv([]string{fmt.Sprintf("TF_CLI_CONFIG_FILE=%s", config.CliConfigFile)})
}
args := []string{}
if config.Command == "apply" {
args = append(args, "-auto-approve")
}
if (config.Command == "apply" || config.Command == "plan") && config.TerraformSecrets != "" {
args = append(args, fmt.Sprintf("-var-file=%s", config.TerraformSecrets))
}
if config.AdditionalArgs != nil {
args = append(args, config.AdditionalArgs...)
}
if config.Init {
err := runTerraform(utils, "init", []string{}, config.GlobalOptions)
if err != nil {
return err
}
}
return runTerraform(utils, config.Command, args, config.GlobalOptions)
}
func runTerraform(utils terraformExecuteUtils, command string, args []string, globalOptions []string) error {
cliArgs := []string{}
if globalOptions != nil {
cliArgs = append(cliArgs, globalOptions...)
}
cliArgs = append(cliArgs, command)
if args != nil {
cliArgs = append(cliArgs, args...)
}
return utils.RunExecutable("terraform", cliArgs...)
}