Merge pull request #611 from Banno/fix-multivar-in-modules

looking up count of multivars in correct module config
This commit is contained in:
Armon Dadgar 2014-11-30 12:06:02 -07:00
commit 4b8151ec6d
5 changed files with 64 additions and 2 deletions

View File

@ -1679,10 +1679,19 @@ func (c *walkContext) computeResourceMultiVariable(
c.Context.sl.RLock()
defer c.Context.sl.RUnlock()
childPath := c.Path[1:len(c.Path)]
var modTree *module.Tree
if len(childPath) == 0 {
modTree = c.Context.module
} else {
modTree = c.Context.module.Child(childPath)
}
// Get the resource from the configuration so we can know how
// many of the resource there is.
var cr *config.Resource
for _, r := range c.Context.module.Config().Resources {
for _, r := range modTree.Config().Resources {
if r.Id() == v.ResourceId() {
cr = r
break
@ -1697,7 +1706,7 @@ func (c *walkContext) computeResourceMultiVariable(
// Get the relevant module
// TODO: Not use only root module
module := c.Context.state.RootModule()
module := c.Context.state.ModuleByPath(c.Path)
count, err := cr.Count()
if err != nil {

View File

@ -2978,6 +2978,28 @@ func TestContextPlan_moduleInputFromVar(t *testing.T) {
t.Fatalf("bad:\n%s", actual)
}
}
func TestContextPlan_moduleMultiVar(t *testing.T) {
m := testModule(t, "plan-module-multi-var")
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
plan, err := ctx.Plan(nil)
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanModuleMultiVarStr)
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
}
func TestContextPlan_moduleOrphans(t *testing.T) {
m := testModule(t, "plan-modules-remove")
p := testProvider("aws")

View File

@ -841,6 +841,25 @@ STATE:
<no state>
`
const testTerraformPlanModuleMultiVarStr = `
DIFF:
module.child:
CREATE: aws_instance.bar.0
baz: "" => "baz"
type: "" => "aws_instance"
CREATE: aws_instance.bar.1
baz: "" => "baz"
type: "" => "aws_instance"
CREATE: aws_instance.foo
foo: "" => "baz,baz"
type: "" => "aws_instance"
STATE:
<no state>
`
const testTerraformPlanModuleOrphansStr = `
DIFF:

View File

@ -0,0 +1,8 @@
resource "aws_instance" "bar" {
baz = "baz"
count = 2
}
resource "aws_instance" "foo" {
foo = "${join(",",aws_instance.bar.*.baz)}"
}

View File

@ -0,0 +1,4 @@
module "child" {
source = "./child"
}