terraform: test for single index access of counts
This commit is contained in:
parent
cfb08b6c60
commit
cc0c32d5ca
|
@ -260,7 +260,7 @@ func (c *Context) computeVars(raw *config.RawConfig) error {
|
|||
case *config.ResourceVariable:
|
||||
var attr string
|
||||
var err error
|
||||
if v.Multi {
|
||||
if v.Multi && v.Index == -1 {
|
||||
attr, err = c.computeResourceMultiVariable(v)
|
||||
} else {
|
||||
attr, err = c.computeResourceVariable(v)
|
||||
|
@ -281,11 +281,16 @@ func (c *Context) computeVars(raw *config.RawConfig) error {
|
|||
|
||||
func (c *Context) computeResourceVariable(
|
||||
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 {
|
||||
return "", fmt.Errorf(
|
||||
"Resource '%s' not found for variable '%s'",
|
||||
v.ResourceId(),
|
||||
id,
|
||||
v.FullKey())
|
||||
}
|
||||
|
||||
|
@ -294,7 +299,7 @@ func (c *Context) computeResourceVariable(
|
|||
return "", fmt.Errorf(
|
||||
"Resource '%s' does not have attribute '%s' "+
|
||||
"for variable '%s'",
|
||||
v.ResourceId(),
|
||||
id,
|
||||
v.Field,
|
||||
v.FullKey())
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
c := testConfig(t, "apply-unknown")
|
||||
p := testProvider("aws")
|
||||
|
|
|
@ -144,6 +144,29 @@ Outputs:
|
|||
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 = `
|
||||
aws_instance.foo:
|
||||
ID = foo
|
||||
|
|
|
@ -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}"
|
||||
}
|
Loading…
Reference in New Issue