Skip to content

Commit

Permalink
Fix count interpolation with computed values
Browse files Browse the repository at this point in the history
This commit enables the use computed values when interpolating
the count special variable.

This is achieved by defering the expansion of `count` during
walkInput and walkValidate as the real value isn't necessary
at this point.

The value will be resolved during walkRefresh and DynamicExpand
will work as expected.

Partially fixes hashicorp#3888.
  • Loading branch information
greenboxal committed Nov 29, 2016
1 parent a888601 commit bbff7b0
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 11 deletions.
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ type Resource struct {
Provider string
DependsOn []string
Lifecycle ResourceLifecycle

DeferCountComputation bool
}

// Copy returns a copy of this Resource. Helpful for avoiding shared
Expand Down Expand Up @@ -213,6 +215,10 @@ func (r *Module) Id() string {

// Count returns the count of this resource.
func (r *Resource) Count() (int, error) {
if r.DeferCountComputation {
return 1, nil
}

v, err := strconv.ParseInt(r.RawCount.Value().(string), 0, 0)
if err != nil {
return 0, err
Expand Down
7 changes: 7 additions & 0 deletions terraform/eval_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ type EvalContext interface {
// State returns the global state as well as the lock that should
// be used to modify that state.
State() (*State, *sync.RWMutex)

// True if we can ignore missing computed values when interpolating
// the count variable
//
// This is useful as we don't have the full tree during walkInput
// or walkValidate
CanIgnoreMissingCountExpansion() bool
}
5 changes: 5 additions & 0 deletions terraform/eval_context_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ type BuiltinEvalContext struct {
DiffLock *sync.RWMutex
StateValue *State
StateLock *sync.RWMutex
IgnoreMissingCount bool

once sync.Once
}

func (ctx *BuiltinEvalContext) CanIgnoreMissingCountExpansion() bool {
return ctx.IgnoreMissingCount
}

func (ctx *BuiltinEvalContext) Hook(fn func(Hook) (HookAction, error)) error {
for _, h := range ctx.Hooks {
action, err := fn(h)
Expand Down
4 changes: 4 additions & 0 deletions terraform/eval_context_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,7 @@ func (c *MockEvalContext) State() (*State, *sync.RWMutex) {
c.StateCalled = true
return c.StateState, c.StateLock
}

func (c *MockEvalContext) CanIgnoreMissingCountExpansion() bool {
return false
}
25 changes: 16 additions & 9 deletions terraform/eval_count_computed.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ import (
"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 {
// EvalCountFixComputed is an EvalNode that checks if a resource count
// is computed and try to fix the value.
// It can only be fixed if the walk operation is either walkInput or walkValidate
// Otherwise it errors
type EvalCountFixComputed 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())
func (n *EvalCountFixComputed) Eval(ctx EvalContext) (interface{}, error) {
if ctx.CanIgnoreMissingCountExpansion() {
n.Resource.DeferCountComputation = true
} else {
n.Resource.DeferCountComputation = false

if n.Resource.RawCount.Value() == unknownValue() {
return nil, fmt.Errorf(
"%s: value of 'count' cannot be computed",
n.Resource.Id())
}
}

return nil, nil
Expand Down
2 changes: 1 addition & 1 deletion terraform/graph_config_node_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (n *GraphNodeConfigResource) EvalTree() EvalNode {
return &EvalSequence{
Nodes: []EvalNode{
&EvalInterpolate{Config: n.Resource.RawCount},
&EvalCountCheckComputed{Resource: n.Resource},
&EvalCountFixComputed{Resource: n.Resource},
&EvalOpFilter{
Ops: []walkOperation{walkValidate},
Node: &EvalValidateCount{Resource: n.Resource},
Expand Down
6 changes: 6 additions & 0 deletions terraform/graph_walk_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func (w *ContextGraphWalker) EnterPath(path []string) EvalContext {
w.interpolaterVars[key] = variables
w.interpolaterVarLock.Unlock()

ignoreMissingCount := false
if w.Operation == walkInput || w.Operation == walkValidate {
ignoreMissingCount = true
}

ctx := &BuiltinEvalContext{
PathValue: path,
Hooks: w.Context.hooks,
Expand All @@ -89,6 +94,7 @@ func (w *ContextGraphWalker) EnterPath(path []string) EvalContext {
},
InterpolaterVars: w.interpolaterVars,
InterpolaterVarLock: &w.interpolaterVarLock,
IgnoreMissingCount: ignoreMissingCount,
}

w.contexts[key] = ctx
Expand Down
2 changes: 1 addition & 1 deletion terraform/node_resource_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (n *NodePlannableResource) EvalTree() EvalNode {
// into the proper number of instances.
&EvalInterpolate{Config: n.Config.RawCount},

&EvalCountCheckComputed{Resource: n.Config},
&EvalCountFixComputed{Resource: n.Config},
&EvalCountFixZeroOneBoundary{Resource: n.Config},
},
}
Expand Down

0 comments on commit bbff7b0

Please sign in to comment.