core: Don't save provider input for non-root module

We only support provider input for the root module. This is already
checked in ProviderInput, but was not checked in SetProviderInput. We
can't actually do anything particularly clever with an invalid call here,
but we will at least generate a WARN log to help with debugging.

Also need to update TestBuiltinEvalContextProviderInput to expect this
new behavior of ignoring input for non-root modules.
This commit is contained in:
Martin Atkins 2018-06-01 15:08:56 -07:00
parent b99b31ebea
commit 059de66fcc
2 changed files with 11 additions and 5 deletions

View File

@ -198,6 +198,12 @@ func (ctx *BuiltinEvalContext) ProviderInput(pc addrs.ProviderConfig) map[string
func (ctx *BuiltinEvalContext) SetProviderInput(pc addrs.ProviderConfig, c map[string]cty.Value) {
absProvider := pc.Absolute(ctx.Path())
if !ctx.Path().IsRoot() {
// Only root module provider configurations can have input.
log.Printf("[WARN] BuiltinEvalContext: attempt to SetProviderInput for non-root module")
return
}
// Save the configuration
ctx.ProviderLock.Lock()
ctx.ProviderInputConfig[absProvider.String()] = c

View File

@ -28,17 +28,17 @@ func TestBuiltinEvalContextProviderInput(t *testing.T) {
expected1 := map[string]cty.Value{"value": cty.StringVal("foo")}
ctx1.SetProviderInput(providerAddr, expected1)
expected2 := map[string]cty.Value{"value": cty.StringVal("bar")}
ctx2.SetProviderInput(providerAddr, expected2)
try2 := map[string]cty.Value{"value": cty.StringVal("bar")}
ctx2.SetProviderInput(providerAddr, try2) // ignored because not a root module
actual1 := ctx1.ProviderInput(providerAddr)
actual2 := ctx2.ProviderInput(providerAddr)
if !reflect.DeepEqual(actual1, expected1) {
t.Fatalf("bad: %#v %#v", actual1, expected1)
t.Errorf("wrong result 1\ngot: %#v\nwant: %#v", actual1, expected1)
}
if !reflect.DeepEqual(actual2, expected2) {
t.Fatalf("bad: %#v %#v", actual2, expected2)
if actual2 != nil {
t.Errorf("wrong result 2\ngot: %#v\nwant: %#v", actual2, nil)
}
}