invlaid use of dynamic with attrs should not panic
Mistakenly using dynamic on an attribute will lead to a panic when attempting to resolve variable references with a partial body, because the dynamic blocks have yet to be expanded and validated. Check that the block element type is actually an object before generating a schema.
This commit is contained in:
parent
4c337cc51d
commit
6e222375c7
|
@ -33,7 +33,7 @@ func walkVariables(node dynblock.WalkVariablesNode, body hcl.Body, schema *confi
|
||||||
for _, child := range children {
|
for _, child := range children {
|
||||||
if blockS, exists := schema.BlockTypes[child.BlockTypeName]; exists {
|
if blockS, exists := schema.BlockTypes[child.BlockTypeName]; exists {
|
||||||
vars = append(vars, walkVariables(child.Node, child.Body(), &blockS.Block)...)
|
vars = append(vars, walkVariables(child.Node, child.Body(), &blockS.Block)...)
|
||||||
} else if attrS, exists := schema.Attributes[child.BlockTypeName]; exists {
|
} else if attrS, exists := schema.Attributes[child.BlockTypeName]; exists && attrS.Type.ElementType().IsObjectType() {
|
||||||
synthSchema := SchemaForCtyElementType(attrS.Type.ElementType())
|
synthSchema := SchemaForCtyElementType(attrS.Type.ElementType())
|
||||||
vars = append(vars, walkVariables(child.Node, child.Body(), synthSchema)...)
|
vars = append(vars, walkVariables(child.Node, child.Body(), synthSchema)...)
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,10 @@ func TestExpandedVariables(t *testing.T) {
|
||||||
})),
|
})),
|
||||||
Optional: true,
|
Optional: true,
|
||||||
},
|
},
|
||||||
|
"bar": {
|
||||||
|
Type: cty.Map(cty.String),
|
||||||
|
Optional: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,6 +147,29 @@ dynamic "foo" {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"misplaced dynamic block": {
|
||||||
|
src: `
|
||||||
|
dynamic "bar" {
|
||||||
|
for_each = beep
|
||||||
|
content {
|
||||||
|
key = val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
schema: fooSchema,
|
||||||
|
want: []hcl.Traversal{
|
||||||
|
{
|
||||||
|
hcl.TraverseRoot{
|
||||||
|
Name: "beep",
|
||||||
|
SrcRange: hcl.Range{
|
||||||
|
Filename: "test.tf",
|
||||||
|
Start: hcl.Pos{Line: 3, Column: 14, Byte: 30},
|
||||||
|
End: hcl.Pos{Line: 3, Column: 18, Byte: 34},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, test := range tests {
|
for name, test := range tests {
|
||||||
|
|
Loading…
Reference in New Issue