diff --git a/bundle/deploy/terraform/convert.go b/bundle/deploy/terraform/convert.go index 38c7b247..f2d2605a 100644 --- a/bundle/deploy/terraform/convert.go +++ b/bundle/deploy/terraform/convert.go @@ -154,14 +154,6 @@ func BundleToTerraform(config *config.Root) *schema.Root { } func TerraformToBundle(state *tfjson.State, config *config.Root) error { - if state.Values == nil { - return fmt.Errorf("state.Values not set") - } - - if state.Values.RootModule == nil { - return fmt.Errorf("state.Values.RootModule not set") - } - for _, resource := range state.Values.RootModule.Resources { // Limit to resources. if resource.Mode != tfjson.ManagedResourceMode { diff --git a/bundle/deploy/terraform/load.go b/bundle/deploy/terraform/load.go index 7f350abf..f312811e 100644 --- a/bundle/deploy/terraform/load.go +++ b/bundle/deploy/terraform/load.go @@ -6,6 +6,7 @@ import ( "github.com/databricks/bricks/bundle" "github.com/hashicorp/terraform-exec/tfexec" + tfjson "github.com/hashicorp/terraform-json" ) type load struct{} @@ -30,6 +31,11 @@ func (l *load) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutator, e return nil, err } + err = ValidateState(state) + if err != nil { + return nil, err + } + // Merge state into configuration. err = TerraformToBundle(state, &b.Config) if err != nil { @@ -39,6 +45,18 @@ func (l *load) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutator, e return nil, nil } +func ValidateState(state *tfjson.State) error { + if state.Values == nil { + return fmt.Errorf("no deployment state. Did you forget to run 'bricks bundle deploy'?") + } + + if state.Values.RootModule == nil { + return fmt.Errorf("malformed terraform state: RootModule not set") + } + + return nil +} + func Load() bundle.Mutator { return &load{} } diff --git a/bundle/deploy/terraform/load_test.go b/bundle/deploy/terraform/load_test.go new file mode 100644 index 00000000..5ed1c261 --- /dev/null +++ b/bundle/deploy/terraform/load_test.go @@ -0,0 +1,41 @@ +package terraform + +import ( + "context" + "os/exec" + "testing" + + "github.com/databricks/bricks/bundle" + "github.com/databricks/bricks/bundle/config" + "github.com/stretchr/testify/require" +) + +func TestLoadWithNoState(t *testing.T) { + _, err := exec.LookPath("terraform") + if err != nil { + t.Skipf("cannot find terraform binary: %s", err) + } + + b := &bundle.Bundle{ + Config: config.Root{ + Path: t.TempDir(), + Bundle: config.Bundle{ + Environment: "whatever", + Terraform: &config.Terraform{ + ExecPath: "terraform", + }, + }, + }, + } + + t.Setenv("DATABRICKS_HOST", "https://x") + t.Setenv("DATABRICKS_TOKEN", "foobar") + b.WorkspaceClient() + + err = bundle.Apply(context.Background(), b, []bundle.Mutator{ + Initialize(), + Load(), + }) + + require.ErrorContains(t, err, "Did you forget to run 'bricks bundle deploy'") +}