-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: evaluate locals and return them for interpolation
We stash the locals in the module state in a map that is ignored for JSON serialization. We don't include locals in the persisted state because they can be trivially recomputed and this allows us to assume that they will pass through verbatim, without any normalization or other transforms caused by the JSON serialization. From a user standpoint a local is just a named alias for an expression, so it's desirable that the result passes through here in as raw a form as possible, so it behaves as closely as possible to simply using the given expression directly.
- Loading branch information
1 parent
6cdf9f7
commit 2ac7afd
Showing
11 changed files
with
295 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package terraform | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/config" | ||
) | ||
|
||
// EvalLocal is an EvalNode implementation that evaluates the | ||
// expression for a local value and writes it into a transient part of | ||
// the state. | ||
type EvalLocal struct { | ||
Name string | ||
Value *config.RawConfig | ||
} | ||
|
||
func (n *EvalLocal) Eval(ctx EvalContext) (interface{}, error) { | ||
cfg, err := ctx.Interpolate(n.Value, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("local.%s: %s", n.Name, err) | ||
} | ||
|
||
state, lock := ctx.State() | ||
if state == nil { | ||
return nil, fmt.Errorf("cannot write local value to nil state") | ||
} | ||
|
||
// Get a write lock so we can access the state | ||
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 | ||
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 | ||
} | ||
} | ||
|
||
if mod.Locals == nil { | ||
// initialize | ||
mod.Locals = map[string]interface{}{} | ||
} | ||
mod.Locals[n.Name] = valueRaw | ||
|
||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package terraform | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/davecgh/go-spew/spew" | ||
"github.com/hashicorp/terraform/config" | ||
) | ||
|
||
func TestEvalLocal_impl(t *testing.T) { | ||
var _ EvalNode = new(EvalLocal) | ||
} | ||
|
||
func TestEvalLocal(t *testing.T) { | ||
tests := []struct { | ||
Value string | ||
Want interface{} | ||
Err bool | ||
}{ | ||
{ | ||
"hello!", | ||
"hello!", | ||
false, | ||
}, | ||
{ | ||
"", | ||
"", | ||
false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Value, func(t *testing.T) { | ||
rawConfig, err := config.NewRawConfig(map[string]interface{}{ | ||
"value": test.Value, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
n := &EvalLocal{ | ||
Name: "foo", | ||
Value: rawConfig, | ||
} | ||
ctx := &MockEvalContext{ | ||
StateState: &State{}, | ||
StateLock: &sync.RWMutex{}, | ||
|
||
InterpolateConfigResult: testResourceConfig(t, map[string]interface{}{ | ||
"value": test.Want, | ||
}), | ||
} | ||
|
||
_, err = n.Eval(ctx) | ||
if (err != nil) != test.Err { | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err) | ||
} else { | ||
t.Errorf("successful Eval; want error") | ||
} | ||
} | ||
|
||
ms := ctx.StateState.ModuleByPath([]string{}) | ||
gotLocals := ms.Locals | ||
wantLocals := map[string]interface{}{ | ||
"foo": test.Want, | ||
} | ||
|
||
if !reflect.DeepEqual(gotLocals, wantLocals) { | ||
t.Errorf( | ||
"wrong locals after Eval\ngot: %swant: %s", | ||
spew.Sdump(gotLocals), spew.Sdump(wantLocals), | ||
) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
output "result" { | ||
value = "hello" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
module "child" { | ||
source = "./child" | ||
} | ||
|
||
locals { | ||
result_1 = "${module.child.result}" | ||
result_2 = "${local.result_1}" | ||
result_3 = "${local.result_2} world" | ||
} | ||
|
||
output "result_1" { | ||
value = "${local.result_1}" | ||
} | ||
|
||
output "result_3" { | ||
value = "${local.result_3}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
locals { | ||
foo = "..." | ||
} |