Skip to content

Commit

Permalink
Do not error if data source or resource not found in schemas (#151)
Browse files Browse the repository at this point in the history
* 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 <ivan.demarino@hashicorp.com>

* Update internal/provider/generate.go

Co-authored-by: Ivan De Marino <ivan.demarino@hashicorp.com>

* Updating CHANGELOG.md

Co-authored-by: Ivan De Marino <ivan.demarino@hashicorp.com>
  • Loading branch information
bendbennett and Ivan De Marino authored Jun 14, 2022
1 parent eaa28b7 commit 70c0afc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
52 changes: 24 additions & 28 deletions internal/provider/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down

0 comments on commit 70c0afc

Please sign in to comment.