github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/terraform/eval_output.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/config"
     7  )
     8  
     9  // EvalWriteOutput is an EvalNode implementation that writes the output
    10  // for the given name to the current state.
    11  type EvalWriteOutput struct {
    12  	Name  string
    13  	Value *config.RawConfig
    14  }
    15  
    16  // TODO: test
    17  func (n *EvalWriteOutput) Eval(ctx EvalContext) (interface{}, error) {
    18  	cfg, err := ctx.Interpolate(n.Value, nil)
    19  	if err != nil {
    20  		// Ignore it
    21  	}
    22  
    23  	state, lock := ctx.State()
    24  	if state == nil {
    25  		return nil, fmt.Errorf("cannot write state to nil state")
    26  	}
    27  
    28  	// Get a write lock so we can access this instance
    29  	lock.Lock()
    30  	defer lock.Unlock()
    31  
    32  	// Look for the module state. If we don't have one, create it.
    33  	mod := state.ModuleByPath(ctx.Path())
    34  	if mod == nil {
    35  		mod = state.AddModule(ctx.Path())
    36  	}
    37  
    38  	// Get the value from the config
    39  	var valueRaw interface{} = config.UnknownVariableValue
    40  	if cfg != nil {
    41  		var ok bool
    42  		valueRaw, ok = cfg.Get("value")
    43  		if !ok {
    44  			valueRaw = ""
    45  		}
    46  		if cfg.IsComputed("value") {
    47  			valueRaw = config.UnknownVariableValue
    48  		}
    49  	}
    50  
    51  	// If it is a list of values, get the first one
    52  	if list, ok := valueRaw.([]interface{}); ok {
    53  		valueRaw = list[0]
    54  	}
    55  	if _, ok := valueRaw.(string); !ok {
    56  		return nil, fmt.Errorf("output %s is not a string", n.Name)
    57  	}
    58  
    59  	// Write the output
    60  	mod.Outputs[n.Name] = valueRaw.(string)
    61  
    62  	return nil, nil
    63  }