Skip to content

Commit 8651cac

Browse files
authored
bundle/internal/tf/codegen: apply go/format in generator, add Taskfil… (#5415)
## Changes - Update tf/codegen to generate formatted files right away. - Fix generation mismatch #5392 re resource_all.go vs resource_schema.go - Update Taskfile.yml with this task.
1 parent 2b33efe commit 8651cac

5 files changed

Lines changed: 197 additions & 186 deletions

File tree

Taskfile.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,13 +707,17 @@ tasks:
707707
# can be invoked standalone.
708708

709709
generate:
710-
desc: Run all generators (genkit, refschema, schema, docs, validation, direct, pydabs)
710+
desc: Run all generators (genkit, tf-schema, refschema, schema, docs, validation, direct, pydabs)
711711
cmds:
712712
# Runs first: regenerates CLI command stubs from the OpenAPI spec at
713713
# .codegen/_openapi_sha. SDK version bumps (go.mod/go.sum) are a manual
714714
# step outside this task; TestConsistentDatabricksSdkVersion (run inside
715715
# generate-genkit) asserts the two stay in sync.
716716
- task: generate-genkit
717+
# Requires Terraform provider access (set TF_CLI_CONFIG_FILE to a
718+
# filesystem mirror, or rely on the public registry). Only re-runs when
719+
# bundle/internal/tf/codegen/schema/version.go changes.
720+
- task: generate-tf-schema
717721
# Refreshes acceptance/bundle/refschema/out.fields.txt, which feeds
718722
# generate-direct-apitypes and generate-direct-resources below.
719723
- task: generate-refschema
@@ -868,6 +872,19 @@ tasks:
868872
cmds:
869873
- go test ./bundle/terraform_dabs_map -run TestGenerateSchemaMap -update -v
870874

875+
generate-tf-schema:
876+
desc: Regenerate bundle/internal/tf/schema from the Databricks Terraform provider
877+
dir: bundle/internal/tf/codegen
878+
sources:
879+
- schema/version.go
880+
- templates/**
881+
- "**/*.go"
882+
- exclude: "**/*_test.go"
883+
generates:
884+
- ../schema/*.go
885+
cmds:
886+
- go run .
887+
871888
generate-direct:
872889
desc: Generate direct engine config (apitypes + resources)
873890
deps: ['generate-direct-apitypes', 'generate-direct-resources']

bundle/internal/tf/codegen/generator/generator.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
package generator
33

44
import (
5+
"bytes"
56
"context"
67
"fmt"
8+
"go/format"
79
"log"
810
"maps"
911
"os"
@@ -16,6 +18,19 @@ import (
1618
tfjson "github.com/hashicorp/terraform-json"
1719
)
1820

21+
// generateFormatted executes tmpl with data, formats the output with gofmt, and writes it to path.
22+
func generateFormatted(path string, tmpl *template.Template, data any) error {
23+
var buf bytes.Buffer
24+
if err := tmpl.Execute(&buf, data); err != nil {
25+
return err
26+
}
27+
src, err := format.Source(buf.Bytes())
28+
if err != nil {
29+
return fmt.Errorf("formatting %s: %w", path, err)
30+
}
31+
return os.WriteFile(path, src, 0o644)
32+
}
33+
1934
func normalizeName(name string) string {
2035
return strings.TrimPrefix(name, "databricks_")
2136
}
@@ -27,14 +42,7 @@ type collection struct {
2742

2843
func (c *collection) Generate(path string) error {
2944
tmpl := template.Must(template.ParseFiles(fmt.Sprintf("./templates/%s.tmpl", c.OutputFile)))
30-
f, err := os.Create(filepath.Join(path, c.OutputFile))
31-
if err != nil {
32-
return err
33-
}
34-
35-
defer func() { _ = f.Close() }()
36-
37-
return tmpl.Execute(f, c)
45+
return generateFormatted(filepath.Join(path, c.OutputFile), tmpl, c)
3846
}
3947

4048
type root struct {
@@ -46,14 +54,7 @@ type root struct {
4654

4755
func (r *root) Generate(path string) error {
4856
tmpl := template.Must(template.ParseFiles(fmt.Sprintf("./templates/%s.tmpl", r.OutputFile)))
49-
f, err := os.Create(filepath.Join(path, r.OutputFile))
50-
if err != nil {
51-
return err
52-
}
53-
54-
defer func() { _ = f.Close() }()
55-
56-
return tmpl.Execute(f, r)
57+
return generateFormatted(filepath.Join(path, r.OutputFile), tmpl, r)
5758
}
5859

5960
// Run generates Go type files under path for every resource and data source in schema.
@@ -138,10 +139,10 @@ func Run(_ context.Context, schema *tfjson.ProviderSchema, checksums *schemapkg.
138139
}
139140
}
140141

141-
// Generate resource_schemas.go
142+
// Generate resource_all.go
142143
{
143144
cr := &collection{
144-
OutputFile: "resource_schemas.go",
145+
OutputFile: "resource_all.go",
145146
Blocks: resources,
146147
}
147148
err := cr.Generate(path)

bundle/internal/tf/codegen/generator/named_block.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package generator
22

33
import (
44
"fmt"
5-
"os"
65
"path/filepath"
76
"strings"
87
"text/template"
@@ -48,13 +47,7 @@ func (b *namedBlock) Generate(path string) error {
4847
return err
4948
}
5049

51-
f, err := os.Create(filepath.Join(path, fmt.Sprintf(b.filePattern, b.normalizedName())))
52-
if err != nil {
53-
return err
54-
}
55-
56-
defer func() { _ = f.Close() }()
57-
5850
tmpl := template.Must(template.ParseFiles("./templates/block.go.tmpl"))
59-
return tmpl.Execute(f, w)
51+
outPath := filepath.Join(path, fmt.Sprintf(b.filePattern, b.normalizedName()))
52+
return generateFormatted(outPath, tmpl, w)
6053
}

bundle/internal/tf/codegen/templates/resource_schemas.go.tmpl renamed to bundle/internal/tf/codegen/templates/resource_all.go.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
package schema
44

5-
// ResourceSchemas provides typed access to resource struct types via reflection.
5+
// AllResources provides typed access to resource struct types via reflection.
66
// Each field corresponds to a Terraform resource type; the field type is the
77
// generated Go struct (e.g., ResourceJob) rather than map[string]any.
88
// Use this for field enumeration without a manual type registry.
9-
type ResourceSchemas struct {
9+
type AllResources struct {
1010
{{- range .Blocks }}
1111
{{ .FieldName }} {{ .TypeName }} `json:"{{ .TerraformName }},omitempty"`
1212
{{- end }}

0 commit comments

Comments
 (0)