Skip to content

Commit

Permalink
Returning error if resource not found
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Jun 9, 2022
1 parent 27bf553 commit b0001a4
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 38 deletions.
54 changes: 28 additions & 26 deletions internal/provider/generate.go
Expand Up @@ -414,35 +414,37 @@ func (g *generator) renderStaticWebsite(providerName string, providerSchema *tfj
g.infof("rendering %q", rel)
switch relDir {
case "data-sources/":
resName := resourceName(shortName, relFile)
resSchema, ok := providerSchema.DataSourceSchemas[resName]
if ok {
tmpl := resourceTemplate(tmplData)
render, err := tmpl.Render(resName, providerName, "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
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, "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
case "resources/":
resName := resourceName(shortName, relFile)
resSchema, ok := providerSchema.ResourceSchemas[resName]
if ok {
tmpl := resourceTemplate(tmplData)
render, err := tmpl.Render(resName, providerName, "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
resSchema, resName := resourceSchema(providerSchema.ResourceSchemas, 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, "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
case "": // provider
if relFile == "index.md.tmpl" {
tmpl := providerTemplate(tmplData)
Expand Down
23 changes: 15 additions & 8 deletions internal/provider/util.go
Expand Up @@ -9,6 +9,8 @@ import (
"os/exec"
"path/filepath"
"strings"

tfjson "github.com/hashicorp/terraform-json"
)

func providerShortName(n string) string {
Expand Down Expand Up @@ -52,16 +54,21 @@ func removeAllExt(file string) string {
}
}

// resourceName determines whether the shortname and the relFile
// are identical after file extensions have been stripped from the
// latter. This allows single word resources (e.g., http) to use
// templates (e.g., http.md.tmpl).
func resourceName(shortName, relFile string) string {
if shortName == removeAllExt(relFile) {
return shortName
// resourceSchema determines whether there is a schema in the supplied schemas map which
// has either the providerShortName or the providerShortName concatenated with the
// templateFileName (stripped of file extension.
func resourceSchema(schemas map[string]*tfjson.Schema, providerShortName, templateFileName string) (*tfjson.Schema, string) {
if schema, ok := schemas[providerShortName]; ok {
return schema, providerShortName
}

resName := providerShortName + "_" + removeAllExt(templateFileName)

if schema, ok := schemas[resName]; ok {
return schema, resName
}

return shortName + "_" + removeAllExt(relFile)
return nil, ""
}

func writeFile(path string, data string) error {
Expand Down
54 changes: 50 additions & 4 deletions internal/provider/util_test.go
Expand Up @@ -4,32 +4,78 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
tfjson "github.com/hashicorp/terraform-json"
)

func Test_resourceName(t *testing.T) {
func Test_resourceSchema(t *testing.T) {
cases := []struct {
name string
schemas map[string]*tfjson.Schema
providerShortName string
templateFileName string
expectedSchema *tfjson.Schema
expectedResourceName string
}{
{
"provider short name same as template file name",
"provider short name matches schema name",
map[string]*tfjson.Schema{
"http": {},
},
"http",
"http.md.tmpl",
&tfjson.Schema{},
"http",
},
{
"provider short name different to template file name",
"provider short name does not match schema name",
map[string]*tfjson.Schema{
"http": {},
},
"tls",
"http.md.tmpl",
nil,
"",
},
{
"provider short name concatenated with template file name matches schema name",
map[string]*tfjson.Schema{
"tls_cert_request": {},
},
"tls",
"cert_request.md.tmpl",
&tfjson.Schema{},
"tls_cert_request",
},
{
"provider short name concatenated with template file name does not match schema name",
map[string]*tfjson.Schema{
"tls_cert_request": {},
},
"tls",
"not_found.md.tmpl",
nil,
"",
},
{
"provider short name concatenated with same template file name matches schema name",
map[string]*tfjson.Schema{
"tls_tls": {},
},
"tls",
"tls.md.tmpl",
&tfjson.Schema{},
"tls_tls",
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actualResourceName := resourceName(c.providerShortName, c.templateFileName)
actualSchema, actualResourceName := resourceSchema(c.schemas, c.providerShortName, c.templateFileName)

if !cmp.Equal(c.expectedSchema, actualSchema) {
t.Errorf("expected: %+v, got: %+v", c.expectedSchema, actualSchema)
}

if !cmp.Equal(c.expectedResourceName, actualResourceName) {
t.Errorf("expected: %s, got: %s", c.expectedResourceName, actualResourceName)
}
Expand Down

0 comments on commit b0001a4

Please sign in to comment.