From 70c0afcd39fd8ce06bc0e4bfb87c5fd356ab8a98 Mon Sep 17 00:00:00 2001 From: Benjamin Bennett Date: Tue, 14 Jun 2022 14:40:51 +0100 Subject: [PATCH] Do not error if data source or resource not found in schemas (#151) * Do not error if data source or resource not found in schemas * Log message when schema not found * Fixing tests * Update internal/provider/generate.go Co-authored-by: Ivan De Marino * Update internal/provider/generate.go Co-authored-by: Ivan De Marino * Updating CHANGELOG.md Co-authored-by: Ivan De Marino --- CHANGELOG.md | 6 ++++ internal/provider/generate.go | 52 ++++++++++++++++------------------ internal/provider/util.go | 2 +- internal/provider/util_test.go | 4 +-- 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e86d3ebc..73b1e5b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.10.1 (June 14, 2022) + +BUG FIXES: + +* cmd/tfplugindocs: Do not error when schema not found, issue log warning ([#151](https://github.com/hashicorp/terraform-plugin-docs/pull/151)). + # 0.10.0 (June 13, 2022) BUG FIXES: diff --git a/internal/provider/generate.go b/internal/provider/generate.go index f9473bae..f9fed5f8 100644 --- a/internal/provider/generate.go +++ b/internal/provider/generate.go @@ -415,38 +415,34 @@ func (g *generator) renderStaticWebsite(providerName string, providerSchema *tfj switch relDir { case "data-sources/": resSchema, resName := resourceSchema(providerSchema.DataSourceSchemas, shortName, relFile) - if resSchema == nil { - return fmt.Errorf("unable to find resource for provider (%s) and template (%s)", shortName, relFile) - } - - tmpl := resourceTemplate(tmplData) - render, err := tmpl.Render(resName, providerName, g.renderedProviderName, "Data Source", "", "", resSchema) - if err != nil { - return fmt.Errorf("unable to render data source template %q: %w", rel, err) - } - _, err = out.WriteString(render) - if err != nil { - return fmt.Errorf("unable to write rendered string: %w", err) + if resSchema != nil { + tmpl := resourceTemplate(tmplData) + render, err := tmpl.Render(resName, providerName, g.renderedProviderName, "Data Source", "", "", resSchema) + if err != nil { + return fmt.Errorf("unable to render data source template %q: %w", rel, err) + } + _, err = out.WriteString(render) + if err != nil { + return fmt.Errorf("unable to write rendered string: %w", err) + } + return nil } - return nil + g.warnf("data source entitled %q, or %q does not exist", shortName, resName) case "resources/": - // spew.Dump(providerSchema.ResourceSchemas, shortName) resSchema, resName := resourceSchema(providerSchema.ResourceSchemas, shortName, relFile) - // spew.Dump(resName, resSchema) - if resSchema == nil { - return fmt.Errorf("unable to find resource for provider (%s) and template (%s)", shortName, relFile) - } - - tmpl := resourceTemplate(tmplData) - render, err := tmpl.Render(resName, providerName, g.renderedProviderName, "Resource", "", "", resSchema) - if err != nil { - return fmt.Errorf("unable to render resource template %q: %w", rel, err) - } - _, err = out.WriteString(render) - if err != nil { - return fmt.Errorf("unable to write regindered string: %w", err) + if resSchema != nil { + tmpl := resourceTemplate(tmplData) + render, err := tmpl.Render(resName, providerName, g.renderedProviderName, "Resource", "", "", resSchema) + if err != nil { + return fmt.Errorf("unable to render resource template %q: %w", rel, err) + } + _, err = out.WriteString(render) + if err != nil { + return fmt.Errorf("unable to write regindered string: %w", err) + } + return nil } - return nil + g.warnf("resource entitled %q, or %q does not exist", shortName, resName) case "": // provider if relFile == "index.md.tmpl" { tmpl := providerTemplate(tmplData) diff --git a/internal/provider/util.go b/internal/provider/util.go index 6410bbdd..1de2ed46 100644 --- a/internal/provider/util.go +++ b/internal/provider/util.go @@ -68,7 +68,7 @@ func resourceSchema(schemas map[string]*tfjson.Schema, providerShortName, templa return schema, resName } - return nil, "" + return nil, resName } func writeFile(path string, data string) error { diff --git a/internal/provider/util_test.go b/internal/provider/util_test.go index 3e94549c..468c3231 100644 --- a/internal/provider/util_test.go +++ b/internal/provider/util_test.go @@ -31,7 +31,7 @@ func Test_resourceSchema(t *testing.T) { providerShortName: "tls", templateFileName: "http.md.tmpl", expectedSchema: nil, - expectedResourceName: "", + expectedResourceName: "tls_http", }, "provider short name concatenated with template file name matches schema name": { schemas: map[string]*tfjson.Schema{ @@ -49,7 +49,7 @@ func Test_resourceSchema(t *testing.T) { providerShortName: "tls", templateFileName: "not_found.md.tmpl", expectedSchema: nil, - expectedResourceName: "", + expectedResourceName: "tls_not_found", }, "provider short name concatenated with same template file name matches schema name": { schemas: map[string]*tfjson.Schema{