Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error message when 'bricks bundle run' is executed before 'bricks bundle deploy' #378

Merged
merged 2 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 0 additions & 8 deletions bundle/deploy/terraform/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions bundle/deploy/terraform/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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 {
Expand All @@ -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("terraform show: No state. Did you forget to run 'bricks bundle deploy'?")
andrewnester marked this conversation as resolved.
Show resolved Hide resolved
}

if state.Values.RootModule == nil {
return fmt.Errorf("malformed terraform state: RootModule not set")
}

return nil
}

func Load() bundle.Mutator {
return &load{}
}
41 changes: 41 additions & 0 deletions bundle/deploy/terraform/load_test.go
Original file line number Diff line number Diff line change
@@ -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'")
}