Skip to content

Commit

Permalink
Don't pass variables to the modules that don't declare them (#40)
Browse files Browse the repository at this point in the history
Don't pass variable to a module if it doesn't require it
  • Loading branch information
btromanova authored Sep 19, 2019
1 parent 94386df commit be238b2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
30 changes: 30 additions & 0 deletions astro/astro_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,33 @@ func TestApplyFailModule(t *testing.T) {
assert.NoError(t, results[id])
}
}

// Tests that variables are passed to the modules that declare them and not
// passed to the modules that didn't
func TestPassVariables(t *testing.T) {
t.Parallel()

c, err := NewProjectFromConfigFile("fixtures/test-pass-variables/astro.yaml")
require.NoError(t, err)

c.config.TerraformDefaults.Path = absolutePath("fixtures/mock-terraform/success")

_, resultChan, err := c.Plan(PlanExecutionParameters{
ExecutionParameters: ExecutionParameters{
UserVars: &UserVariables{
Values: map[string]string{
"region": "east1",
},
},
},
})
require.NoError(t, err)

results := testReadResults(resultChan)

// Mocked terraform prints parameters it was called with to stderr.
// Check that variables were passed to the module that declared it and not
// passed to the one that didn't.
assert.Contains(t, results["bar-east1"].TerraformResult().Stderr(), "-var region=east1")
assert.NotContains(t, results["foo"].TerraformResult().Stderr(), "-var")
}
11 changes: 9 additions & 2 deletions astro/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,17 @@ type unboundExecution struct {
// boundExecution with variable values replaced. An error is returned if
// not all required user values were provided.
func (e *unboundExecution) bind(userVars map[string]string) (*boundExecution, error) {
boundVars := union(e.Variables(), userVars)
// boundVars is the map of execution variables bound to the values provided by user
boundVars := make(map[string]string)

missingVars := []string{}
for key, val := range e.Variables() {
boundVars[key] = val
if userVal, ok := userVars[key]; ok {
boundVars[key] = userVal
}
}

missingVars := []string{}
// Check that the user provided variables replace everything that
// needs to be replaced.
for _, val := range boundVars {
Expand Down
22 changes: 22 additions & 0 deletions astro/fixtures/test-pass-variables/astro.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---

terraform:
path: ../mock-terraform/success

modules:

- name: foo
path: .
remote:
backend: local
backend_config:
key: "/tmp/terraform-test/foo.tfstate"

- name: bar
path: .
remote:
backend: local
backend_config:
key: "/tmp/terraform-test/bar.tfstate"
variables:
- name: region
13 changes: 0 additions & 13 deletions astro/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,3 @@ func filterMaps(a, b map[string]string) bool {
}
return true
}

// union takes a series of maps and returns a new map which is a union of all
// of them.
func union(maps ...map[string]string) (result map[string]string) {
result = make(map[string]string)
for _, m := range maps {
for k, v := range m {
result[k] = v
}

}
return result
}

0 comments on commit be238b2

Please sign in to comment.