Skip to content

Commit

Permalink
Merge pull request #42 from severeone/feature/addBackendConfigParam
Browse files Browse the repository at this point in the history
Adding support of -backend-config Terraform parameter.
  • Loading branch information
im2nguyen authored Oct 2, 2021
2 parents 1e04495 + 24e6ed5 commit ba50369
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ Then, add it as environment variables to your Docker container with `--env-file`
$ docker run --rm -it -p 9000:9000 -v $(pwd):/src --env-file ./.env im2nguyen/rover
```

## Define tfvars and Terraform variables
## Define tfbackend, tfvars and Terraform variables

Use `-tfVarsFile` or `-tfVar` to define variables. For example, you can run the following in the `example/random-test` directory to overload variables.
Use `-tfBackendConfig` to define backend config files and `-tfVarsFile` or `-tfVar` to define variables. For example, you can run the following in the `example/random-test` directory to overload variables.

```
$ docker run --rm -it -p 9000:9000 -v $(pwd):/src im2nguyen/rover -tfVarsFile test.tfvars -tfVar max_length=4
$ docker run --rm -it -p 9000:9000 -v $(pwd):/src im2nguyen/rover -tfBackendConfig test.tfbackend -tfVarsFile test.tfvars -tfVar max_length=4
```

## Installation
Expand Down
69 changes: 43 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,27 @@ func (i *arrayFlags) Set(value string) error {
}

type rover struct {
Name string
WorkingDir string
TfPath string
TfVarsFiles []string
TfVars []string
PlanPath string
WorkspaceName string
TFConfigExists bool
ShowSensitive bool
Config *tfconfig.Module
Plan *tfjson.Plan
RSO *ResourcesOverview
Map *Map
Graph Graph
Name string
WorkingDir string
TfPath string
TfVarsFiles []string
TfVars []string
TfBackendConfigs []string
PlanPath string
WorkspaceName string
TFConfigExists bool
ShowSensitive bool
Config *tfconfig.Module
Plan *tfjson.Plan
RSO *ResourcesOverview
Map *Map
Graph Graph
}

func main() {
var tfPath, workingDir, name, zipFileName, ipPort, planPath, workspaceName string
var standalone, tfConfigExists, showSensitive, getVersion bool
var tfVarsFiles, tfVars arrayFlags
var tfVarsFiles, tfVars, tfBackendConfigs arrayFlags
flag.StringVar(&tfPath, "tfPath", "/usr/local/bin/terraform", "Path to Terraform binary")
flag.StringVar(&workingDir, "workingDir", ".", "Path to Terraform configuration")
flag.StringVar(&name, "name", "rover", "Configuration name")
Expand All @@ -75,6 +76,7 @@ func main() {
flag.BoolVar(&getVersion, "version", false, "Get current version")
flag.Var(&tfVarsFiles, "tfVarsFile", "Path to *.tfvars files")
flag.Var(&tfVars, "tfVar", "Terraform variable (key=value)")
flag.Var(&tfBackendConfigs, "tfBackendConfig", "Path to *.tfbackend files")
flag.Parse()

if getVersion {
Expand All @@ -86,6 +88,7 @@ func main() {

parsedTfVarsFiles := strings.Split(tfVarsFiles.String(), ",")
parsedTfVars := strings.Split(tfVars.String(), ",")
parsedTfBackendConfigs := strings.Split(tfBackendConfigs.String(), ",")

if planPath != "" {
path, err := os.Getwd()
Expand All @@ -99,15 +102,16 @@ func main() {
}

r := rover{
Name: name,
WorkingDir: workingDir,
TfPath: tfPath,
PlanPath: planPath,
TFConfigExists: tfConfigExists,
ShowSensitive: showSensitive,
TfVarsFiles: parsedTfVarsFiles,
TfVars: parsedTfVars,
WorkspaceName: workspaceName,
Name: name,
WorkingDir: workingDir,
TfPath: tfPath,
PlanPath: planPath,
TFConfigExists: tfConfigExists,
ShowSensitive: showSensitive,
TfVarsFiles: parsedTfVarsFiles,
TfVars: parsedTfVars,
TfBackendConfigs: parsedTfBackendConfigs,
WorkspaceName: workspaceName,
}

// Generate assets
Expand Down Expand Up @@ -207,8 +211,21 @@ func (r *rover) getPlan() error {
}

log.Println("Initializing Terraform...")
// err = tf.Init(context.Background(), tfexec.Upgrade(true), tfexec.LockTimeout("60s"))
err = tf.Init(context.Background(), tfexec.Upgrade(true))

// Create TF Init options
var tfInitOptions []tfexec.InitOption
tfInitOptions = append(tfInitOptions, tfexec.Upgrade(true))

// Add *.tfbackend files
for _, tfBackendConfig := range r.TfBackendConfigs {
if tfBackendConfig != "" {
tfInitOptions = append(tfInitOptions, tfexec.BackendConfig(tfBackendConfig))
}
}

// tfInitOptions = append(tfInitOptions, tfexec.LockTimeout("60s"))

err = tf.Init(context.Background(), tfInitOptions...)
if err != nil {
return errors.New(fmt.Sprintf("Unable to initialize Terraform Plan: %s", err))
}
Expand Down

0 comments on commit ba50369

Please sign in to comment.