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 Feb 16, 2017
1 parent 555c8bb commit 62d338c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 10 deletions.
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,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 @@ -212,6 +214,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
}

raw := r.RawCount.Value()
count, ok := r.RawCount.Value().(string)
if !ok {
Expand Down
7 changes: 7 additions & 0 deletions terraform/eval_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,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 @@ -43,6 +43,7 @@ type BuiltinEvalContext struct {
DiffLock *sync.RWMutex
StateValue *State
StateLock *sync.RWMutex
IgnoreMissingCount bool

once sync.Once
}
Expand All @@ -56,6 +57,10 @@ func (ctx *BuiltinEvalContext) Stopped() <-chan struct{} {
return ctx.StopContext.Done()
}

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 @@ -206,3 +206,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
6 changes: 6 additions & 0 deletions terraform/graph_walk_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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{
StopContext: w.StopContext,
PathValue: path,
Expand All @@ -92,6 +97,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_abstract_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (n *NodeAbstractCountResource) EvalTree() EvalNode {
// into more computed values.
var evalCountCheckComputed EvalNode
if !n.Validate {
evalCountCheckComputed = &EvalCountCheckComputed{Resource: n.Config}
evalCountCheckComputed = &EvalCountFixComputed{Resource: n.Config}
}

return &EvalSequence{
Expand Down

0 comments on commit 62d338c

Please sign in to comment.