9 "github.com/hashicorp/errwrap"
10 "github.com/hashicorp/terraform/dag"
13 // ContextGraphWalker is the GraphWalker implementation used with the
14 // Context struct to walk and evaluate the graph.
15 type ContextGraphWalker struct {
18 // Configurable values
20 Operation walkOperation
21 StopContext context.Context
23 // Outputs, do not set these. Do not read these while the graph
25 ValidationWarnings []string
26 ValidationErrors []error
30 contexts map[string]*BuiltinEvalContext
31 contextLock sync.Mutex
32 interpolaterVars map[string]map[string]interface{}
33 interpolaterVarLock sync.Mutex
34 providerCache map[string]ResourceProvider
35 providerLock sync.Mutex
36 provisionerCache map[string]ResourceProvisioner
37 provisionerLock sync.Mutex
40 func (w *ContextGraphWalker) EnterPath(path []string) EvalContext {
44 defer w.contextLock.Unlock()
46 // If we already have a context for this path cached, use that
47 key := PathCacheKey(path)
48 if ctx, ok := w.contexts[key]; ok {
52 // Setup the variables for this interpolater
53 variables := make(map[string]interface{})
55 for k, v := range w.Context.variables {
59 w.interpolaterVarLock.Lock()
60 if m, ok := w.interpolaterVars[key]; ok {
65 w.interpolaterVars[key] = variables
66 w.interpolaterVarLock.Unlock()
68 ctx := &BuiltinEvalContext{
69 StopContext: w.StopContext,
71 Hooks: w.Context.hooks,
72 InputValue: w.Context.uiInput,
73 Components: w.Context.components,
74 ProviderCache: w.providerCache,
75 ProviderInputConfig: w.Context.providerInputConfig,
76 ProviderLock: &w.providerLock,
77 ProvisionerCache: w.provisionerCache,
78 ProvisionerLock: &w.provisionerLock,
79 DiffValue: w.Context.diff,
80 DiffLock: &w.Context.diffLock,
81 StateValue: w.Context.state,
82 StateLock: &w.Context.stateLock,
83 Interpolater: &Interpolater{
84 Operation: w.Operation,
86 Module: w.Context.module,
87 State: w.Context.state,
88 StateLock: &w.Context.stateLock,
89 VariableValues: variables,
90 VariableValuesLock: &w.interpolaterVarLock,
92 InterpolaterVars: w.interpolaterVars,
93 InterpolaterVarLock: &w.interpolaterVarLock,
100 func (w *ContextGraphWalker) EnterEvalTree(v dag.Vertex, n EvalNode) EvalNode {
101 log.Printf("[TRACE] [%s] Entering eval tree: %s",
102 w.Operation, dag.VertexName(v))
104 // Acquire a lock on the semaphore
105 w.Context.parallelSem.Acquire()
107 // We want to filter the evaluation tree to only include operations
108 // that belong in this operation.
109 return EvalFilter(n, EvalNodeFilterOp(w.Operation))
112 func (w *ContextGraphWalker) ExitEvalTree(
113 v dag.Vertex, output interface{}, err error) error {
114 log.Printf("[TRACE] [%s] Exiting eval tree: %s",
115 w.Operation, dag.VertexName(v))
117 // Release the semaphore
118 w.Context.parallelSem.Release()
124 // Acquire the lock because anything is going to require a lock.
126 defer w.errorLock.Unlock()
128 // Try to get a validation error out of it. If its not a validation
129 // error, then just record the normal error.
130 verr, ok := err.(*EvalValidateError)
135 for _, msg := range verr.Warnings {
136 w.ValidationWarnings = append(
137 w.ValidationWarnings,
138 fmt.Sprintf("%s: %s", dag.VertexName(v), msg))
140 for _, e := range verr.Errors {
141 w.ValidationErrors = append(
143 errwrap.Wrapf(fmt.Sprintf("%s: {{err}}", dag.VertexName(v)), e))
149 func (w *ContextGraphWalker) init() {
150 w.contexts = make(map[string]*BuiltinEvalContext, 5)
151 w.providerCache = make(map[string]ResourceProvider, 5)
152 w.provisionerCache = make(map[string]ResourceProvisioner, 5)
153 w.interpolaterVars = make(map[string]map[string]interface{}, 5)