diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index b488d5776b..1a4efde4f4 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -1887,6 +1887,66 @@ func TestContext2Plan_computedDataResource(t *testing.T) { ) } +func TestContext2Plan_computedInFunction(t *testing.T) { + m := testModule(t, "plan-computed-in-function") + p := testProvider("aws") + p.GetSchemaReturn = &ProviderSchema{ + ResourceTypes: map[string]*configschema.Block{ + "aws_instance": { + Attributes: map[string]*configschema.Attribute{ + "attr": {Type: cty.Number, Optional: true}, + }, + }, + }, + DataSources: map[string]*configschema.Block{ + "aws_data_source": { + Attributes: map[string]*configschema.Attribute{ + "computed": {Type: cty.List(cty.String), Computed: true}, + }, + }, + }, + } + p.DiffFn = testDiffFn + + ctx := testContext2(t, &ContextOpts{ + Config: m, + ProviderResolver: providers.ResolverFixed( + map[string]providers.Factory{ + "aws": testProviderFuncFixed(p), + }, + ), + }) + + defer ctx.acquireRun("validate")() + + pgb := &PlanGraphBuilder{ + Config: ctx.config, + State: ctx.state, + Components: ctx.components, + Schemas: ctx.schemas, + Targets: ctx.targets, + } + graph, _ := pgb.Build(addrs.RootModuleInstance) + + // walk + walker := &ContextGraphWalker{ + Context: ctx, + State: ctx.state.SyncWrapper(), + Changes: ctx.changes.SyncWrapper(), + Operation: walkPlan, + StopContext: ctx.runContext, + RootVariableValues: ctx.variables, + } + watchStop, watchWait := ctx.watchStop(walker) + diags := graph.Walk(walker) + close(watchStop) + <-watchWait + + if diags.HasErrors() { + t.Fatalf("unexpected errors: %s", diags.Err()) + } +} + func TestContext2Plan_computedDataCountResource(t *testing.T) { m := testModule(t, "plan-computed-data-count") p := testProvider("aws") diff --git a/terraform/context_validate_test.go b/terraform/context_validate_test.go index bac7bfa5b1..ddf3a679d3 100644 --- a/terraform/context_validate_test.go +++ b/terraform/context_validate_test.go @@ -174,6 +174,42 @@ func TestContext2Validate_computedVar(t *testing.T) { } } +func TestContext2Validate_computedInFunction(t *testing.T) { + p := testProvider("aws") + p.GetSchemaReturn = &ProviderSchema{ + ResourceTypes: map[string]*configschema.Block{ + "aws_instance": { + Attributes: map[string]*configschema.Attribute{ + "attr": {Type: cty.Number, Optional: true}, + }, + }, + }, + DataSources: map[string]*configschema.Block{ + "aws_data_source": { + Attributes: map[string]*configschema.Attribute{ + "optional_attr": {Type: cty.String, Optional: true}, + "computed": {Type: cty.String, Computed: true}, + }, + }, + }, + } + + m := testModule(t, "validate-computed-in-function") + c := testContext2(t, &ContextOpts{ + Config: m, + ProviderResolver: providers.ResolverFixed( + map[string]providers.Factory{ + "aws": testProviderFuncFixed(p), + }, + ), + }) + + diags := c.Validate() + if diags.HasErrors() { + t.Fatalf("unexpected error: %s", diags.Err()) + } +} + // Test that validate allows through computed counts. We do this and allow // them to fail during "plan" since we can't know if the computed values // can be realized during a plan. diff --git a/terraform/test-fixtures/plan-computed-in-function/main.tf b/terraform/test-fixtures/plan-computed-in-function/main.tf new file mode 100644 index 0000000000..554394de6a --- /dev/null +++ b/terraform/test-fixtures/plan-computed-in-function/main.tf @@ -0,0 +1,7 @@ +data "aws_data_source" "foo" { + +} + +resource "aws_instance" "bar" { + attr = "${length(data.aws_data_source.foo.computed)}" +} diff --git a/terraform/test-fixtures/validate-computed-in-function/main.tf b/terraform/test-fixtures/validate-computed-in-function/main.tf new file mode 100644 index 0000000000..504e194261 --- /dev/null +++ b/terraform/test-fixtures/validate-computed-in-function/main.tf @@ -0,0 +1,7 @@ +data "aws_data_source" "foo" { + optional_attr = "value" +} + +resource "aws_instance" "bar" { + attr = "${length(data.aws_data_source.foo.computed)}" +}