github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  
    21  	// Outputs, do not set these. Do not read these while the graph
    22  	// is being walked.
    23  	ValidationWarnings []string
    24  	ValidationErrors   []error
    25  
    26  	errorLock           sync.Mutex
    27  	once                sync.Once
    28  	contexts            map[string]*BuiltinEvalContext
    29  	contextLock         sync.Mutex
    30  	interpolaterVars    map[string]map[string]interface{}
    31  	interpolaterVarLock sync.Mutex
    32  	providerCache       map[string]ResourceProvider
    33  	providerConfigCache map[string]*ResourceConfig
    34  	providerLock        sync.Mutex
    35  	provisionerCache    map[string]ResourceProvisioner
    36  	provisionerLock     sync.Mutex
    37  }
    38  
    39  func (w *ContextGraphWalker) EnterPath(path []string) EvalContext {
    40  	w.once.Do(w.init)
    41  
    42  	w.contextLock.Lock()
    43  	defer w.contextLock.Unlock()
    44  
    45  	// If we already have a context for this path cached, use that
    46  	key := PathCacheKey(path)
    47  	if ctx, ok := w.contexts[key]; ok {
    48  		return ctx
    49  	}
    50  
    51  	// Setup the variables for this interpolater
    52  	variables := make(map[string]interface{})
    53  	if len(path) <= 1 {
    54  		for k, v := range w.Context.variables {
    55  			variables[k] = v
    56  		}
    57  	}
    58  	w.interpolaterVarLock.Lock()
    59  	if m, ok := w.interpolaterVars[key]; ok {
    60  		for k, v := range m {
    61  			variables[k] = v
    62  		}
    63  	}
    64  	w.interpolaterVars[key] = variables
    65  	w.interpolaterVarLock.Unlock()
    66  
    67  	ctx := &BuiltinEvalContext{
    68  		PathValue:           path,
    69  		Hooks:               w.Context.hooks,
    70  		InputValue:          w.Context.uiInput,
    71  		Providers:           w.Context.providers,
    72  		ProviderCache:       w.providerCache,
    73  		ProviderConfigCache: w.providerConfigCache,
    74  		ProviderInputConfig: w.Context.providerInputConfig,
    75  		ProviderLock:        &w.providerLock,
    76  		Provisioners:        w.Context.provisioners,
    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) init() {
   149  	w.contexts = make(map[string]*BuiltinEvalContext, 5)
   150  	w.providerCache = make(map[string]ResourceProvider, 5)
   151  	w.providerConfigCache = make(map[string]*ResourceConfig, 5)
   152  	w.provisionerCache = make(map[string]ResourceProvisioner, 5)
   153  	w.interpolaterVars = make(map[string]map[string]interface{}, 5)
   154  }