Merge pull request #10060 from hashicorp/b-computed-count

terraform: detect compute counts and show a nicer error
This commit is contained in:
Mitchell Hashimoto 2016-11-11 11:12:12 -08:00 committed by GitHub
commit ab71467824
6 changed files with 60 additions and 0 deletions

View File

@ -1309,6 +1309,26 @@ func TestContext2Plan_countComputed(t *testing.T) {
}
}
func TestContext2Plan_countComputedModule(t *testing.T) {
m := testModule(t, "plan-count-computed-module")
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
_, err := ctx.Plan()
expectedErr := "aws_instance.bar: value of 'count'"
if !strings.Contains(fmt.Sprintf("%s", err), expectedErr) {
t.Fatalf("expected err would contain %q\nerr: %s\n",
expectedErr, err)
}
}
func TestContext2Plan_countIndex(t *testing.T) {
m := testModule(t, "plan-count-index")
p := testProvider("aws")

View File

@ -0,0 +1,25 @@
package terraform
import (
"fmt"
"github.com/hashicorp/terraform/config"
)
// EvalCountCheckComputed is an EvalNode that checks if a resource count
// is computed and errors if so. This can possibly happen across a
// module boundary and we don't yet support this.
type EvalCountCheckComputed struct {
Resource *config.Resource
}
// TODO: test
func (n *EvalCountCheckComputed) Eval(ctx EvalContext) (interface{}, error) {
if n.Resource.RawCount.Value() == unknownValue() {
return nil, fmt.Errorf(
"%s: value of 'count' cannot be computed",
n.Resource.Id())
}
return nil, nil
}

View File

@ -215,6 +215,7 @@ func (n *GraphNodeConfigResource) EvalTree() EvalNode {
return &EvalSequence{
Nodes: []EvalNode{
&EvalInterpolate{Config: n.Resource.RawCount},
&EvalCountCheckComputed{Resource: n.Resource},
&EvalOpFilter{
Ops: []walkOperation{walkValidate},
Node: &EvalValidateCount{Resource: n.Resource},

View File

@ -31,6 +31,7 @@ func (n *NodePlannableResource) EvalTree() EvalNode {
// into the proper number of instances.
&EvalInterpolate{Config: n.Config.RawCount},
&EvalCountCheckComputed{Resource: n.Config},
&EvalCountFixZeroOneBoundary{Resource: n.Config},
},
}

View File

@ -0,0 +1,5 @@
variable "value" {}
resource "aws_instance" "bar" {
count = "${var.value}"
}

View File

@ -0,0 +1,8 @@
resource "aws_instance" "foo" {
compute = "foo"
}
module "child" {
source = "./child"
value = "${aws_instance.foo.foo}"
}