github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/terraform/graph_walk_context.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"sync"
     7  
     8  	"github.com/hashicorp/errwrap"
     9  	"github.com/hashicorp/terraform/dag"
    10  )
    11  
    12  // ContextGraphWalker is the GraphWalker implementation used with the
    13  // Context struct to walk and evaluate the graph.
    14  type ContextGraphWalker struct {
    15  	NullGraphWalker
    16  
    17  	// Configurable values
    18  	Context    *Context
    19  	Operation  walkOperation
    20  	DebugGraph *DebugGraph
    21  
    22  	// Outputs, do not set these. Do not read these while the graph
    23  	// is being walked.
    24  	ValidationWarnings []string
    25  	ValidationErrors   []error
    26  
    27  	errorLock           sync.Mutex
    28  	once                sync.Once
    29  	contexts            map[string]*BuiltinEvalContext
    30  	contextLock         sync.Mutex
    31  	interpolaterVars    map[string]map[string]interface{}
    32  	interpolaterVarLock sync.Mutex
    33  	providerCache       map[string]ResourceProvider
    34  	providerConfigCache map[string]*ResourceConfig
    35  	providerLock        sync.Mutex
    36  	provisionerCache    map[string]ResourceProvisioner
    37  	provisionerLock     sync.Mutex
    38  }
    39  
    40  func (w *ContextGraphWalker) EnterPath(path []string) EvalContext {
    41  	w.once.Do(w.init)
    42  
    43  	w.contextLock.Lock()
    44  	defer w.contextLock.Unlock()
    45  
    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 {
    49  		return ctx
    50  	}
    51  
    52  	// Setup the variables for this interpolater
    53  	variables := make(map[string]interface{})
    54  	if len(path) <= 1 {
    55  		for k, v := range w.Context.variables {
    56  			variables[k] = v
    57  		}
    58  	}
    59  	w.interpolaterVarLock.Lock()
    60  	if m, ok := w.interpolaterVars[key]; ok {
    61  		for k, v := range m {
    62  			variables[k] = v
    63  		}
    64  	}
    65  	w.interpolaterVars[key] = variables
    66  	w.interpolaterVarLock.Unlock()
    67  
    68  	ctx := &BuiltinEvalContext{
    69  		PathValue:           path,
    70  		Hooks:               w.Context.hooks,
    71  		InputValue:          w.Context.uiInput,
    72  		Components:          w.Context.components,
    73  		ProviderCache:       w.providerCache,
    74  		ProviderConfigCache: w.providerConfigCache,
    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,
    85  			Module:             w.Context.module,
    86  			State:              w.Context.state,
    87  			StateLock:          &w.Context.stateLock,
    88  			VariableValues:     variables,
    89  			VariableValuesLock: &w.interpolaterVarLock,
    90  		},
    91  		InterpolaterVars:    w.interpolaterVars,
    92  		InterpolaterVarLock: &w.interpolaterVarLock,
    93  	}
    94  
    95  	w.contexts[key] = ctx
    96  	return ctx
    97  }
    98  
    99  func (w *ContextGraphWalker) EnterEvalTree(v dag.Vertex, n EvalNode) EvalNode {
   100  	log.Printf("[TRACE] [%s] Entering eval tree: %s",
   101  		w.Operation, dag.VertexName(v))
   102  
   103  	// Acquire a lock on the semaphore
   104  	w.Context.parallelSem.Acquire()
   105  
   106  	// We want to filter the evaluation tree to only include operations
   107  	// that belong in this operation.
   108  	return EvalFilter(n, EvalNodeFilterOp(w.Operation))
   109  }
   110  
   111  func (w *ContextGraphWalker) ExitEvalTree(
   112  	v dag.Vertex, output interface{}, err error) error {
   113  	log.Printf("[TRACE] [%s] Exiting eval tree: %s",
   114  		w.Operation, dag.VertexName(v))
   115  
   116  	// Release the semaphore
   117  	w.Context.parallelSem.Release()
   118  
   119  	if err == nil {
   120  		return nil
   121  	}
   122  
   123  	// Acquire the lock because anything is going to require a lock.
   124  	w.errorLock.Lock()
   125  	defer w.errorLock.Unlock()
   126  
   127  	// Try to get a validation error out of it. If its not a validation
   128  	// error, then just record the normal error.
   129  	verr, ok := err.(*EvalValidateError)
   130  	if !ok {
   131  		return err
   132  	}
   133  
   134  	for _, msg := range verr.Warnings {
   135  		w.ValidationWarnings = append(
   136  			w.ValidationWarnings,
   137  			fmt.Sprintf("%s: %s", dag.VertexName(v), msg))
   138  	}
   139  	for _, e := range verr.Errors {
   140  		w.ValidationErrors = append(
   141  			w.ValidationErrors,
   142  			errwrap.Wrapf(fmt.Sprintf("%s: {{err}}", dag.VertexName(v)), e))
   143  	}
   144  
   145  	return nil
   146  }
   147  
   148  func (w *ContextGraphWalker) Debug() *DebugGraph {
   149  	return w.DebugGraph
   150  }
   151  
   152  func (w *ContextGraphWalker) init() {
   153  	w.contexts = make(map[string]*BuiltinEvalContext, 5)
   154  	w.providerCache = make(map[string]ResourceProvider, 5)
   155  	w.providerConfigCache = make(map[string]*ResourceConfig, 5)
   156  	w.provisionerCache = make(map[string]ResourceProvisioner, 5)
   157  	w.interpolaterVars = make(map[string]map[string]interface{}, 5)
   158  }