Skip to content

Commit b0001a4

Browse files
committed
Returning error if resource not found
1 parent 27bf553 commit b0001a4

File tree

3 files changed

+93
-38
lines changed

3 files changed

+93
-38
lines changed

internal/provider/generate.go

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -414,35 +414,37 @@ func (g *generator) renderStaticWebsite(providerName string, providerSchema *tfj
414414
g.infof("rendering %q", rel)
415415
switch relDir {
416416
case "data-sources/":
417-
resName := resourceName(shortName, relFile)
418-
resSchema, ok := providerSchema.DataSourceSchemas[resName]
419-
if ok {
420-
tmpl := resourceTemplate(tmplData)
421-
render, err := tmpl.Render(resName, providerName, "Data Source", "", "", resSchema)
422-
if err != nil {
423-
return fmt.Errorf("unable to render data source template %q: %w", rel, err)
424-
}
425-
_, err = out.WriteString(render)
426-
if err != nil {
427-
return fmt.Errorf("unable to write rendered string: %w", err)
428-
}
429-
return nil
417+
resSchema, resName := resourceSchema(providerSchema.DataSourceSchemas, shortName, relFile)
418+
if resSchema == nil {
419+
return fmt.Errorf("unable to find resource for provider (%s) and template (%s)", shortName, relFile)
420+
}
421+
422+
tmpl := resourceTemplate(tmplData)
423+
render, err := tmpl.Render(resName, providerName, "Data Source", "", "", resSchema)
424+
if err != nil {
425+
return fmt.Errorf("unable to render data source template %q: %w", rel, err)
430426
}
427+
_, err = out.WriteString(render)
428+
if err != nil {
429+
return fmt.Errorf("unable to write rendered string: %w", err)
430+
}
431+
return nil
431432
case "resources/":
432-
resName := resourceName(shortName, relFile)
433-
resSchema, ok := providerSchema.ResourceSchemas[resName]
434-
if ok {
435-
tmpl := resourceTemplate(tmplData)
436-
render, err := tmpl.Render(resName, providerName, "Resource", "", "", resSchema)
437-
if err != nil {
438-
return fmt.Errorf("unable to render resource template %q: %w", rel, err)
439-
}
440-
_, err = out.WriteString(render)
441-
if err != nil {
442-
return fmt.Errorf("unable to write regindered string: %w", err)
443-
}
444-
return nil
433+
resSchema, resName := resourceSchema(providerSchema.ResourceSchemas, shortName, relFile)
434+
if resSchema == nil {
435+
return fmt.Errorf("unable to find resource for provider (%s) and template (%s)", shortName, relFile)
436+
}
437+
438+
tmpl := resourceTemplate(tmplData)
439+
render, err := tmpl.Render(resName, providerName, "Resource", "", "", resSchema)
440+
if err != nil {
441+
return fmt.Errorf("unable to render resource template %q: %w", rel, err)
445442
}
443+
_, err = out.WriteString(render)
444+
if err != nil {
445+
return fmt.Errorf("unable to write regindered string: %w", err)
446+
}
447+
return nil
446448
case "": // provider
447449
if relFile == "index.md.tmpl" {
448450
tmpl := providerTemplate(tmplData)

internal/provider/util.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"os/exec"
1010
"path/filepath"
1111
"strings"
12+
13+
tfjson "github.com/hashicorp/terraform-json"
1214
)
1315

1416
func providerShortName(n string) string {
@@ -52,16 +54,21 @@ func removeAllExt(file string) string {
5254
}
5355
}
5456

55-
// resourceName determines whether the shortname and the relFile
56-
// are identical after file extensions have been stripped from the
57-
// latter. This allows single word resources (e.g., http) to use
58-
// templates (e.g., http.md.tmpl).
59-
func resourceName(shortName, relFile string) string {
60-
if shortName == removeAllExt(relFile) {
61-
return shortName
57+
// resourceSchema determines whether there is a schema in the supplied schemas map which
58+
// has either the providerShortName or the providerShortName concatenated with the
59+
// templateFileName (stripped of file extension.
60+
func resourceSchema(schemas map[string]*tfjson.Schema, providerShortName, templateFileName string) (*tfjson.Schema, string) {
61+
if schema, ok := schemas[providerShortName]; ok {
62+
return schema, providerShortName
63+
}
64+
65+
resName := providerShortName + "_" + removeAllExt(templateFileName)
66+
67+
if schema, ok := schemas[resName]; ok {
68+
return schema, resName
6269
}
6370

64-
return shortName + "_" + removeAllExt(relFile)
71+
return nil, ""
6572
}
6673

6774
func writeFile(path string, data string) error {

internal/provider/util_test.go

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,78 @@ import (
44
"testing"
55

66
"github.com/google/go-cmp/cmp"
7+
tfjson "github.com/hashicorp/terraform-json"
78
)
89

9-
func Test_resourceName(t *testing.T) {
10+
func Test_resourceSchema(t *testing.T) {
1011
cases := []struct {
1112
name string
13+
schemas map[string]*tfjson.Schema
1214
providerShortName string
1315
templateFileName string
16+
expectedSchema *tfjson.Schema
1417
expectedResourceName string
1518
}{
1619
{
17-
"provider short name same as template file name",
20+
"provider short name matches schema name",
21+
map[string]*tfjson.Schema{
22+
"http": {},
23+
},
1824
"http",
1925
"http.md.tmpl",
26+
&tfjson.Schema{},
2027
"http",
2128
},
2229
{
23-
"provider short name different to template file name",
30+
"provider short name does not match schema name",
31+
map[string]*tfjson.Schema{
32+
"http": {},
33+
},
34+
"tls",
35+
"http.md.tmpl",
36+
nil,
37+
"",
38+
},
39+
{
40+
"provider short name concatenated with template file name matches schema name",
41+
map[string]*tfjson.Schema{
42+
"tls_cert_request": {},
43+
},
2444
"tls",
2545
"cert_request.md.tmpl",
46+
&tfjson.Schema{},
2647
"tls_cert_request",
2748
},
49+
{
50+
"provider short name concatenated with template file name does not match schema name",
51+
map[string]*tfjson.Schema{
52+
"tls_cert_request": {},
53+
},
54+
"tls",
55+
"not_found.md.tmpl",
56+
nil,
57+
"",
58+
},
59+
{
60+
"provider short name concatenated with same template file name matches schema name",
61+
map[string]*tfjson.Schema{
62+
"tls_tls": {},
63+
},
64+
"tls",
65+
"tls.md.tmpl",
66+
&tfjson.Schema{},
67+
"tls_tls",
68+
},
2869
}
2970

3071
for _, c := range cases {
3172
t.Run(c.name, func(t *testing.T) {
32-
actualResourceName := resourceName(c.providerShortName, c.templateFileName)
73+
actualSchema, actualResourceName := resourceSchema(c.schemas, c.providerShortName, c.templateFileName)
74+
75+
if !cmp.Equal(c.expectedSchema, actualSchema) {
76+
t.Errorf("expected: %+v, got: %+v", c.expectedSchema, actualSchema)
77+
}
78+
3379
if !cmp.Equal(c.expectedResourceName, actualResourceName) {
3480
t.Errorf("expected: %s, got: %s", c.expectedResourceName, actualResourceName)
3581
}

0 commit comments

Comments
 (0)