terraform: test for single index access of counts

This commit is contained in:
Mitchell Hashimoto 2014-07-06 14:09:44 -07:00
parent cfb08b6c60
commit cc0c32d5ca
4 changed files with 72 additions and 4 deletions

View File

@ -260,7 +260,7 @@ func (c *Context) computeVars(raw *config.RawConfig) error {
case *config.ResourceVariable: case *config.ResourceVariable:
var attr string var attr string
var err error var err error
if v.Multi { if v.Multi && v.Index == -1 {
attr, err = c.computeResourceMultiVariable(v) attr, err = c.computeResourceMultiVariable(v)
} else { } else {
attr, err = c.computeResourceVariable(v) attr, err = c.computeResourceVariable(v)
@ -281,11 +281,16 @@ func (c *Context) computeVars(raw *config.RawConfig) error {
func (c *Context) computeResourceVariable( func (c *Context) computeResourceVariable(
v *config.ResourceVariable) (string, error) { v *config.ResourceVariable) (string, error) {
r, ok := c.state.Resources[v.ResourceId()] id := v.ResourceId()
if v.Multi {
id = fmt.Sprintf("%s.%d", id, v.Index)
}
r, ok := c.state.Resources[id]
if !ok { if !ok {
return "", fmt.Errorf( return "", fmt.Errorf(
"Resource '%s' not found for variable '%s'", "Resource '%s' not found for variable '%s'",
v.ResourceId(), id,
v.FullKey()) v.FullKey())
} }
@ -294,7 +299,7 @@ func (c *Context) computeResourceVariable(
return "", fmt.Errorf( return "", fmt.Errorf(
"Resource '%s' does not have attribute '%s' "+ "Resource '%s' does not have attribute '%s' "+
"for variable '%s'", "for variable '%s'",
v.ResourceId(), id,
v.Field, v.Field,
v.FullKey()) v.FullKey())
} }

View File

@ -564,6 +564,34 @@ func TestContextApply_outputMulti(t *testing.T) {
} }
} }
func TestContextApply_outputMultiIndex(t *testing.T) {
c := testConfig(t, "apply-output-multi-index")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext(t, &ContextOpts{
Config: c,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
if _, err := ctx.Plan(nil); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(testTerraformApplyOutputMultiIndexStr)
if actual != expected {
t.Fatalf("bad: \n%s", actual)
}
}
func TestContextApply_unknownAttribute(t *testing.T) { func TestContextApply_unknownAttribute(t *testing.T) {
c := testConfig(t, "apply-unknown") c := testConfig(t, "apply-unknown")
p := testProvider("aws") p := testProvider("aws")

View File

@ -144,6 +144,29 @@ Outputs:
foo_num = bar,bar,bar foo_num = bar,bar,bar
` `
const testTerraformApplyOutputMultiIndexStr = `
aws_instance.bar.0:
ID = foo
foo = bar
type = aws_instance
aws_instance.bar.1:
ID = foo
foo = bar
type = aws_instance
aws_instance.bar.2:
ID = foo
foo = bar
type = aws_instance
aws_instance.foo:
ID = foo
num = 2
type = aws_instance
Outputs:
foo_num = bar
`
const testTerraformApplyUnknownAttrStr = ` const testTerraformApplyUnknownAttrStr = `
aws_instance.foo: aws_instance.foo:
ID = foo ID = foo

View File

@ -0,0 +1,12 @@
resource "aws_instance" "foo" {
num = "2"
}
resource "aws_instance" "bar" {
foo = "bar"
count = 3
}
output "foo_num" {
value = "${aws_instance.bar.0.foo}"
}