Skip to content

Commit fc48e0d

Browse files
Auto-fetch provider checksums during codegen instead of hardcoding
The codegen tool (`go run .`) now automatically downloads the SHA256SUMS file from the GitHub release and embeds the checksums into the generated root.go. When bumping the provider version, developers only need to update version.go — checksums are resolved automatically. Co-authored-by: Isaac
1 parent 9b3aed6 commit fc48e0d

5 files changed

Lines changed: 81 additions & 14 deletions

File tree

bundle/internal/tf/codegen/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The entry point for this tool is `.`.
77
It uses `./tmp` a temporary data directory and `../schema` as output directory.
88

99
It automatically installs the Terraform binary as well as the Databricks Terraform provider.
10+
It also fetches SHA256 checksums for the provider archive from GitHub releases.
1011

1112
Run with:
1213

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (r *root) Generate(path string) error {
5353
return tmpl.Execute(f, r)
5454
}
5555

56-
func Run(ctx context.Context, schema *tfjson.ProviderSchema, path string) error {
56+
func Run(ctx context.Context, schema *tfjson.ProviderSchema, checksums *schemapkg.ProviderChecksums, path string) error {
5757
// Generate types for resources
5858
var resources []*namedBlock
5959
for _, k := range sortKeys(schema.ResourceSchemas) {
@@ -151,8 +151,8 @@ func Run(ctx context.Context, schema *tfjson.ProviderSchema, path string) error
151151
r := &root{
152152
OutputFile: "root.go",
153153
ProviderVersion: schemapkg.ProviderVersion,
154-
ProviderChecksumLinuxAmd64: schemapkg.ProviderChecksumLinuxAmd64,
155-
ProviderChecksumLinuxArm64: schemapkg.ProviderChecksumLinuxArm64,
154+
ProviderChecksumLinuxAmd64: checksums.LinuxAmd64,
155+
ProviderChecksumLinuxArm64: checksums.LinuxArm64,
156156
}
157157
err := r.Generate(path)
158158
if err != nil {

bundle/internal/tf/codegen/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ import (
1111
func main() {
1212
ctx := context.Background()
1313

14-
schema, err := schema.Load(ctx)
14+
s, err := schema.Load(ctx)
1515
if err != nil {
1616
log.Fatal(err)
1717
}
1818

19-
err = generator.Run(ctx, schema, "../schema")
19+
log.Printf("fetching provider checksums for v%s", schema.ProviderVersion)
20+
checksums, err := schema.FetchProviderChecksums(schema.ProviderVersion)
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
log.Printf(" linux_amd64: %s", checksums.LinuxAmd64)
25+
log.Printf(" linux_arm64: %s", checksums.LinuxArm64)
26+
27+
err = generator.Run(ctx, s, checksums, "../schema")
2028
if err != nil {
2129
log.Fatal(err)
2230
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package schema
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
)
9+
10+
// ProviderChecksums holds the SHA256 checksums for the Databricks Terraform
11+
// provider archive for supported Linux architectures.
12+
type ProviderChecksums struct {
13+
LinuxAmd64 string
14+
LinuxArm64 string
15+
}
16+
17+
// FetchProviderChecksums downloads the SHA256SUMS file from the GitHub release
18+
// for the given provider version and extracts checksums for the linux_amd64 and
19+
// linux_arm64 archives.
20+
// https://github.com/databricks/terraform-provider-databricks/releases
21+
func FetchProviderChecksums(version string) (*ProviderChecksums, error) {
22+
url := fmt.Sprintf(
23+
"https://github.com/databricks/terraform-provider-databricks/releases/download/v%s/terraform-provider-databricks_%s_SHA256SUMS",
24+
version, version,
25+
)
26+
27+
resp, err := http.Get(url)
28+
if err != nil {
29+
return nil, fmt.Errorf("downloading SHA256SUMS for provider v%s: %w", version, err)
30+
}
31+
defer resp.Body.Close()
32+
33+
if resp.StatusCode != http.StatusOK {
34+
return nil, fmt.Errorf("downloading SHA256SUMS for provider v%s: HTTP %s", version, resp.Status)
35+
}
36+
37+
checksums := &ProviderChecksums{}
38+
amd64Suffix := fmt.Sprintf("terraform-provider-databricks_%s_linux_amd64.zip", version)
39+
arm64Suffix := fmt.Sprintf("terraform-provider-databricks_%s_linux_arm64.zip", version)
40+
41+
scanner := bufio.NewScanner(resp.Body)
42+
for scanner.Scan() {
43+
line := scanner.Text()
44+
parts := strings.Fields(line)
45+
if len(parts) != 2 {
46+
continue
47+
}
48+
switch parts[1] {
49+
case amd64Suffix:
50+
checksums.LinuxAmd64 = parts[0]
51+
case arm64Suffix:
52+
checksums.LinuxArm64 = parts[0]
53+
}
54+
}
55+
if err := scanner.Err(); err != nil {
56+
return nil, fmt.Errorf("reading SHA256SUMS for provider v%s: %w", version, err)
57+
}
58+
59+
if checksums.LinuxAmd64 == "" {
60+
return nil, fmt.Errorf("checksum not found for %s in SHA256SUMS", amd64Suffix)
61+
}
62+
if checksums.LinuxArm64 == "" {
63+
return nil, fmt.Errorf("checksum not found for %s in SHA256SUMS", arm64Suffix)
64+
}
65+
66+
return checksums, nil
67+
}
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
11
package schema
22

33
const ProviderVersion = "1.111.0"
4-
5-
// Checksums for the Databricks Terraform provider archive. These are not used
6-
// inside the CLI. They are co-located here to be output in the
7-
// "databricks bundle debug terraform" output. Downstream applications like the
8-
// CLI docker image use these checksums to verify the integrity of the downloaded
9-
// provider archive. Please update these when the provider version is bumped.
10-
// The checksums are obtained from https://github.com/databricks/terraform-provider-databricks/releases.
11-
const ProviderChecksumLinuxAmd64 = "c1b46bbaf5c4a0b253309dad072e05025e24731536719d4408bacd48dc0ccfd9"
12-
const ProviderChecksumLinuxArm64 = "ce379c424009b01ec4762dee4d0db27cfc554d921b55a0af8e4203b3652259e9"

0 commit comments

Comments
 (0)