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

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