github.com/trawler/terraform@v0.10.8-0.20171106022149-4b1c7a1d9b48/terraform/eval_local.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/terraform/config" 7 ) 8 9 // EvalLocal is an EvalNode implementation that evaluates the 10 // expression for a local value and writes it into a transient part of 11 // the state. 12 type EvalLocal struct { 13 Name string 14 Value *config.RawConfig 15 } 16 17 func (n *EvalLocal) Eval(ctx EvalContext) (interface{}, error) { 18 cfg, err := ctx.Interpolate(n.Value, nil) 19 if err != nil { 20 return nil, fmt.Errorf("local.%s: %s", n.Name, err) 21 } 22 23 state, lock := ctx.State() 24 if state == nil { 25 return nil, fmt.Errorf("cannot write local value to nil state") 26 } 27 28 // Get a write lock so we can access the state 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 mod.Locals == nil { 52 // initialize 53 mod.Locals = map[string]interface{}{} 54 } 55 mod.Locals[n.Name] = valueRaw 56 57 return nil, nil 58 } 59 60 // EvalDeleteLocal is an EvalNode implementation that deletes a Local value 61 // from the state. Locals aren't persisted, but we don't need to evaluate them 62 // during destroy. 63 type EvalDeleteLocal struct { 64 Name string 65 } 66 67 func (n *EvalDeleteLocal) Eval(ctx EvalContext) (interface{}, error) { 68 state, lock := ctx.State() 69 if state == nil { 70 return nil, nil 71 } 72 73 // Get a write lock so we can access this instance 74 lock.Lock() 75 defer lock.Unlock() 76 77 // Look for the module state. If we don't have one, create it. 78 mod := state.ModuleByPath(ctx.Path()) 79 if mod == nil { 80 return nil, nil 81 } 82 83 delete(mod.Locals, n.Name) 84 85 return nil, nil 86 }