From 2b200dd9bb4d5de379e22749cf5f2e2a559b3004 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Sat, 1 Jun 2019 11:40:54 -0400 Subject: [PATCH] re-validate data source config during read Just like resources, early data soruce validation will contain many unknown values. Re-validate once we have the config config. --- terraform/context_refresh_test.go | 40 +++++++++++++++++++++++++++++++ terraform/eval_read_data.go | 11 +++++++++ 2 files changed, 51 insertions(+) diff --git a/terraform/context_refresh_test.go b/terraform/context_refresh_test.go index 0f99abb95d..7f6670296c 100644 --- a/terraform/context_refresh_test.go +++ b/terraform/context_refresh_test.go @@ -1866,3 +1866,43 @@ test_thing.bar: } } } + +func TestContext2Refresh_dataValidation(t *testing.T) { + m := testModuleInline(t, map[string]string{ + "main.tf": ` +data "aws_data_source" "foo" { + foo = "bar" +} +`, + }) + + p := testProvider("aws") + p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) { + resp.PlannedState = req.ProposedNewState + return + } + p.ReadDataSourceFn = func(req providers.ReadDataSourceRequest) (resp providers.ReadDataSourceResponse) { + resp.State = req.Config + return + } + + ctx := testContext2(t, &ContextOpts{ + Config: m, + ProviderResolver: providers.ResolverFixed( + map[string]providers.Factory{ + "aws": testProviderFuncFixed(p), + }, + ), + }) + + _, diags := ctx.Refresh() + if diags.HasErrors() { + // Should get this error: + // Unsupported attribute: This object does not have an attribute named "missing" + t.Fatal(diags.Err()) + } + + if !p.ValidateDataSourceConfigCalled { + t.Fatal("ValidateDataSourceConfig not called during plan") + } +} diff --git a/terraform/eval_read_data.go b/terraform/eval_read_data.go index 34f2d60ade..728719a7a5 100644 --- a/terraform/eval_read_data.go +++ b/terraform/eval_read_data.go @@ -179,6 +179,17 @@ func (n *EvalReadData) Eval(ctx EvalContext) (interface{}, error) { ) } + log.Printf("[TRACE] Re-validating config for %s", absAddr) + validateResp := provider.ValidateDataSourceConfig( + providers.ValidateDataSourceConfigRequest{ + TypeName: n.Addr.Resource.Type, + Config: configVal, + }, + ) + if validateResp.Diagnostics.HasErrors() { + return nil, validateResp.Diagnostics.InConfigBody(n.Config.Config).Err() + } + // If we get down here then our configuration is complete and we're read // to actually call the provider to read the data. log.Printf("[TRACE] EvalReadData: %s configuration is complete, so reading from provider", absAddr)