2015-02-07 15:29:55 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2015-02-16 21:11:23 -06:00
|
|
|
"fmt"
|
2015-10-14 12:43:51 -05:00
|
|
|
"log"
|
2015-02-07 15:29:55 -06:00
|
|
|
"sync"
|
|
|
|
|
2015-02-16 21:11:23 -06:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-02-07 15:29:55 -06:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ContextGraphWalker is the GraphWalker implementation used with the
|
|
|
|
// Context struct to walk and evaluate the graph.
|
|
|
|
type ContextGraphWalker struct {
|
|
|
|
NullGraphWalker
|
|
|
|
|
2015-02-07 17:14:34 -06:00
|
|
|
// Configurable values
|
2016-11-10 15:18:18 -06:00
|
|
|
Context *Context
|
|
|
|
Operation walkOperation
|
2015-02-07 15:29:55 -06:00
|
|
|
|
2015-02-07 17:14:34 -06:00
|
|
|
// Outputs, do not set these. Do not read these while the graph
|
|
|
|
// is being walked.
|
2015-02-07 15:29:55 -06:00
|
|
|
ValidationWarnings []string
|
|
|
|
ValidationErrors []error
|
2015-02-07 17:14:34 -06:00
|
|
|
|
2015-02-10 01:32:28 -06:00
|
|
|
errorLock sync.Mutex
|
|
|
|
once sync.Once
|
2015-02-11 19:01:08 -06:00
|
|
|
contexts map[string]*BuiltinEvalContext
|
|
|
|
contextLock sync.Mutex
|
2016-04-11 12:40:06 -05:00
|
|
|
interpolaterVars map[string]map[string]interface{}
|
2015-05-01 18:29:19 -05:00
|
|
|
interpolaterVarLock sync.Mutex
|
2015-02-10 01:32:28 -06:00
|
|
|
providerCache map[string]ResourceProvider
|
|
|
|
providerConfigCache map[string]*ResourceConfig
|
|
|
|
providerLock sync.Mutex
|
|
|
|
provisionerCache map[string]ResourceProvisioner
|
|
|
|
provisionerLock sync.Mutex
|
2015-02-07 15:29:55 -06:00
|
|
|
}
|
|
|
|
|
2015-05-01 16:19:32 -05:00
|
|
|
func (w *ContextGraphWalker) EnterPath(path []string) EvalContext {
|
2015-02-08 19:58:02 -06:00
|
|
|
w.once.Do(w.init)
|
|
|
|
|
2015-02-11 19:01:08 -06:00
|
|
|
w.contextLock.Lock()
|
|
|
|
defer w.contextLock.Unlock()
|
|
|
|
|
|
|
|
// If we already have a context for this path cached, use that
|
2015-05-01 16:19:32 -05:00
|
|
|
key := PathCacheKey(path)
|
2015-02-11 19:01:08 -06:00
|
|
|
if ctx, ok := w.contexts[key]; ok {
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2015-05-01 18:29:19 -05:00
|
|
|
// Setup the variables for this interpolater
|
2016-04-11 12:40:06 -05:00
|
|
|
variables := make(map[string]interface{})
|
2015-05-01 18:29:19 -05:00
|
|
|
if len(path) <= 1 {
|
|
|
|
for k, v := range w.Context.variables {
|
|
|
|
variables[k] = v
|
|
|
|
}
|
2015-02-11 19:01:08 -06:00
|
|
|
}
|
2015-05-01 18:29:19 -05:00
|
|
|
w.interpolaterVarLock.Lock()
|
|
|
|
if m, ok := w.interpolaterVars[key]; ok {
|
|
|
|
for k, v := range m {
|
|
|
|
variables[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.interpolaterVars[key] = variables
|
|
|
|
w.interpolaterVarLock.Unlock()
|
2015-02-11 19:01:08 -06:00
|
|
|
|
|
|
|
ctx := &BuiltinEvalContext{
|
2015-05-01 16:19:32 -05:00
|
|
|
PathValue: path,
|
2015-02-11 15:43:07 -06:00
|
|
|
Hooks: w.Context.hooks,
|
2015-02-13 19:59:54 -06:00
|
|
|
InputValue: w.Context.uiInput,
|
2016-10-03 20:23:37 -05:00
|
|
|
Components: w.Context.components,
|
2015-02-10 01:32:28 -06:00
|
|
|
ProviderCache: w.providerCache,
|
|
|
|
ProviderConfigCache: w.providerConfigCache,
|
2015-02-13 19:59:54 -06:00
|
|
|
ProviderInputConfig: w.Context.providerInputConfig,
|
2015-02-10 01:32:28 -06:00
|
|
|
ProviderLock: &w.providerLock,
|
|
|
|
ProvisionerCache: w.provisionerCache,
|
|
|
|
ProvisionerLock: &w.provisionerLock,
|
2015-02-12 21:34:21 -06:00
|
|
|
DiffValue: w.Context.diff,
|
|
|
|
DiffLock: &w.Context.diffLock,
|
2015-02-11 10:48:45 -06:00
|
|
|
StateValue: w.Context.state,
|
|
|
|
StateLock: &w.Context.stateLock,
|
2015-02-07 15:29:55 -06:00
|
|
|
Interpolater: &Interpolater{
|
2016-04-11 12:40:06 -05:00
|
|
|
Operation: w.Operation,
|
|
|
|
Module: w.Context.module,
|
|
|
|
State: w.Context.state,
|
|
|
|
StateLock: &w.Context.stateLock,
|
|
|
|
VariableValues: variables,
|
|
|
|
VariableValuesLock: &w.interpolaterVarLock,
|
2015-02-07 15:29:55 -06:00
|
|
|
},
|
2015-05-01 18:29:19 -05:00
|
|
|
InterpolaterVars: w.interpolaterVars,
|
|
|
|
InterpolaterVarLock: &w.interpolaterVarLock,
|
2015-02-07 15:29:55 -06:00
|
|
|
}
|
2015-02-11 19:01:08 -06:00
|
|
|
|
|
|
|
w.contexts[key] = ctx
|
|
|
|
return ctx
|
2015-02-07 15:29:55 -06:00
|
|
|
}
|
|
|
|
|
2015-02-10 10:58:14 -06:00
|
|
|
func (w *ContextGraphWalker) EnterEvalTree(v dag.Vertex, n EvalNode) EvalNode {
|
2016-02-24 09:49:11 -06:00
|
|
|
log.Printf("[TRACE] [%s] Entering eval tree: %s",
|
|
|
|
w.Operation, dag.VertexName(v))
|
2015-10-14 12:43:51 -05:00
|
|
|
|
2015-02-16 22:27:29 -06:00
|
|
|
// Acquire a lock on the semaphore
|
|
|
|
w.Context.parallelSem.Acquire()
|
|
|
|
|
2015-02-10 10:58:14 -06:00
|
|
|
// We want to filter the evaluation tree to only include operations
|
|
|
|
// that belong in this operation.
|
|
|
|
return EvalFilter(n, EvalNodeFilterOp(w.Operation))
|
|
|
|
}
|
|
|
|
|
2015-02-07 15:29:55 -06:00
|
|
|
func (w *ContextGraphWalker) ExitEvalTree(
|
2015-02-11 20:00:22 -06:00
|
|
|
v dag.Vertex, output interface{}, err error) error {
|
2016-02-24 09:49:11 -06:00
|
|
|
log.Printf("[TRACE] [%s] Exiting eval tree: %s",
|
|
|
|
w.Operation, dag.VertexName(v))
|
2015-10-14 12:43:51 -05:00
|
|
|
|
2015-02-16 22:27:29 -06:00
|
|
|
// Release the semaphore
|
|
|
|
w.Context.parallelSem.Release()
|
|
|
|
|
2015-02-07 15:29:55 -06:00
|
|
|
if err == nil {
|
2015-02-11 20:00:22 -06:00
|
|
|
return nil
|
2015-02-07 15:29:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Acquire the lock because anything is going to require a lock.
|
2015-02-07 17:14:34 -06:00
|
|
|
w.errorLock.Lock()
|
|
|
|
defer w.errorLock.Unlock()
|
2015-02-07 15:29:55 -06:00
|
|
|
|
|
|
|
// Try to get a validation error out of it. If its not a validation
|
|
|
|
// error, then just record the normal error.
|
|
|
|
verr, ok := err.(*EvalValidateError)
|
|
|
|
if !ok {
|
2015-02-11 20:00:22 -06:00
|
|
|
return err
|
2015-02-07 15:29:55 -06:00
|
|
|
}
|
|
|
|
|
2015-02-16 21:11:23 -06:00
|
|
|
for _, msg := range verr.Warnings {
|
|
|
|
w.ValidationWarnings = append(
|
|
|
|
w.ValidationWarnings,
|
|
|
|
fmt.Sprintf("%s: %s", dag.VertexName(v), msg))
|
|
|
|
}
|
|
|
|
for _, e := range verr.Errors {
|
|
|
|
w.ValidationErrors = append(
|
|
|
|
w.ValidationErrors,
|
|
|
|
errwrap.Wrapf(fmt.Sprintf("%s: {{err}}", dag.VertexName(v)), e))
|
|
|
|
}
|
|
|
|
|
2015-02-11 20:00:22 -06:00
|
|
|
return nil
|
2015-02-07 15:29:55 -06:00
|
|
|
}
|
2015-02-08 19:58:02 -06:00
|
|
|
|
|
|
|
func (w *ContextGraphWalker) init() {
|
2015-02-11 19:01:08 -06:00
|
|
|
w.contexts = make(map[string]*BuiltinEvalContext, 5)
|
2015-02-08 19:58:02 -06:00
|
|
|
w.providerCache = make(map[string]ResourceProvider, 5)
|
2015-02-10 01:32:28 -06:00
|
|
|
w.providerConfigCache = make(map[string]*ResourceConfig, 5)
|
2015-02-09 13:15:54 -06:00
|
|
|
w.provisionerCache = make(map[string]ResourceProvisioner, 5)
|
2016-04-11 12:40:06 -05:00
|
|
|
w.interpolaterVars = make(map[string]map[string]interface{}, 5)
|
2015-02-08 19:58:02 -06:00
|
|
|
}
|