diff --git a/terraform/context.go b/terraform/context.go index c7cb86bfb..0888d52b8 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -451,7 +451,31 @@ func (c *Context) validateWalkFn(rws *[]string, res *[]error) depgraph.WalkFunc switch rn := n.Meta.(type) { case *GraphNodeResource: + if rn.Resource == nil { + panic("resource should never be nil") + } + + // If it doesn't have a provider, that is a different problem + if rn.Resource.Provider == nil { + return nil + } + + ws, es := rn.Resource.Provider.ValidateResource( + rn.Type, rn.Resource.Config) + for i, w := range ws { + ws[i] = fmt.Sprintf("'%s' warning: %s", rn.Resource.Id, w) + } + for i, e := range es { + es[i] = fmt.Errorf("'%s' error: %s", rn.Resource.Id, e) + } + + *rws = append(*rws, ws...) + *res = append(*res, es...) case *GraphNodeResourceProvider: + if rn.Config == nil { + return nil + } + rc := NewResourceConfig(rn.Config.RawConfig) for k, p := range rn.Providers { diff --git a/terraform/context_test.go b/terraform/context_test.go index eec78017c..8368df95f 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -77,6 +77,27 @@ func TestContextValidate_providerConfig_good(t *testing.T) { } } +func TestContextValidate_resourceConfig_bad(t *testing.T) { + config := testConfig(t, "validate-bad-rc") + p := testProvider("aws") + c := testContext(t, &ContextOpts{ + Config: config, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + p.ValidateResourceReturnErrors = []error{fmt.Errorf("bad")} + + w, e := c.Validate() + if len(w) > 0 { + t.Fatalf("bad: %#v", w) + } + if len(e) == 0 { + t.Fatalf("bad: %#v", e) + } +} + func TestContextValidate_requiredVar(t *testing.T) { config := testConfig(t, "validate-required-var") c := testContext(t, &ContextOpts{ diff --git a/terraform/test-fixtures/validate-bad-rc/main.tf b/terraform/test-fixtures/validate-bad-rc/main.tf new file mode 100644 index 000000000..292a1443e --- /dev/null +++ b/terraform/test-fixtures/validate-bad-rc/main.tf @@ -0,0 +1,3 @@ +resource "aws_instance" "test" { + foo = "bar" +}