terraform: test count = 1 variable access

This commit is contained in:
Mitchell Hashimoto 2014-10-02 17:24:22 -07:00
parent 221e40a3a9
commit 66c58788fe
4 changed files with 55 additions and 4 deletions

View File

@ -1497,12 +1497,17 @@ func (c *walkContext) computeResourceVariable(
module := c.Context.state.ModuleByPath(c.Path)
r, ok := module.Resources[id]
if !ok {
if v.Multi && v.Index == 0 {
r, ok = module.Resources[v.ResourceId()]
}
if !ok {
return "", fmt.Errorf(
"Resource '%s' not found for variable '%s'",
id,
v.FullKey())
}
}
if r.Primary == nil {
goto MISSING

View File

@ -2513,6 +2513,29 @@ func TestContextPlan_countZero(t *testing.T) {
}
}
func TestContextPlan_countOneIndex(t *testing.T) {
m := testModule(t, "plan-count-one-index")
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(testTerraformPlanCountOneIndexStr)
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
}
func TestContextPlan_countDecreaseToOne(t *testing.T) {
m := testModule(t, "plan-count-dec")
p := testProvider("aws")

View File

@ -477,6 +477,21 @@ STATE:
<no state>
`
const testTerraformPlanCountOneIndexStr = `
DIFF:
CREATE: aws_instance.bar
foo: "" => "foo"
type: "" => "aws_instance"
CREATE: aws_instance.foo
foo: "" => "foo"
type: "" => "aws_instance"
STATE:
<no state>
`
const testTerraformPlanCountZeroStr = `
DIFF:

View File

@ -0,0 +1,8 @@
resource "aws_instance" "foo" {
count = 1
foo = "foo"
}
resource "aws_instance" "bar" {
foo = "${aws_instance.foo.0.foo}"
}