Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -707,13 +707,17 @@ tasks:
# can be invoked standalone.

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

generate-tf-schema:
desc: Regenerate bundle/internal/tf/schema from the Databricks Terraform provider
dir: bundle/internal/tf/codegen
sources:
- schema/version.go
- templates/**
- "**/*.go"
- exclude: "**/*_test.go"
generates:
- ../schema/*.go
cmds:
- go run .

generate-direct:
desc: Generate direct engine config (apitypes + resources)
deps: ['generate-direct-apitypes', 'generate-direct-resources']
Expand Down
37 changes: 19 additions & 18 deletions bundle/internal/tf/codegen/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
package generator

import (
"bytes"
"context"
"fmt"
"go/format"
"log"
"maps"
"os"
Expand All @@ -16,6 +18,19 @@ import (
tfjson "github.com/hashicorp/terraform-json"
)

// generateFormatted executes tmpl with data, formats the output with gofmt, and writes it to path.
func generateFormatted(path string, tmpl *template.Template, data any) error {
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return err
}
src, err := format.Source(buf.Bytes())
if err != nil {
return fmt.Errorf("formatting %s: %w", path, err)
}
return os.WriteFile(path, src, 0o644)
}

func normalizeName(name string) string {
return strings.TrimPrefix(name, "databricks_")
}
Expand All @@ -27,14 +42,7 @@ type collection struct {

func (c *collection) Generate(path string) error {
tmpl := template.Must(template.ParseFiles(fmt.Sprintf("./templates/%s.tmpl", c.OutputFile)))
f, err := os.Create(filepath.Join(path, c.OutputFile))
if err != nil {
return err
}

defer func() { _ = f.Close() }()

return tmpl.Execute(f, c)
return generateFormatted(filepath.Join(path, c.OutputFile), tmpl, c)
}

type root struct {
Expand All @@ -46,14 +54,7 @@ type root struct {

func (r *root) Generate(path string) error {
tmpl := template.Must(template.ParseFiles(fmt.Sprintf("./templates/%s.tmpl", r.OutputFile)))
f, err := os.Create(filepath.Join(path, r.OutputFile))
if err != nil {
return err
}

defer func() { _ = f.Close() }()

return tmpl.Execute(f, r)
return generateFormatted(filepath.Join(path, r.OutputFile), tmpl, r)
}

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

// Generate resource_schemas.go
// Generate resource_all.go
{
cr := &collection{
OutputFile: "resource_schemas.go",
OutputFile: "resource_all.go",
Blocks: resources,
}
err := cr.Generate(path)
Expand Down
11 changes: 2 additions & 9 deletions bundle/internal/tf/codegen/generator/named_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package generator

import (
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
Expand Down Expand Up @@ -48,13 +47,7 @@ func (b *namedBlock) Generate(path string) error {
return err
}

f, err := os.Create(filepath.Join(path, fmt.Sprintf(b.filePattern, b.normalizedName())))
if err != nil {
return err
}

defer func() { _ = f.Close() }()

tmpl := template.Must(template.ParseFiles("./templates/block.go.tmpl"))
return tmpl.Execute(f, w)
outPath := filepath.Join(path, fmt.Sprintf(b.filePattern, b.normalizedName()))
return generateFormatted(outPath, tmpl, w)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

package schema

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