github.com/nov1n/terraform@v0.7.9-0.20161103151050-bf6852f38e28/terraform/node_module_variable.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/config"
     7  	"github.com/hashicorp/terraform/config/module"
     8  )
     9  
    10  // NodeApplyableModuleVariable represents a module variable input during
    11  // the apply step.
    12  type NodeApplyableModuleVariable struct {
    13  	PathValue []string
    14  	Config    *config.Variable  // Config is the var in the config
    15  	Value     *config.RawConfig // Value is the value that is set
    16  
    17  	Module *module.Tree // Antiquated, want to remove
    18  }
    19  
    20  func (n *NodeApplyableModuleVariable) Name() string {
    21  	result := fmt.Sprintf("var.%s", n.Config.Name)
    22  	if len(n.PathValue) > 1 {
    23  		result = fmt.Sprintf("%s.%s", modulePrefixStr(n.PathValue), result)
    24  	}
    25  
    26  	return result
    27  }
    28  
    29  // GraphNodeSubPath
    30  func (n *NodeApplyableModuleVariable) Path() []string {
    31  	// We execute in the parent scope (above our own module) so that
    32  	// we can access the proper interpolations.
    33  	if len(n.PathValue) > 2 {
    34  		return n.PathValue[:len(n.PathValue)-1]
    35  	}
    36  
    37  	return rootModulePath
    38  }
    39  
    40  // GraphNodeReferenceGlobal
    41  func (n *NodeApplyableModuleVariable) ReferenceGlobal() bool {
    42  	// We have to create fully qualified references because we cross
    43  	// boundaries here: our ReferenceableName is in one path and our
    44  	// References are from another path.
    45  	return true
    46  }
    47  
    48  // GraphNodeReferenceable
    49  func (n *NodeApplyableModuleVariable) ReferenceableName() []string {
    50  	return []string{n.Name()}
    51  }
    52  
    53  // GraphNodeReferencer
    54  func (n *NodeApplyableModuleVariable) References() []string {
    55  	// If we have no value set, we depend on nothing
    56  	if n.Value == nil {
    57  		return nil
    58  	}
    59  
    60  	// Can't depend on anything if we're in the root
    61  	if len(n.PathValue) < 2 {
    62  		return nil
    63  	}
    64  
    65  	// Otherwise, we depend on anything that is in our value, but
    66  	// specifically in the namespace of the parent path.
    67  	// Create the prefix based on the path
    68  	var prefix string
    69  	if p := n.Path(); len(p) > 0 {
    70  		prefix = modulePrefixStr(p)
    71  	}
    72  
    73  	result := ReferencesFromConfig(n.Value)
    74  	return modulePrefixList(result, prefix)
    75  }
    76  
    77  // GraphNodeEvalable
    78  func (n *NodeApplyableModuleVariable) EvalTree() EvalNode {
    79  	// If we have no value, do nothing
    80  	if n.Value == nil {
    81  		return &EvalNoop{}
    82  	}
    83  
    84  	// Otherwise, interpolate the value of this variable and set it
    85  	// within the variables mapping.
    86  	var config *ResourceConfig
    87  	variables := make(map[string]interface{})
    88  	return &EvalSequence{
    89  		Nodes: []EvalNode{
    90  			&EvalInterpolate{
    91  				Config: n.Value,
    92  				Output: &config,
    93  			},
    94  
    95  			&EvalVariableBlock{
    96  				Config:         &config,
    97  				VariableValues: variables,
    98  			},
    99  
   100  			&EvalCoerceMapVariable{
   101  				Variables:  variables,
   102  				ModulePath: n.PathValue,
   103  				ModuleTree: n.Module,
   104  			},
   105  
   106  			&EvalTypeCheckVariable{
   107  				Variables:  variables,
   108  				ModulePath: n.PathValue,
   109  				ModuleTree: n.Module,
   110  			},
   111  
   112  			&EvalSetVariables{
   113  				Module:    &n.PathValue[len(n.PathValue)-1],
   114  				Variables: variables,
   115  			},
   116  		},
   117  	}
   118  }