Skip to content

Commit

Permalink
set Terraform variable from environment if available
Browse files Browse the repository at this point in the history
  • Loading branch information
lhitchon committed Sep 25, 2018
1 parent 4cd4543 commit 6e11738
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
21 changes: 17 additions & 4 deletions linter/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/hcl/hcl/parser"
"github.com/stelligent/config-lint/assertion"
"io/ioutil"
"os"
"path/filepath"
"regexp"
)
Expand Down Expand Up @@ -85,15 +86,27 @@ func loadVariables(data interface{}) []Variable {
list := data.([]interface{})
for _, entry := range list {
m := entry.(map[string]interface{})
for key, value := range m {
variables = append(variables, Variable{Name: key, Value: extractDefault(value)})
for key, resource := range m {
variables = append(variables, Variable{Name: key, Value: getVariableValue(key, resource)})
}
}
return variables
}

func extractDefault(value interface{}) interface{} {
list := value.([]interface{})
func getVariableValue(key string, resource interface{}) interface{} {
value := getVariableFromEnvironment(key)
if value != "" {
return value
}
return getVariableDefault(resource)
}

func getVariableFromEnvironment(key string) interface{} {
return os.Getenv("TF_VAR_" + key)
}

func getVariableDefault(resource interface{}) interface{} {
list := resource.([]interface{})
var defaultValue interface{}
for _, entry := range list {
m := entry.(map[string]interface{})
Expand Down
24 changes: 23 additions & 1 deletion linter/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package linter

import (
"github.com/stelligent/config-lint/assertion"
"os"
"testing"
)

Expand Down Expand Up @@ -34,7 +35,7 @@ func TestTerraformVariables(t *testing.T) {
}
resources, err := loader.PostLoad(loaded)
if err != nil {
t.Error("Expecting TestTerraformLinter.ReplaceVariables to not return an error")
t.Error("Expecting TestTerraformLinter.PostLoad to not return an error")
}
if len(resources) != 1 {
t.Errorf("Expecting to load 1 resources, not %d", len(loaded.Resources))
Expand All @@ -52,6 +53,27 @@ func TestTerraformVariables(t *testing.T) {
}
}

func TestTerraformVariablesFromEnvironment(t *testing.T) {
os.Setenv("TF_VAR_instance_type", "c4.large")
loader := TerraformResourceLoader{}
loaded, err := loader.Load("./testdata/resources/uses_variables.tf")
if err != nil {
t.Error("Expecting TestTerraformLinter.Load to not return an error")
}
resources, err := loader.PostLoad(loaded)
if err != nil {
t.Error("Expecting TestTerraformLinter.PostLoad to not return an error")
}
if len(resources) != 1 {
t.Errorf("Expecting to load 1 resources, not %d", len(loaded.Resources))
}
properties := resources[0].Properties.(map[string]interface{})
if properties["instance_type"] != "c4.large" {
t.Errorf("Unexpected value for variable: %s", properties["instance_type"])
}
os.Setenv("TF_VAR_instance_type", "")
}

func TestTerraformVariablesInDifferentFile(t *testing.T) {
options := Options{
Tags: []string{},
Expand Down

0 comments on commit 6e11738

Please sign in to comment.