github.com/jdextraze/terraform@v0.6.17-0.20160511153921-e33847c8a8af/terraform/eval_output.go (about)

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