Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Feature) Support terraform.workspace variable #181

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 80 additions & 70 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ func TestCLIRun(t *testing.T) {
}
defaultCLIOptions := TestCLIOptions{
Config: &config.Config{
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
},
ConfigFile: ".tflint.hcl",
}
Expand Down Expand Up @@ -211,13 +212,14 @@ func TestCLIRun(t *testing.T) {
Status: ExitCodeOK,
CLIOptions: TestCLIOptions{
Config: &config.Config{
Debug: true,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
Debug: true,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
},
ConfigFile: ".tflint.hcl",
},
Expand Down Expand Up @@ -259,13 +261,14 @@ func TestCLIRun(t *testing.T) {
Status: ExitCodeOK,
CLIOptions: TestCLIOptions{
Config: &config.Config{
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{"rule1": true, "rule2": true},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{"rule1": true, "rule2": true},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
},
ConfigFile: ".tflint.hcl",
},
Expand All @@ -281,13 +284,14 @@ func TestCLIRun(t *testing.T) {
Status: ExitCodeOK,
CLIOptions: TestCLIOptions{
Config: &config.Config{
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{"module1": true, "module2": true},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{"module1": true, "module2": true},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
},
ConfigFile: ".tflint.hcl",
},
Expand All @@ -309,13 +313,14 @@ func TestCLIRun(t *testing.T) {
Status: ExitCodeOK,
CLIOptions: TestCLIOptions{
Config: &config.Config{
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars", "example1.tfvars", "example2.tfvars"},
TerraformEnv: "default",
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars", "example1.tfvars", "example2.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
},
ConfigFile: ".tflint.hcl",
},
Expand All @@ -331,13 +336,14 @@ func TestCLIRun(t *testing.T) {
Status: ExitCodeOK,
CLIOptions: TestCLIOptions{
Config: &config.Config{
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
},
ConfigFile: ".tflint.example.hcl",
},
Expand All @@ -353,13 +359,14 @@ func TestCLIRun(t *testing.T) {
Status: ExitCodeOK,
CLIOptions: TestCLIOptions{
Config: &config.Config{
Debug: false,
DeepCheck: true,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
Debug: false,
DeepCheck: true,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
},
ConfigFile: ".tflint.hcl",
},
Expand All @@ -375,17 +382,18 @@ func TestCLIRun(t *testing.T) {
Status: ExitCodeOK,
CLIOptions: TestCLIOptions{
Config: &config.Config{
Debug: false,
DeepCheck: true,
AwsCredentials: map[string]string{
Debug: false,
DeepCheck: true,
AwsCredentials: map[string]string{
"access_key": "AWS_ACCESS_KEY_ID",
"secret_key": "AWS_SECRET_ACCESS_KEY",
"region": "us-east-1",
},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
},
ConfigFile: ".tflint.hcl",
},
Expand All @@ -401,16 +409,17 @@ func TestCLIRun(t *testing.T) {
Status: ExitCodeOK,
CLIOptions: TestCLIOptions{
Config: &config.Config{
Debug: false,
DeepCheck: true,
AwsCredentials: map[string]string{
Debug: false,
DeepCheck: true,
AwsCredentials: map[string]string{
"profile": "account1",
"region": "us-east-1",
},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
},
ConfigFile: ".tflint.hcl",
},
Expand Down Expand Up @@ -471,13 +480,14 @@ func TestCLIRun(t *testing.T) {
Status: ExitCodeOK,
CLIOptions: TestCLIOptions{
Config: &config.Config{
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{"aws_instance_invalid_ami": true},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{"aws_instance_invalid_ami": true},
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
},
ConfigFile: ".tflint.hcl",
},
Expand Down
33 changes: 18 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,34 @@ import (
)

type Config struct {
Debug bool
DeepCheck bool `hcl:"deep_check"`
AwsCredentials map[string]string `hcl:"aws_credentials"`
IgnoreModule map[string]bool `hcl:"ignore_module"`
IgnoreRule map[string]bool `hcl:"ignore_rule"`
Varfile []string `hcl:"varfile"`
TerraformVersion string `hcl:"terraform_version"`
TerraformEnv string
Debug bool
DeepCheck bool `hcl:"deep_check"`
AwsCredentials map[string]string `hcl:"aws_credentials"`
IgnoreModule map[string]bool `hcl:"ignore_module"`
IgnoreRule map[string]bool `hcl:"ignore_rule"`
Varfile []string `hcl:"varfile"`
TerraformVersion string `hcl:"terraform_version"`
TerraformEnv string
TerraformWorkspace string
}

func Init() *Config {
return &Config{
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{},
TerraformEnv: "default",
Debug: false,
DeepCheck: false,
AwsCredentials: map[string]string{},
IgnoreModule: map[string]bool{},
IgnoreRule: map[string]bool{},
Varfile: []string{},
TerraformEnv: "default",
TerraformWorkspace: "default",
}
}

func (c *Config) LoadConfig(filename string) error {
if b, err := ioutil.ReadFile(".terraform/environment"); err == nil {
c.TerraformEnv = string(b)
c.TerraformWorkspace = string(b)
}

if _, err := os.Stat(filename); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ func TestLoadConfig(t *testing.T) {
IgnoreModule: map[string]bool{
"github.com/wata727/example-module": true,
},
Varfile: []string{"example1.tfvars", "example2.tfvars"},
TerraformVersion: "0.9.11",
TerraformEnv: "dev",
Varfile: []string{"example1.tfvars", "example2.tfvars"},
TerraformVersion: "0.9.11",
TerraformEnv: "dev",
TerraformWorkspace: "dev",
},
},
{
Expand Down
4 changes: 4 additions & 0 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func NewEvaluator(templates map[string]*hclast.File, schema []*schema.Template,
Type: hilast.TypeString,
Value: c.TerraformEnv,
}
varMap["terraform.workspace"] = hilast.Variable{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep sure, I'll add that in soon 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready for review 👍

Type: hilast.TypeString,
Value: c.TerraformWorkspace,
}

evaluator := &Evaluator{
Config: hil.EvalConfig{
Expand Down
30 changes: 30 additions & 0 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,33 @@ func TestEvalTerraformEnv(t *testing.T) {
}
}
}

func TestEvalTerraformWorkspace(t *testing.T) {
cases := []struct {
Name string
Input string
Src string
Result string
}{
{
Name: "terraform workspace",
Input: "dev",
Src: "${terraform.workspace}",
Result: "dev",
},
}

for _, tc := range cases {
c := config.Init()
c.TerraformWorkspace = tc.Input

evaluator, err := NewEvaluator(map[string]*ast.File{}, []*schema.Template{}, []*ast.File{}, c)
if err != nil {
t.Fatalf("\nError: %s\n\ntestcase: %s", err, tc.Name)
}
result, _ := evaluator.Eval(tc.Src)
if result != tc.Result {
t.Fatalf("\nBad: %s\nExpected: %s\n\ntestcase: %s", result, tc.Result, tc.Name)
}
}
}