2015-02-12 07:33:38 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-04-11 19:40:06 +02:00
|
|
|
"log"
|
2015-02-12 07:38:38 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
2015-02-12 07:33:38 +01:00
|
|
|
)
|
|
|
|
|
2015-04-29 20:27:12 +02:00
|
|
|
// EvalDeleteOutput is an EvalNode implementation that deletes an output
|
|
|
|
// from the state.
|
|
|
|
type EvalDeleteOutput struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: test
|
|
|
|
func (n *EvalDeleteOutput) Eval(ctx EvalContext) (interface{}, error) {
|
|
|
|
state, lock := ctx.State()
|
|
|
|
if state == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a write lock so we can access this instance
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
// Look for the module state. If we don't have one, create it.
|
|
|
|
mod := state.ModuleByPath(ctx.Path())
|
|
|
|
if mod == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(mod.Outputs, n.Name)
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 07:33:38 +01:00
|
|
|
// EvalWriteOutput is an EvalNode implementation that writes the output
|
|
|
|
// for the given name to the current state.
|
|
|
|
type EvalWriteOutput struct {
|
2016-05-12 02:05:02 +02:00
|
|
|
Name string
|
|
|
|
Sensitive bool
|
|
|
|
Value *config.RawConfig
|
2015-02-12 07:33:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: test
|
2015-02-14 07:58:41 +01:00
|
|
|
func (n *EvalWriteOutput) Eval(ctx EvalContext) (interface{}, error) {
|
2015-02-14 01:41:09 +01:00
|
|
|
cfg, err := ctx.Interpolate(n.Value, nil)
|
|
|
|
if err != nil {
|
2016-04-11 19:40:06 +02:00
|
|
|
// Log error but continue anyway
|
|
|
|
log.Printf("[WARN] Output interpolation %q failed: %s", n.Name, err)
|
2015-02-14 01:41:09 +01:00
|
|
|
}
|
2015-02-12 07:33:38 +01:00
|
|
|
|
|
|
|
state, lock := ctx.State()
|
|
|
|
if state == nil {
|
|
|
|
return nil, fmt.Errorf("cannot write state to nil state")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a write lock so we can access this instance
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
// Look for the module state. If we don't have one, create it.
|
|
|
|
mod := state.ModuleByPath(ctx.Path())
|
|
|
|
if mod == nil {
|
|
|
|
mod = state.AddModule(ctx.Path())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the value from the config
|
2015-02-14 01:41:09 +01:00
|
|
|
var valueRaw interface{} = config.UnknownVariableValue
|
|
|
|
if cfg != nil {
|
|
|
|
var ok bool
|
|
|
|
valueRaw, ok = cfg.Get("value")
|
|
|
|
if !ok {
|
|
|
|
valueRaw = ""
|
|
|
|
}
|
|
|
|
if cfg.IsComputed("value") {
|
|
|
|
valueRaw = config.UnknownVariableValue
|
|
|
|
}
|
2015-02-12 07:38:38 +01:00
|
|
|
}
|
2015-02-12 07:33:38 +01:00
|
|
|
|
2016-04-11 19:40:06 +02:00
|
|
|
switch valueTyped := valueRaw.(type) {
|
|
|
|
case string:
|
2016-05-12 02:05:02 +02:00
|
|
|
mod.Outputs[n.Name] = &OutputState{
|
|
|
|
Type: "string",
|
|
|
|
Sensitive: n.Sensitive,
|
|
|
|
Value: valueTyped,
|
|
|
|
}
|
2016-04-11 19:40:06 +02:00
|
|
|
case []interface{}:
|
2016-05-12 02:05:02 +02:00
|
|
|
mod.Outputs[n.Name] = &OutputState{
|
|
|
|
Type: "list",
|
|
|
|
Sensitive: n.Sensitive,
|
|
|
|
Value: valueTyped,
|
|
|
|
}
|
2016-04-11 19:40:06 +02:00
|
|
|
case map[string]interface{}:
|
2016-05-12 02:05:02 +02:00
|
|
|
mod.Outputs[n.Name] = &OutputState{
|
|
|
|
Type: "map",
|
|
|
|
Sensitive: n.Sensitive,
|
|
|
|
Value: valueTyped,
|
|
|
|
}
|
2016-10-07 21:00:50 +02:00
|
|
|
case []map[string]interface{}:
|
|
|
|
// an HCL map is multi-valued, so if this was read out of a config the
|
|
|
|
// map may still be in a slice.
|
|
|
|
if len(valueTyped) == 1 {
|
|
|
|
mod.Outputs[n.Name] = &OutputState{
|
|
|
|
Type: "map",
|
|
|
|
Sensitive: n.Sensitive,
|
|
|
|
Value: valueTyped[0],
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("output %s type (%T) with %d values not valid for type map",
|
|
|
|
n.Name, valueTyped, len(valueTyped))
|
2016-04-11 19:40:06 +02:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("output %s is not a valid type (%T)\n", n.Name, valueTyped)
|
2015-02-12 07:33:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|