diff --git a/terraform/eval_validate.go b/terraform/eval_validate.go index 221cdec30..88277fa40 100644 --- a/terraform/eval_validate.go +++ b/terraform/eval_validate.go @@ -1,5 +1,11 @@ package terraform +import ( + "fmt" + + "github.com/hashicorp/terraform/config" +) + // EvalValidateError is the error structure returned if there were // validation errors. type EvalValidateError struct { @@ -11,6 +17,49 @@ func (e *EvalValidateError) Error() string { return "" } +// EvalValidateCount is an EvalNode implementation that validates +// the count of a resource. +type EvalValidateCount struct { + Resource *config.Resource +} + +func (n *EvalValidateCount) Args() ([]EvalNode, []EvalType) { + return nil, nil +} + +// TODO: test +func (n *EvalValidateCount) Eval( + ctx EvalContext, args []interface{}) (interface{}, error) { + var count int + var errs []error + var err error + if _, err := ctx.Interpolate(n.Resource.RawCount, nil); err != nil { + errs = append(errs, fmt.Errorf( + "Failed to interpolate count: %s", err)) + goto RETURN + } + + count, err = n.Resource.Count() + if err != nil { + errs = append(errs) + goto RETURN + } + + if count < 0 { + errs = append(errs, fmt.Errorf( + "Count is less than zero: %d", count)) + } + +RETURN: + return nil, &EvalValidateError{ + Errors: errs, + } +} + +func (n *EvalValidateCount) Type() EvalType { + return EvalTypeNull +} + // EvalValidateProvider is an EvalNode implementation that validates // the configuration of a resource. type EvalValidateProvider struct { diff --git a/terraform/graph_config_node.go b/terraform/graph_config_node.go index da14549b3..536f9fb2f 100644 --- a/terraform/graph_config_node.go +++ b/terraform/graph_config_node.go @@ -136,6 +136,13 @@ func (n *GraphNodeConfigResource) DynamicExpand(ctx EvalContext) (*Graph, error) return b.Build(ctx.Path()) } +// GraphNodeEvalable impl. +func (n *GraphNodeConfigResource) EvalTree() EvalNode { + return &EvalValidateCount{ + Resource: n.Resource, + } +} + // GraphNodeProviderConsumer func (n *GraphNodeConfigResource) ProvidedBy() string { return resourceProvider(n.Resource.Type)