2022-12-09 07:57:30 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-02-16 20:54:38 +00:00
|
|
|
"github.com/databricks/cli/bundle/internal/tf/schema"
|
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2022-12-09 07:57:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type write struct{}
|
|
|
|
|
|
|
|
func (w *write) Name() string {
|
|
|
|
return "terraform.Write"
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
func (w *write) Apply(ctx context.Context, b *bundle.Bundle) error {
|
2023-09-11 08:18:43 +00:00
|
|
|
dir, err := Dir(ctx, b)
|
2022-12-09 07:57:30 +00:00
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2022-12-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 20:54:38 +00:00
|
|
|
var root *schema.Root
|
|
|
|
err = b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) {
|
|
|
|
root, err = BundleToTerraformWithDynValue(ctx, v)
|
|
|
|
return v, err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-09 07:57:30 +00:00
|
|
|
f, err := os.Create(filepath.Join(dir, "bundle.tf.json"))
|
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2022-12-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
enc := json.NewEncoder(f)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
err = enc.Encode(root)
|
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2022-12-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2022-12-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write returns a [bundle.Mutator] that converts resources in a bundle configuration
|
|
|
|
// to the equivalent Terraform JSON representation and writes the result to a file.
|
|
|
|
func Write() bundle.Mutator {
|
|
|
|
return &write{}
|
|
|
|
}
|