github.com/simonswine/terraform@v0.9.0-beta2/terraform/graph_walk_context.go (about)

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