Skip to content

Commit 72bafdb

Browse files
authored
Add bundle/statemgmt from terraform/state_{pull,push}.go (#3057)
## Changes Extract state file management into its own package. No other changes, just pure move. ## Why As part of adding "direct deployment" method (#2926), this package will be extended to handle state both for terraform and direct. ## Tests Existing tests.
1 parent 282b9ed commit 72bafdb

12 files changed

Lines changed: 38 additions & 27 deletions

File tree

bundle/phases/bind.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/databricks/cli/bundle"
77
"github.com/databricks/cli/bundle/deploy/lock"
88
"github.com/databricks/cli/bundle/deploy/terraform"
9+
"github.com/databricks/cli/bundle/statemgmt"
910
"github.com/databricks/cli/libs/diag"
1011
"github.com/databricks/cli/libs/log"
1112
)
@@ -23,11 +24,11 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *terraform.BindOptions) (d
2324
}()
2425

2526
diags = diags.Extend(bundle.ApplySeq(ctx, b,
26-
terraform.StatePull(),
27+
statemgmt.StatePull(),
2728
terraform.Interpolate(),
2829
terraform.Write(),
2930
terraform.Import(opts),
30-
terraform.StatePush(),
31+
statemgmt.StatePush(),
3132
))
3233

3334
return diags
@@ -46,11 +47,11 @@ func Unbind(ctx context.Context, b *bundle.Bundle, resourceType, resourceKey str
4647
}()
4748

4849
diags = diags.Extend(bundle.ApplySeq(ctx, b,
49-
terraform.StatePull(),
50+
statemgmt.StatePull(),
5051
terraform.Interpolate(),
5152
terraform.Write(),
5253
terraform.Unbind(resourceType, resourceKey),
53-
terraform.StatePush(),
54+
statemgmt.StatePush(),
5455
))
5556

5657
return diags

bundle/phases/deploy.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/databricks/cli/bundle/metrics"
1919
"github.com/databricks/cli/bundle/permissions"
2020
"github.com/databricks/cli/bundle/scripts"
21+
"github.com/databricks/cli/bundle/statemgmt"
2122
"github.com/databricks/cli/bundle/trampoline"
2223
"github.com/databricks/cli/libs/cmdio"
2324
"github.com/databricks/cli/libs/diag"
@@ -148,7 +149,7 @@ func deployCore(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
148149
// following original logic, continuing with sequence below even if terraform had errors
149150

150151
diags = diags.Extend(bundle.ApplySeq(ctx, b,
151-
terraform.StatePush(),
152+
statemgmt.StatePush(),
152153
terraform.Load(),
153154
apps.InterpolateVariables(),
154155
apps.UploadConfig(),
@@ -185,7 +186,7 @@ func Deploy(ctx context.Context, b *bundle.Bundle, outputHandler sync.OutputHand
185186
}()
186187

187188
diags = bundle.ApplySeq(ctx, b,
188-
terraform.StatePull(),
189+
statemgmt.StatePull(),
189190
terraform.CheckDashboardsModifiedRemotely(),
190191
deploy.StatePull(),
191192
mutator.ValidateGitDetails(),

bundle/phases/destroy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/databricks/cli/bundle/deploy/files"
1010
"github.com/databricks/cli/bundle/deploy/lock"
1111
"github.com/databricks/cli/bundle/deploy/terraform"
12+
"github.com/databricks/cli/bundle/statemgmt"
1213

1314
"github.com/databricks/cli/libs/cmdio"
1415
"github.com/databricks/cli/libs/diag"
@@ -116,7 +117,7 @@ func Destroy(ctx context.Context, b *bundle.Bundle) (diags diag.Diagnostics) {
116117
}()
117118

118119
diags = diags.Extend(bundle.ApplySeq(ctx, b,
119-
terraform.StatePull(),
120+
statemgmt.StatePull(),
120121
terraform.Interpolate(),
121122
terraform.Write(),
122123
terraform.Plan(terraform.PlanGoal("destroy")),
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package terraform
1+
package statemgmt
22

33
import (
44
"context"
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/databricks/cli/bundle"
1313
"github.com/databricks/cli/bundle/deploy"
14+
tf "github.com/databricks/cli/bundle/deploy/terraform"
1415
"github.com/databricks/cli/libs/diag"
1516
"github.com/databricks/cli/libs/log"
1617
)
@@ -34,7 +35,7 @@ func (l *statePull) remoteState(ctx context.Context, b *bundle.Bundle) (*tfState
3435
return nil, nil, err
3536
}
3637

37-
r, err := f.Read(ctx, TerraformStateFileName)
38+
r, err := f.Read(ctx, tf.TerraformStateFileName)
3839
if err != nil {
3940
return nil, nil, err
4041
}
@@ -55,12 +56,12 @@ func (l *statePull) remoteState(ctx context.Context, b *bundle.Bundle) (*tfState
5556
}
5657

5758
func (l *statePull) localState(ctx context.Context, b *bundle.Bundle) (*tfState, error) {
58-
dir, err := Dir(ctx, b)
59+
dir, err := tf.Dir(ctx, b)
5960
if err != nil {
6061
return nil, err
6162
}
6263

63-
content, err := os.ReadFile(filepath.Join(dir, TerraformStateFileName))
64+
content, err := os.ReadFile(filepath.Join(dir, tf.TerraformStateFileName))
6465
if err != nil {
6566
return nil, err
6667
}
@@ -75,12 +76,12 @@ func (l *statePull) localState(ctx context.Context, b *bundle.Bundle) (*tfState,
7576
}
7677

7778
func (l *statePull) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
78-
dir, err := Dir(ctx, b)
79+
dir, err := tf.Dir(ctx, b)
7980
if err != nil {
8081
return diag.FromErr(err)
8182
}
8283

83-
localStatePath := filepath.Join(dir, TerraformStateFileName)
84+
localStatePath := filepath.Join(dir, tf.TerraformStateFileName)
8485

8586
// Case: Remote state file does not exist. In this case we fallback to using the
8687
// local Terraform state. This allows users to change the "root_path" their bundle is

bundle/deploy/terraform/state_pull_test.go renamed to bundle/statemgmt/state_pull_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package terraform
1+
package statemgmt
22

33
import (
44
"bytes"
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/databricks/cli/bundle"
1313
"github.com/databricks/cli/bundle/config"
14+
tf "github.com/databricks/cli/bundle/deploy/terraform"
1415
mockfiler "github.com/databricks/cli/internal/mocks/libs/filer"
1516
"github.com/databricks/cli/libs/filer"
1617
"github.com/stretchr/testify/assert"
@@ -24,7 +25,7 @@ func mockStateFilerForPull(t *testing.T, contents map[string]any, merr error) fi
2425
f := mockfiler.NewMockFiler(t)
2526
f.
2627
EXPECT().
27-
Read(mock.Anything, TerraformStateFileName).
28+
Read(mock.Anything, tf.TerraformStateFileName).
2829
Return(io.NopCloser(bytes.NewReader(buf)), merr).
2930
Times(1)
3031
return f
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package terraform
1+
package statemgmt
22

33
import (
44
"context"
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/databricks/cli/bundle"
1111
"github.com/databricks/cli/bundle/deploy"
12+
tf "github.com/databricks/cli/bundle/deploy/terraform"
1213
"github.com/databricks/cli/libs/cmdio"
1314
"github.com/databricks/cli/libs/diag"
1415
"github.com/databricks/cli/libs/filer"
@@ -29,13 +30,13 @@ func (l *statePush) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostic
2930
return diag.FromErr(err)
3031
}
3132

32-
dir, err := Dir(ctx, b)
33+
dir, err := tf.Dir(ctx, b)
3334
if err != nil {
3435
return diag.FromErr(err)
3536
}
3637

3738
// Expect the state file to live under dir.
38-
local, err := os.Open(filepath.Join(dir, TerraformStateFileName))
39+
local, err := os.Open(filepath.Join(dir, tf.TerraformStateFileName))
3940
if errors.Is(err, fs.ErrNotExist) {
4041
// The state file can be absent if terraform apply is skipped because
4142
// there are no changes to apply in the plan.
@@ -50,7 +51,7 @@ func (l *statePush) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostic
5051
// Upload state file from local cache directory to filer.
5152
cmdio.LogString(ctx, "Updating deployment state...")
5253
log.Infof(ctx, "Writing local state file to remote state directory")
53-
err = f.Write(ctx, TerraformStateFileName, local, filer.CreateParentDirectories, filer.OverwriteIfExists)
54+
err = f.Write(ctx, tf.TerraformStateFileName, local, filer.CreateParentDirectories, filer.OverwriteIfExists)
5455
if err != nil {
5556
return diag.FromErr(err)
5657
}

bundle/deploy/terraform/state_push_test.go renamed to bundle/statemgmt/state_push_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package terraform
1+
package statemgmt
22

33
import (
44
"context"
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package terraform
1+
package statemgmt
22

33
import (
44
"context"
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/databricks/cli/bundle"
1111
"github.com/databricks/cli/bundle/deploy"
12+
tf "github.com/databricks/cli/bundle/deploy/terraform"
1213
"github.com/databricks/cli/libs/filer"
1314
"github.com/stretchr/testify/require"
1415
)
@@ -21,9 +22,9 @@ func identityFiler(f filer.Filer) deploy.FilerFactory {
2122
}
2223

2324
func localStateFile(t *testing.T, ctx context.Context, b *bundle.Bundle) string {
24-
dir, err := Dir(ctx, b)
25+
dir, err := tf.Dir(ctx, b)
2526
require.NoError(t, err)
26-
return filepath.Join(dir, TerraformStateFileName)
27+
return filepath.Join(dir, tf.TerraformStateFileName)
2728
}
2829

2930
func readLocalState(t *testing.T, ctx context.Context, b *bundle.Bundle) map[string]any {

cmd/bundle/generate/dashboard.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/databricks/cli/bundle/phases"
1919
"github.com/databricks/cli/bundle/render"
2020
"github.com/databricks/cli/bundle/resources"
21+
"github.com/databricks/cli/bundle/statemgmt"
2122
"github.com/databricks/cli/cmd/root"
2223
"github.com/databricks/cli/libs/diag"
2324
"github.com/databricks/cli/libs/dyn"
@@ -353,7 +354,7 @@ func (d *dashboard) runForResource(ctx context.Context, b *bundle.Bundle) diag.D
353354
diags = diags.Extend(bundle.ApplySeq(ctx, b,
354355
terraform.Interpolate(),
355356
terraform.Write(),
356-
terraform.StatePull(),
357+
statemgmt.StatePull(),
357358
terraform.Load(),
358359
))
359360
if diags.HasError() {

cmd/bundle/open.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/databricks/cli/bundle/deploy/terraform"
1313
"github.com/databricks/cli/bundle/phases"
1414
"github.com/databricks/cli/bundle/resources"
15+
"github.com/databricks/cli/bundle/statemgmt"
1516
"github.com/databricks/cli/cmd/bundle/utils"
1617
"github.com/databricks/cli/cmd/root"
1718
"github.com/databricks/cli/libs/cmdio"
@@ -87,7 +88,7 @@ func newOpenCommand() *cobra.Command {
8788

8889
if forcePull || noCache {
8990
diags = bundle.ApplySeq(ctx, b,
90-
terraform.StatePull(),
91+
statemgmt.StatePull(),
9192
terraform.Interpolate(),
9293
terraform.Write(),
9394
)

0 commit comments

Comments
 (0)