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

Fix metadata computation for empty bundle #939

Merged
merged 4 commits into from
Nov 2, 2023
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
5 changes: 5 additions & 0 deletions bundle/deploy/terraform/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ func BundleToTerraform(config *config.Root) *schema.Root {
}

func TerraformToBundle(state *tfjson.State, config *config.Root) error {
// This is a no-op if the state is empty.
if state.Values == nil || state.Values.RootModule == nil {
return nil
}

for _, resource := range state.Values.RootModule.Resources {
// Limit to resources.
if resource.Mode != tfjson.ManagedResourceMode {
Expand Down
22 changes: 16 additions & 6 deletions bundle/deploy/terraform/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ package terraform
import (
"context"
"fmt"
"slices"

"github.com/databricks/cli/bundle"
"github.com/hashicorp/terraform-exec/tfexec"
tfjson "github.com/hashicorp/terraform-json"
)

type load struct{}
type loadMode int

const ErrorOnEmptyState loadMode = 0

type load struct {
modes []loadMode
}

func (l *load) Name() string {
return "terraform.Load"
Expand All @@ -31,7 +38,7 @@ func (l *load) Apply(ctx context.Context, b *bundle.Bundle) error {
return err
}

err = ValidateState(state)
err = l.validateState(state)
if err != nil {
return err
}
Expand All @@ -45,9 +52,12 @@ func (l *load) Apply(ctx context.Context, b *bundle.Bundle) error {
return nil
}

func ValidateState(state *tfjson.State) error {
func (l *load) validateState(state *tfjson.State) error {
if state.Values == nil {
return fmt.Errorf("no deployment state. Did you forget to run 'databricks bundle deploy'?")
if slices.Contains(l.modes, ErrorOnEmptyState) {
return fmt.Errorf("no deployment state. Did you forget to run 'databricks bundle deploy'?")
}
return nil
}

if state.Values.RootModule == nil {
Expand All @@ -57,6 +67,6 @@ func ValidateState(state *tfjson.State) error {
return nil
}

func Load() bundle.Mutator {
return &load{}
func Load(modes ...loadMode) bundle.Mutator {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are multiple load modes necessary?

Copy link
Contributor Author

@shreyas-goenka shreyas-goenka Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, infact there probably wont be multiple modes anytime soon. The reason I went with this approach is this is more readable at the call site:

terraform.Load(terraform.NoErrorOnEmptyState)

The alternatives add a boolean field arg (not readable at call site) or a enum for the "goal/mode" of load which also did non seem like a good fit here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, it's for zero or one. We can change the callsite to be explicit ("ErrorOnEmptyState") or have two public methods, one with one parameter and one with no parameters. What do you think?

Copy link
Contributor Author

@shreyas-goenka shreyas-goenka Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly it's zero to one. I'm against having two methods because there's no need for it, most of the code would be dup.

Between ErrorOnEmptyState and NoErrorOnEmptyState, I am ok with either. I preferNoErrorOnEmptyState here because the branching is clearer in that case. Note, we cannot call TerraformToBundle if the state is empty. .

Would renaming the mode to AllowEmptyState be more readable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a general rule, don't use negatives in constant names because double negatives read odd. I.e. if I write if !NoErrorOnFoo is harder to parse than if ErrorOnFoo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only need two Load methods, essentially two constructors.

func Load() bundle.Mutator { ... } // or LoadDefault()
func LoadAllowEmptyState() bundle.Mutator { ... } // in which case you don't need a separate options enum
// or
func Load(allowEmptyState boolean) // or LoadWithOptions

The main validation logic could still be the same. This approach just seems unnecessarily generic for what we're trying to do here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We follow this pattern of variadic args for 0/1 modes in other places too. For example:

cli/libs/filer/filer.go

Lines 17 to 21 in d70d744

type DeleteMode int
const (
DeleteRecursively DeleteMode = iota
)

IMO it's better to expose a single public method here, and allow users to toggle the validation they require using the mode arg. Two public APIs seem unnecessary even if the most the code would be common.

I have refactored the code to use ErrorOnEmptyState instead though now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, happy to go with precedent if it exists. I guess this kind of pattern is used for functional options too. Given that this is also quasi-internal to DABs, maybe it is fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I'm a purist, but to me, it doesn't feel good to allow users to call functions in meaningless ways (in this case, with 2 or more values for this option). I'm pro-referential transparency, pro-"expose the exact interface you permit".

return &load{modes: modes}
}
2 changes: 1 addition & 1 deletion bundle/deploy/terraform/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestLoadWithNoState(t *testing.T) {

err = bundle.Apply(context.Background(), b, bundle.Seq(
Initialize(),
Load(),
Load(ErrorOnEmptyState),
))

require.ErrorContains(t, err, "Did you forget to run 'databricks bundle deploy'")
Expand Down
2 changes: 1 addition & 1 deletion cmd/bundle/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newRunCommand() *cobra.Command {
terraform.Interpolate(),
terraform.Write(),
terraform.StatePull(),
terraform.Load(),
terraform.Load(terraform.ErrorOnEmptyState),
))
if err != nil {
return err
Expand Down