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

Adding suport for -var-file and -target argument #201

Merged
merged 18 commits into from Feb 17, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/terraform-basic-example/main.tf
Expand Up @@ -5,4 +5,8 @@

data "template_file" "example" {
template = "${var.example}"
}
}

data "template_file" "example2" {
template = "${var.example2}"
}
6 changes: 5 additions & 1 deletion examples/terraform-basic-example/outputs.tf
@@ -1,3 +1,7 @@
output "example" {
value = "${data.template_file.example.rendered}"
}
}

output "example2" {
value = "${data.template_file.example2.rendered}"
}
1 change: 1 addition & 0 deletions examples/terraform-basic-example/varfile.tfvars
@@ -0,0 +1 @@
example2 = "test"
7 changes: 6 additions & 1 deletion examples/terraform-basic-example/variables.tf
Expand Up @@ -20,4 +20,9 @@
variable "example" {
description = "Example variable"
default = "example"
}
}

variable "example2" {
description = "Example variable 2"
default = ""
}
8 changes: 8 additions & 0 deletions modules/terraform/cmd.go
Expand Up @@ -17,6 +17,14 @@ func GetCommonOptions(options *Options, args ...string) (*Options, []string) {
args = append(args, "-no-color")
}

for _, varFile = range options.VarFiles {
aclowkey marked this conversation as resolved.
Show resolved Hide resolved
arg = append(args, fmt.Sprintf("%s=%s","-var-file", varFile))
}

for _, target = range options.Targets {
arg = append(args, fmt.Sprintf("%s=%s","-target", target))
}

// if SshAgent is provided, override the local SSH agent with the socket of our in-process agent
if options.SshAgent != nil {
// Initialize EnvVars, if it hasn't been set yet
Expand Down
2 changes: 2 additions & 0 deletions modules/terraform/options.go
Expand Up @@ -10,6 +10,8 @@ import (
type Options struct {
TerraformDir string // The path to the folder where the Terraform code is defined.
Vars map[string]interface{} // The vars to pass to Terraform commands using the -var option.
VarFiles []string // The var file paths to pass to Terraform commands using -var-file option.
Targets []string // The target resources to pass to the terraform command with -target
EnvVars map[string]string // Environment variables to set when running Terraform
BackendConfig map[string]interface{} // The vars to pass to the terraform init command for extra configuration for the backend
RetryableTerraformErrors map[string]string // If Terraform apply fails with one of these (transient) errors, retry. The keys are text to look for in the error and the message is what to display to a user if that error is found.
Expand Down
11 changes: 11 additions & 0 deletions test/terraform_basic_example_test.go
Expand Up @@ -13,6 +13,7 @@ func TestTerraformBasicExample(t *testing.T) {

expectedText := "foo"

// TODO create the terraform varfile from test
aclowkey marked this conversation as resolved.
Show resolved Hide resolved
terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../examples/terraform-basic-example",
Expand All @@ -21,9 +22,13 @@ func TestTerraformBasicExample(t *testing.T) {
Vars: map[string]interface{}{
"example": expectedText,
},
VarFiles: []string{
"varfile.tfvars"
}

NoColor: true,
}


// At the end of the test, run `terraform destroy` to clean up any resources that were created
defer terraform.Destroy(t, terraformOptions)
Expand All @@ -36,4 +41,10 @@ func TestTerraformBasicExample(t *testing.T) {

// Verify we're getting back the variable we expect
assert.Equal(t, expectedText, actualText)

// Test for the second variable which comes from the var file
actualText = terraform.Output(t, terraformOptions, "example")
assert.Equal(t, "example2", actualText)

}