github.com/rmenn/terraform@v0.3.8-0.20150225065417-fc84b3a78802/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  	Variables map[string]string
    15  }
    16  
    17  // TODO: test
    18  func (n *EvalSetVariables) Eval(ctx EvalContext) (interface{}, error) {
    19  	ctx.SetVariables(n.Variables)
    20  	return nil, nil
    21  }
    22  
    23  // EvalVariableBlock is an EvalNode implementation that evaluates the
    24  // given configuration, and uses the final values as a way to set the
    25  // mapping.
    26  type EvalVariableBlock struct {
    27  	Config    **ResourceConfig
    28  	Variables map[string]string
    29  }
    30  
    31  // TODO: test
    32  func (n *EvalVariableBlock) Eval(ctx EvalContext) (interface{}, error) {
    33  	// Clear out the existing mapping
    34  	for k, _ := range n.Variables {
    35  		delete(n.Variables, k)
    36  	}
    37  
    38  	// Get our configuration
    39  	rc := *n.Config
    40  	for k, v := range rc.Config {
    41  		var vStr string
    42  		if err := mapstructure.WeakDecode(v, &vStr); err != nil {
    43  			return nil, errwrap.Wrapf(fmt.Sprintf(
    44  				"%s: error reading value: {{err}}", k), err)
    45  		}
    46  
    47  		n.Variables[k] = vStr
    48  	}
    49  	for k, _ := range rc.Raw {
    50  		if _, ok := n.Variables[k]; !ok {
    51  			n.Variables[k] = config.UnknownVariableValue
    52  		}
    53  	}
    54  
    55  	return nil, nil
    56  }