From 40ee70d5c94719f926dc5c9b553b4e53bdd90c97 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 20 Feb 2015 15:35:57 -0800 Subject: [PATCH] terraform: Input should only be called on providers once /cc @sethvargo Prior to this commit, we'd only persist the result of calling Input if any input was given (len(result) > 0). The result was that every module would also repeat asking for input even if there was no input to be asked for. This commit makes it so that if no input was received, we still set a sentinel so that modules don't re-ask. --- terraform/context_test.go | 27 +++++++++++++++++++ terraform/eval_provider.go | 5 +++- .../input-provider-once/child/main.tf | 2 ++ .../test-fixtures/input-provider-once/main.tf | 5 ++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 terraform/test-fixtures/input-provider-once/child/main.tf create mode 100644 terraform/test-fixtures/input-provider-once/main.tf diff --git a/terraform/context_test.go b/terraform/context_test.go index 01f3745be..a180154be 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -2463,6 +2463,33 @@ func TestContext2Input_provider(t *testing.T) { } } +func TestContext2Input_providerOnce(t *testing.T) { + m := testModule(t, "input-provider-once") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + count := 0 + p.InputFn = func(i UIInput, c *ResourceConfig) (*ResourceConfig, error) { + count++ + return nil, nil + } + + if err := ctx.Input(InputModeStd); err != nil { + t.Fatalf("err: %s", err) + } + + if count != 1 { + t.Fatalf("should only be called once: %d", count) + } +} + func TestContext2Input_providerId(t *testing.T) { input := new(MockUIInput) m := testModule(t, "input-provider") diff --git a/terraform/eval_provider.go b/terraform/eval_provider.go index bc9a2652c..f648fe46f 100644 --- a/terraform/eval_provider.go +++ b/terraform/eval_provider.go @@ -99,9 +99,12 @@ func (n *EvalInputProvider) Eval(ctx EvalContext) (interface{}, error) { "Error configuring %s: %s", n.Name, err) } + // Set the input that we received so that child modules don't attempt + // to ask for input again. if config != nil && len(config.Config) > 0 { - // Set the configuration ctx.SetProviderInput(n.Name, config.Config) + } else { + ctx.SetProviderInput(n.Name, map[string]interface{}{}) } return nil, nil diff --git a/terraform/test-fixtures/input-provider-once/child/main.tf b/terraform/test-fixtures/input-provider-once/child/main.tf new file mode 100644 index 000000000..ca39ff5e5 --- /dev/null +++ b/terraform/test-fixtures/input-provider-once/child/main.tf @@ -0,0 +1,2 @@ +provider "aws" {} +resource "aws_instance" "bar" {} diff --git a/terraform/test-fixtures/input-provider-once/main.tf b/terraform/test-fixtures/input-provider-once/main.tf new file mode 100644 index 000000000..006a74087 --- /dev/null +++ b/terraform/test-fixtures/input-provider-once/main.tf @@ -0,0 +1,5 @@ +resource "aws_instance" "foo" {} + +module "child" { + source = "./child" +}