terraform: EvalValidateCount

This commit is contained in:
Mitchell Hashimoto 2015-02-08 17:20:46 -08:00
parent cd609172e1
commit 584eac9e92
2 changed files with 56 additions and 0 deletions

View File

@ -1,5 +1,11 @@
package terraform package terraform
import (
"fmt"
"github.com/hashicorp/terraform/config"
)
// EvalValidateError is the error structure returned if there were // EvalValidateError is the error structure returned if there were
// validation errors. // validation errors.
type EvalValidateError struct { type EvalValidateError struct {
@ -11,6 +17,49 @@ func (e *EvalValidateError) Error() string {
return "" 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 // EvalValidateProvider is an EvalNode implementation that validates
// the configuration of a resource. // the configuration of a resource.
type EvalValidateProvider struct { type EvalValidateProvider struct {

View File

@ -136,6 +136,13 @@ func (n *GraphNodeConfigResource) DynamicExpand(ctx EvalContext) (*Graph, error)
return b.Build(ctx.Path()) return b.Build(ctx.Path())
} }
// GraphNodeEvalable impl.
func (n *GraphNodeConfigResource) EvalTree() EvalNode {
return &EvalValidateCount{
Resource: n.Resource,
}
}
// GraphNodeProviderConsumer // GraphNodeProviderConsumer
func (n *GraphNodeConfigResource) ProvidedBy() string { func (n *GraphNodeConfigResource) ProvidedBy() string {
return resourceProvider(n.Resource.Type) return resourceProvider(n.Resource.Type)