From b7a9f2a3c2631a888875b2d7d6425ed6afac1d1f Mon Sep 17 00:00:00 2001 From: monalisa Date: Tue, 31 Oct 2023 16:48:29 +0100 Subject: [PATCH 1/3] Fix metadata upload for cases when no jobs are defined --- bundle/deploy/terraform/load.go | 18 +++++++++++++++--- bundle/phases/deploy.go | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/bundle/deploy/terraform/load.go b/bundle/deploy/terraform/load.go index 9fd68884b1e..5c03dbc7154 100644 --- a/bundle/deploy/terraform/load.go +++ b/bundle/deploy/terraform/load.go @@ -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 NoErrorOnEmptyState loadMode = 0 + +type load struct { + modes []loadMode +} func (l *load) Name() string { return "terraform.Load" @@ -31,6 +38,11 @@ func (l *load) Apply(ctx context.Context, b *bundle.Bundle) error { return err } + // If state is empty, return early. + if state.Values == nil && slices.Contains(l.modes, NoErrorOnEmptyState) { + return nil + } + err = ValidateState(state) if err != nil { return err @@ -57,6 +69,6 @@ func ValidateState(state *tfjson.State) error { return nil } -func Load() bundle.Mutator { - return &load{} +func Load(modes ...loadMode) bundle.Mutator { + return &load{modes: modes} } diff --git a/bundle/phases/deploy.go b/bundle/phases/deploy.go index 805bae80e6d..74aba7d67cb 100644 --- a/bundle/phases/deploy.go +++ b/bundle/phases/deploy.go @@ -34,7 +34,7 @@ func Deploy() bundle.Mutator { terraform.Apply(), bundle.Seq( terraform.StatePush(), - terraform.Load(), + terraform.Load(terraform.NoErrorOnEmptyState), metadata.Compute(), metadata.Upload(), ), From 951ffcba109a57ce6b9d676a188759608d0f7424 Mon Sep 17 00:00:00 2001 From: monalisa Date: Thu, 2 Nov 2023 01:15:20 +0100 Subject: [PATCH 2/3] address comments --- bundle/deploy/terraform/convert.go | 5 +++++ bundle/deploy/terraform/load.go | 13 ++++--------- bundle/deploy/terraform/load_test.go | 2 +- bundle/phases/deploy.go | 2 +- cmd/bundle/run.go | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bundle/deploy/terraform/convert.go b/bundle/deploy/terraform/convert.go index 3bfc8b83ba8..713858810d3 100644 --- a/bundle/deploy/terraform/convert.go +++ b/bundle/deploy/terraform/convert.go @@ -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 { diff --git a/bundle/deploy/terraform/load.go b/bundle/deploy/terraform/load.go index 5c03dbc7154..b42c14084d4 100644 --- a/bundle/deploy/terraform/load.go +++ b/bundle/deploy/terraform/load.go @@ -12,7 +12,7 @@ import ( type loadMode int -const NoErrorOnEmptyState loadMode = 0 +const ErrorOnEmptyState loadMode = 0 type load struct { modes []loadMode @@ -38,13 +38,8 @@ func (l *load) Apply(ctx context.Context, b *bundle.Bundle) error { return err } - // If state is empty, return early. - if state.Values == nil && slices.Contains(l.modes, NoErrorOnEmptyState) { - return nil - } - - err = ValidateState(state) - if err != nil { + err = validateState(state) + if err != nil && slices.Contains(l.modes, ErrorOnEmptyState) { return err } @@ -57,7 +52,7 @@ func (l *load) Apply(ctx context.Context, b *bundle.Bundle) error { return nil } -func ValidateState(state *tfjson.State) error { +func validateState(state *tfjson.State) error { if state.Values == nil { return fmt.Errorf("no deployment state. Did you forget to run 'databricks bundle deploy'?") } diff --git a/bundle/deploy/terraform/load_test.go b/bundle/deploy/terraform/load_test.go index 1937ca8a266..aeaffa14e9d 100644 --- a/bundle/deploy/terraform/load_test.go +++ b/bundle/deploy/terraform/load_test.go @@ -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'") diff --git a/bundle/phases/deploy.go b/bundle/phases/deploy.go index 74aba7d67cb..805bae80e6d 100644 --- a/bundle/phases/deploy.go +++ b/bundle/phases/deploy.go @@ -34,7 +34,7 @@ func Deploy() bundle.Mutator { terraform.Apply(), bundle.Seq( terraform.StatePush(), - terraform.Load(terraform.NoErrorOnEmptyState), + terraform.Load(), metadata.Compute(), metadata.Upload(), ), diff --git a/cmd/bundle/run.go b/cmd/bundle/run.go index b5a60ee155f..b2766b205e2 100644 --- a/cmd/bundle/run.go +++ b/cmd/bundle/run.go @@ -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 From fc15deb30f64721b4db9ba1dc3d40fb0a8ee5b9c Mon Sep 17 00:00:00 2001 From: monalisa Date: Thu, 2 Nov 2023 01:36:53 +0100 Subject: [PATCH 3/3] make method --- bundle/deploy/terraform/load.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/bundle/deploy/terraform/load.go b/bundle/deploy/terraform/load.go index b42c14084d4..624bf7a50a0 100644 --- a/bundle/deploy/terraform/load.go +++ b/bundle/deploy/terraform/load.go @@ -38,8 +38,8 @@ func (l *load) Apply(ctx context.Context, b *bundle.Bundle) error { return err } - err = validateState(state) - if err != nil && slices.Contains(l.modes, ErrorOnEmptyState) { + err = l.validateState(state) + if err != nil { return err } @@ -52,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 {