diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 4c585b73c..cec90d7ca 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -660,19 +660,15 @@ func TestContext2Apply_providerAliasConfigure(t *testing.T) { // Configure to record calls AFTER Plan above var configCount int32 - p2.ConfigureFn = func(c *ResourceConfig) error { + p2.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { atomic.AddInt32(&configCount, 1) - foo, ok := c.Get("foo") - if !ok { - return fmt.Errorf("foo is not found") - } - + foo := req.Config.GetAttr("foo").AsString() if foo != "bar" { - return fmt.Errorf("foo: %#v", foo) + resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("foo: %#v", foo)) } - return nil + return } state, diags := ctx.Apply() @@ -2477,16 +2473,18 @@ func TestContext2Apply_moduleInheritAlias(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn - p.ConfigureFn = func(c *ResourceConfig) error { - if _, ok := c.Get("value"); !ok { - return nil + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + val := req.Config.GetAttr("value") + if val.IsNull() { + return } - if _, ok := c.Get("root"); ok { - return fmt.Errorf("child should not get root") + root := req.Config.GetAttr("root") + if !root.IsNull() { + resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("child should not get root")) } - return nil + return } ctx := testContext2(t, &ContextOpts{ @@ -2601,19 +2599,18 @@ func TestContext2Apply_moduleOrphanInheritAlias(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn - called := false - p.ConfigureFn = func(c *ResourceConfig) error { - called = true - - if _, ok := c.Get("child"); !ok { - return nil + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + val := req.Config.GetAttr("value") + if val.IsNull() { + return } - if _, ok := c.Get("root"); ok { - return fmt.Errorf("child should not get root") + root := req.Config.GetAttr("root") + if !root.IsNull() { + resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("child should not get root")) } - return nil + return } // Create a state with an orphan module @@ -2645,7 +2642,7 @@ func TestContext2Apply_moduleOrphanInheritAlias(t *testing.T) { t.Fatalf("diags: %s", diags.Err()) } - if !called { + if !p.ConfigureCalled { t.Fatal("must call configure") } @@ -2658,12 +2655,13 @@ func TestContext2Apply_moduleOrphanProvider(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn - p.ConfigureFn = func(c *ResourceConfig) error { - if _, ok := c.Get("value"); !ok { - return fmt.Errorf("value is not found") + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + val := req.Config.GetAttr("value") + if val.IsNull() { + resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("value is not found")) } - return nil + return } // Create a state with an orphan module @@ -2701,12 +2699,13 @@ func TestContext2Apply_moduleOrphanGrandchildProvider(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn - p.ConfigureFn = func(c *ResourceConfig) error { - if _, ok := c.Get("value"); !ok { - return fmt.Errorf("value is not found") + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + val := req.Config.GetAttr("value") + if val.IsNull() { + resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("value is not found")) } - return nil + return } // Create a state with an orphan module that is nested (grandchild) @@ -2746,15 +2745,17 @@ func TestContext2Apply_moduleGrandchildProvider(t *testing.T) { var callLock sync.Mutex called := false - p.ConfigureFn = func(c *ResourceConfig) error { - if _, ok := c.Get("value"); !ok { - return fmt.Errorf("value is not found") + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + val := req.Config.GetAttr("value") + if val.IsNull() { + resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("value is not found")) } + callLock.Lock() called = true callLock.Unlock() - return nil + return } ctx := testContext2(t, &ContextOpts{ @@ -3985,20 +3986,13 @@ func TestContext2Apply_providerComputedVar(t *testing.T) { }, }) - p.ConfigureFn = func(c *ResourceConfig) error { - if c.IsComputed("value") { - return fmt.Errorf("value is computed") + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + val := req.Config.GetAttr("value") + if val.IsNull() { + resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("value is not found")) + return } - - v, ok := c.Get("value") - if !ok { - return fmt.Errorf("value is not found") - } - if v != "yes" { - return fmt.Errorf("value is not 'yes': %v", v) - } - - return nil + return } if _, diags := ctx.Plan(); diags.HasErrors() { @@ -4016,15 +4010,13 @@ func TestContext2Apply_providerConfigureDisabled(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn - called := false - p.ConfigureFn = func(c *ResourceConfig) error { - called = true - - if _, ok := c.Get("value"); !ok { - return fmt.Errorf("value is not found") + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + val := req.Config.GetAttr("value") + if val.IsNull() { + resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("value is not found")) } - return nil + return } ctx := testContext2(t, &ContextOpts{ @@ -4042,7 +4034,7 @@ func TestContext2Apply_providerConfigureDisabled(t *testing.T) { t.Fatalf("apply errors: %s", diags.Err()) } - if !called { + if !p.ConfigureCalled { t.Fatal("configure never called") } } @@ -8834,12 +8826,15 @@ func TestContext2Apply_providerWithLocals(t *testing.T) { providerRegion := "" // this should not be overridden during destroy - p.ConfigureFn = func(c *ResourceConfig) error { - if r, ok := c.Get("region"); ok { - providerRegion = r.(string) + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + val := req.Config.GetAttr("region") + if !val.IsNull() { + providerRegion = val.AsString() } - return nil + + return } + p.DiffFn = testDiffFn p.ApplyFn = testApplyFn ctx := testContext2(t, &ContextOpts{ @@ -11385,7 +11380,7 @@ output "output" { testP.GetSchemaReturn = schemaFn("test") providerConfig := "" - testP.ConfigureNewFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + testP.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { value := req.Config.GetAttr("value") if value.IsKnown() && !value.IsNull() { providerConfig = value.AsString() diff --git a/terraform/context_import_test.go b/terraform/context_import_test.go index 31ab41ce4..01b9c2de2 100644 --- a/terraform/context_import_test.go +++ b/terraform/context_import_test.go @@ -1,7 +1,7 @@ package terraform import ( - "fmt" + "errors" "strings" "testing" @@ -196,15 +196,13 @@ func TestContextImport_moduleProvider(t *testing.T) { }, } - configured := false - p.ConfigureFn = func(c *ResourceConfig) error { - configured = true - - if v, ok := c.Get("foo"); !ok || v.(string) != "bar" { - return fmt.Errorf("bad") + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + foo := req.Config.GetAttr("foo").AsString() + if foo != "bar" { + resp.Diagnostics = resp.Diagnostics.Append(errors.New("not bar")) } - return nil + return } m := testModule(t, "import-provider") @@ -229,7 +227,7 @@ func TestContextImport_moduleProvider(t *testing.T) { t.Fatalf("unexpected errors: %s", diags.Err()) } - if !configured { + if !p.ConfigureCalled { t.Fatal("didn't configure provider") } @@ -258,15 +256,13 @@ func TestContextImport_providerModule(t *testing.T) { }, } - configured := false - p.ConfigureFn = func(c *ResourceConfig) error { - configured = true - - if v, ok := c.Get("foo"); !ok || v.(string) != "bar" { - return fmt.Errorf("bad") + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + foo := req.Config.GetAttr("foo").AsString() + if foo != "bar" { + resp.Diagnostics = resp.Diagnostics.Append(errors.New("not bar")) } - return nil + return } _, diags := ctx.Import(&ImportOpts{ @@ -283,7 +279,7 @@ func TestContextImport_providerModule(t *testing.T) { t.Fatalf("unexpected errors: %s", diags.Err()) } - if !configured { + if !p.ConfigureCalled { t.Fatal("didn't configure provider") } } diff --git a/terraform/context_input_test.go b/terraform/context_input_test.go index cb63b6906..2e1dd269f 100644 --- a/terraform/context_input_test.go +++ b/terraform/context_input_test.go @@ -56,9 +56,9 @@ func TestContext2Input_provider(t *testing.T) { }) var actual interface{} - p.ConfigureFn = func(c *ResourceConfig) error { - actual = c.Config["foo"] - return nil + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + actual = req.Config.GetAttr("foo").AsString() + return } p.ValidateFn = func(c *ResourceConfig) ([]string, []error) { return nil, c.CheckSet([]string{"foo"}) @@ -145,11 +145,11 @@ func TestContext2Input_providerMulti(t *testing.T) { t.Fatalf("plan errors: %s", diags.Err()) } - p.ConfigureFn = func(c *ResourceConfig) error { + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { lock.Lock() defer lock.Unlock() - actual = append(actual, c.Config["foo"]) - return nil + actual = append(actual, req.Config.GetAttr("foo").AsString()) + return } if _, diags := ctx.Apply(); diags.HasErrors() { t.Fatalf("apply errors: %s", diags.Err()) @@ -217,9 +217,9 @@ func TestContext2Input_providerId(t *testing.T) { }) var actual interface{} - p.ConfigureFn = func(c *ResourceConfig) error { - actual = c.Config["foo"] - return nil + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + actual = req.Config.GetAttr("foo").AsString() + return } input.InputReturnMap = map[string]string{ @@ -290,9 +290,9 @@ func TestContext2Input_providerOnly(t *testing.T) { } var actual interface{} - p.ConfigureFn = func(c *ResourceConfig) error { - actual = c.Config["foo"] - return nil + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + actual = req.Config.GetAttr("foo").AsString() + return } if err := ctx.Input(InputModeProvider); err != nil { @@ -344,15 +344,10 @@ func TestContext2Input_providerVars(t *testing.T) { } var actual interface{} - /*p.InputFn = func(i UIInput, c *ResourceConfig) (*ResourceConfig, error) { - c.Config["bar"] = "baz" - return c, nil - }*/ - p.ConfigureFn = func(c *ResourceConfig) error { - actual, _ = c.Get("foo") - return nil + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + actual = req.Config.GetAttr("foo").AsString() + return } - if diags := ctx.Input(InputModeStd); diags.HasErrors() { t.Fatalf("input errors: %s", diags.Err()) } @@ -384,16 +379,6 @@ func TestContext2Input_providerVarsModuleInherit(t *testing.T) { UIInput: input, }) - /*p.InputFn = func(i UIInput, c *ResourceConfig) (*ResourceConfig, error) { - if errs := c.CheckSet([]string{"access_key"}); len(errs) > 0 { - return c, errs[0] - } - return c, nil - }*/ - p.ConfigureFn = func(c *ResourceConfig) error { - return nil - } - if diags := ctx.Input(InputModeStd); diags.HasErrors() { t.Fatalf("input errors: %s", diags.Err()) } @@ -414,13 +399,6 @@ func TestContext2Input_submoduleTriggersInvalidCount(t *testing.T) { UIInput: input, }) - /*p.InputFn = func(i UIInput, c *ResourceConfig) (*ResourceConfig, error) { - return c, nil - }*/ - p.ConfigureFn = func(c *ResourceConfig) error { - return nil - } - if diags := ctx.Input(InputModeStd); diags.HasErrors() { t.Fatalf("input errors: %s", diags.Err()) } diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index a1c6258df..75cdf5a8c 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -1016,12 +1016,13 @@ func TestContext2Plan_moduleProviderInherit(t *testing.T) { }, }, } - p.ConfigureFn = func(c *ResourceConfig) error { - if v, ok := c.Get("from"); !ok || v.(string) != "root" { - return fmt.Errorf("bad") + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + from := req.Config.GetAttr("from") + if from.IsNull() || from.AsString() != "root" { + resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("not root")) } - return nil + return } p.DiffFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) { from := req.Config.GetAttr("from").AsString() @@ -1078,14 +1079,14 @@ func TestContext2Plan_moduleProviderInheritDeep(t *testing.T) { }, } - p.ConfigureFn = func(c *ResourceConfig) error { - v, ok := c.Get("from") - if !ok || v.(string) != "root" { - return fmt.Errorf("bad") + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + v := req.Config.GetAttr("from") + if v.IsNull() || v.AsString() != "root" { + resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("not root")) } + from = v.AsString() - from = v.(string) - return nil + return } p.DiffFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) { @@ -1135,20 +1136,23 @@ func TestContext2Plan_moduleProviderDefaultsVar(t *testing.T) { }, }, } - p.ConfigureFn = func(c *ResourceConfig) error { + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { var buf bytes.Buffer - if v, ok := c.Get("from"); ok { - buf.WriteString(v.(string) + "\n") + from := req.Config.GetAttr("from") + if !from.IsNull() { + buf.WriteString(from.AsString() + "\n") } - if v, ok := c.Get("to"); ok { - buf.WriteString(v.(string) + "\n") + to := req.Config.GetAttr("to") + if !to.IsNull() { + buf.WriteString(to.AsString() + "\n") } l.Lock() defer l.Unlock() calls = append(calls, buf.String()) - return nil + return } + p.DiffFn = testDiffFn return p, nil }, @@ -4434,9 +4438,9 @@ func TestContext2Plan_provider(t *testing.T) { p.DiffFn = testDiffFn var value interface{} - p.ConfigureFn = func(c *ResourceConfig) error { - value, _ = c.Get("foo") - return nil + p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { + value = req.Config.GetAttr("foo").AsString() + return } ctx := testContext2(t, &ContextOpts{ diff --git a/terraform/context_validate_test.go b/terraform/context_validate_test.go index 75415f58b..b88e10d11 100644 --- a/terraform/context_validate_test.go +++ b/terraform/context_validate_test.go @@ -173,14 +173,13 @@ func TestContext2Validate_computedVar(t *testing.T) { return nil, c.CheckSet([]string{"value"}) } - p.ConfigureFn = func(c *ResourceConfig) error { - return fmt.Errorf("Configure should not be called for provider") - } - diags := c.Validate() if diags.HasErrors() { t.Fatalf("unexpected error: %s", diags.Err()) } + if p.ConfigureCalled { + t.Fatal("Configure should not be called for provider") + } } func TestContext2Validate_computedInFunction(t *testing.T) { diff --git a/terraform/provider_mock.go b/terraform/provider_mock.go index f30a4adf8..c069ebbe9 100644 --- a/terraform/provider_mock.go +++ b/terraform/provider_mock.go @@ -51,7 +51,7 @@ type MockProvider struct { ConfigureCalled bool ConfigureResponse providers.ConfigureResponse ConfigureRequest providers.ConfigureRequest - ConfigureNewFn func(providers.ConfigureRequest) providers.ConfigureResponse // Named ConfigureNewFn so we can still have the legacy ConfigureFn declared below + ConfigureFn func(providers.ConfigureRequest) providers.ConfigureResponse StopCalled bool StopFn func() error @@ -88,10 +88,8 @@ type MockProvider struct { CloseCalled bool CloseError error - ValidateFn func(c *ResourceConfig) (ws []string, es []error) - ConfigureFn func(c *ResourceConfig) error + ValidateFn func(c *ResourceConfig) (ws []string, es []error) //ValidateFn func(providers.ValidateResourceTypeConfigRequest) providers.ValidateResourceTypeConfigResponse - //ConfigureFn func(providers.ConfigureRequest) providers.ConfigureResponse DiffFn func(providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse ApplyFn func(providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse } @@ -232,19 +230,7 @@ func (p *MockProvider) Configure(r providers.ConfigureRequest) providers.Configu p.ConfigureRequest = r if p.ConfigureFn != nil { - resp := p.getSchema() - schema := resp.Provider.Block - rc := NewResourceConfigShimmed(r.Config, schema) - ret := providers.ConfigureResponse{} - - err := p.ConfigureFn(rc) - if err != nil { - ret.Diagnostics = ret.Diagnostics.Append(err) - } - return ret - } - if p.ConfigureNewFn != nil { - return p.ConfigureNewFn(r) + return p.ConfigureFn(r) } return p.ConfigureResponse diff --git a/terraform/testdata/apply-provider-computed/main.tf b/terraform/testdata/apply-provider-computed/main.tf index 4a39bb546..81acf7cfa 100644 --- a/terraform/testdata/apply-provider-computed/main.tf +++ b/terraform/testdata/apply-provider-computed/main.tf @@ -1,5 +1,5 @@ provider "aws" { - value = "${test_instance.foo.value}" + value = test_instance.foo.id } resource "aws_instance" "bar" {}