diff --git a/terraform/context_input_test.go b/terraform/context_input_test.go index 928c114772..750db918a5 100644 --- a/terraform/context_input_test.go +++ b/terraform/context_input_test.go @@ -719,3 +719,57 @@ func TestContext2Input_submoduleTriggersInvalidCount(t *testing.T) { t.Fatalf("err: %s", err) } } + +// In this case, a module variable can't be resolved from a data source until +// it's refreshed, but it can't be refreshed during Input. +func TestContext2Input_dataSourceRequiresRefresh(t *testing.T) { + input := new(MockUIInput) + p := testProvider("null") + m := testModule(t, "input-module-data-vars") + + p.ReadDataDiffFn = testDataDiffFn + + state := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "data.null_data_source.bar": &ResourceState{ + Type: "null_data_source", + Primary: &InstanceState{ + ID: "-", + Attributes: map[string]string{ + "foo.#": "1", + "foo.0": "a", + // foo.1 exists in the data source, but needs to be refreshed. + }, + }, + }, + }, + }, + }, + } + + ctx := testContext2(t, &ContextOpts{ + Module: m, + ProviderResolver: ResourceProviderResolverFixed( + map[string]ResourceProviderFactory{ + "null": testProviderFuncFixed(p), + }, + ), + State: state, + UIInput: input, + }) + + if err := ctx.Input(InputModeStd); err != nil { + t.Fatalf("err: %s", err) + } + + // ensure that plan works after Refresh + if _, err := ctx.Refresh(); err != nil { + t.Fatalf("err: %s", err) + } + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } +} diff --git a/terraform/test-fixtures/input-module-data-vars/child/main.tf b/terraform/test-fixtures/input-module-data-vars/child/main.tf new file mode 100644 index 0000000000..aa5d69bd5f --- /dev/null +++ b/terraform/test-fixtures/input-module-data-vars/child/main.tf @@ -0,0 +1,5 @@ +variable "in" {} + +output "out" { + value = "${var.in}" +} diff --git a/terraform/test-fixtures/input-module-data-vars/main.tf b/terraform/test-fixtures/input-module-data-vars/main.tf new file mode 100644 index 0000000000..0a327b1024 --- /dev/null +++ b/terraform/test-fixtures/input-module-data-vars/main.tf @@ -0,0 +1,8 @@ +data "null_data_source" "bar" { + foo = ["a", "b"] +} + +module "child" { + source = "./child" + in = "${data.null_data_source.bar.foo[1]}" +}