github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/terraform/eval_variable.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/errwrap"
     7  	"github.com/hashicorp/terraform/config"
     8  	"github.com/mitchellh/mapstructure"
     9  )
    10  
    11  // EvalSetVariables is an EvalNode implementation that sets the variables
    12  // explicitly for interpolation later.
    13  type EvalSetVariables struct {
    14  	Module    *string
    15  	Variables map[string]string
    16  }
    17  
    18  // TODO: test
    19  func (n *EvalSetVariables) Eval(ctx EvalContext) (interface{}, error) {
    20  	ctx.SetVariables(*n.Module, n.Variables)
    21  	return nil, nil
    22  }
    23  
    24  // EvalVariableBlock is an EvalNode implementation that evaluates the
    25  // given configuration, and uses the final values as a way to set the
    26  // mapping.
    27  type EvalVariableBlock struct {
    28  	Config    **ResourceConfig
    29  	Variables map[string]string
    30  }
    31  
    32  // TODO: test
    33  func (n *EvalVariableBlock) Eval(ctx EvalContext) (interface{}, error) {
    34  	// Clear out the existing mapping
    35  	for k, _ := range n.Variables {
    36  		delete(n.Variables, k)
    37  	}
    38  
    39  	// Get our configuration
    40  	rc := *n.Config
    41  	for k, v := range rc.Config {
    42  		var vStr string
    43  		if err := mapstructure.WeakDecode(v, &vStr); err != nil {
    44  			return nil, errwrap.Wrapf(fmt.Sprintf(
    45  				"%s: error reading value: {{err}}", k), err)
    46  		}
    47  
    48  		n.Variables[k] = vStr
    49  	}
    50  	for k, _ := range rc.Raw {
    51  		if _, ok := n.Variables[k]; !ok {
    52  			n.Variables[k] = config.UnknownVariableValue
    53  		}
    54  	}
    55  
    56  	return nil, nil
    57  }