Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix count interpolation with computed values #10418

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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