core: check sets for computed [GH-247]

This commit is contained in:
Mitchell Hashimoto 2014-08-30 17:25:34 -07:00
parent 3a6940d715
commit 52bc9a1055
5 changed files with 72 additions and 8 deletions

View File

@ -10,6 +10,9 @@ BUG FIXES:
* core: Configuration parses when identifier and '=' have no space. [GH-243] * core: Configuration parses when identifier and '=' have no space. [GH-243]
* core: `depends_on` with `count` generates the proper graph. [GH-244] * core: `depends_on` with `count` generates the proper graph. [GH-244]
* core: Depending on a computed variable of a list type generates a
plan without failure. i.e. `${type.name.foos.0.bar}` where `foos`
is computed. [GH-247]
## 0.2.0 (August 28, 2014) ## 0.2.0 (August 28, 2014)

View File

@ -367,16 +367,30 @@ func (c *Context) computeResourceVariable(
} }
attr, ok := r.Attributes[v.Field] attr, ok := r.Attributes[v.Field]
if !ok { if ok {
return attr, nil
}
// We didn't find the exact field, so lets separate the dots
// and see if anything along the way is a computed set. i.e. if
// we have "foo.0.bar" as the field, check to see if "foo" is
// a computed list. If so, then the whole thing is computed.
parts := strings.Split(v.Field, ".")
if len(parts) > 1 {
for i := 1; i < len(parts); i++ {
key := fmt.Sprintf("%s.#", strings.Join(parts[:i], "."))
if attr, ok := r.Attributes[key]; ok {
return attr, nil
}
}
}
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'",
id, id,
v.Field, v.Field,
v.FullKey()) v.FullKey())
}
return attr, nil
} }
func (c *Context) computeResourceMultiVariable( func (c *Context) computeResourceMultiVariable(

View File

@ -1418,6 +1418,29 @@ func TestContextPlan_computed(t *testing.T) {
} }
} }
func TestContextPlan_computedList(t *testing.T) {
c := testConfig(t, "plan-computed-list")
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext(t, &ContextOpts{
Config: c,
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(testTerraformPlanComputedListStr)
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
}
func TestContextPlan_count(t *testing.T) { func TestContextPlan_count(t *testing.T) {
c := testConfig(t, "plan-count") c := testConfig(t, "plan-count")
p := testProvider("aws") p := testProvider("aws")

View File

@ -302,6 +302,22 @@ STATE:
<no state> <no state>
` `
const testTerraformPlanComputedListStr = `
DIFF:
CREATE: aws_instance.bar
foo: "" => "<computed>"
type: "" => "aws_instance"
CREATE: aws_instance.foo
list.#: "" => "<computed>"
num: "" => "2"
type: "" => "aws_instance"
STATE:
<no state>
`
const testTerraformPlanCountStr = ` const testTerraformPlanCountStr = `
DIFF: DIFF:

View File

@ -0,0 +1,8 @@
resource "aws_instance" "foo" {
num = "2"
compute = "list.#"
}
resource "aws_instance" "bar" {
foo = "${aws_instance.foo.list.0.bar}"
}