Skip to content

Commit a47f91a

Browse files
committed
update
1 parent 84de40b commit a47f91a

File tree

6 files changed

+35
-49
lines changed

6 files changed

+35
-49
lines changed

bundle/deploy/terraform/import.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,17 @@ type importResource struct {
2828

2929
// Apply implements bundle.Mutator.
3030
func (m *importResource) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
31-
if *b.DirectDeployment {
32-
return diag.Errorf("import is not implemented for DATABRICKS_BUNDLE_ENGINE=direct")
33-
}
34-
3531
dir, err := Dir(ctx, b)
3632
if err != nil {
3733
return diag.FromErr(err)
3834
}
3935

40-
tf := b.Terraform
41-
if tf == nil {
42-
return diag.Errorf("terraform not initialized")
36+
diags := Initialize(ctx, b)
37+
if diags.HasError() {
38+
return diags
4339
}
4440

45-
err = tf.Init(ctx, tfexec.Upgrade(true))
46-
if err != nil {
47-
return diag.Errorf("terraform init: %v", err)
48-
}
41+
tf := b.Terraform
4942
tmpDir, err := os.MkdirTemp("", "state-*")
5043
if err != nil {
5144
return diag.Errorf("terraform init: %v", err)
@@ -109,7 +102,7 @@ func (m *importResource) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
109102
return diag.FromErr(err)
110103
}
111104

112-
return nil
105+
return diags
113106
}
114107

115108
// Name implements bundle.Mutator.

bundle/deploy/terraform/init.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@ import (
2323
"golang.org/x/exp/maps"
2424
)
2525

26-
type initialize struct{}
27-
28-
func (m *initialize) Name() string {
29-
return "terraform.Initialize"
30-
}
31-
32-
func (m *initialize) findExecPath(ctx context.Context, b *bundle.Bundle, tf *config.Terraform, installer Installer) (string, error) {
26+
func findExecPath(ctx context.Context, b *bundle.Bundle, tf *config.Terraform, installer Installer) (string, error) {
3327
// If set, pass it through [exec.LookPath] to resolve its absolute path.
3428
if tf.ExecPath != "" {
3529
execPath, err := exec.LookPath(tf.ExecPath)
@@ -303,14 +297,14 @@ func getTerraformExec(ctx context.Context, b *bundle.Bundle, execPath string) (*
303297
return tfexec.NewTerraform(workingDir, execPath)
304298
}
305299

306-
func (m *initialize) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
300+
func Initialize(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
307301
tfConfig := b.Config.Bundle.Terraform
308302
if tfConfig == nil {
309303
tfConfig = &config.Terraform{}
310304
b.Config.Bundle.Terraform = tfConfig
311305
}
312306

313-
execPath, err := m.findExecPath(ctx, b, tfConfig, tfInstaller{})
307+
execPath, err := findExecPath(ctx, b, tfConfig, tfInstaller{})
314308
if err != nil {
315309
return diag.FromErr(err)
316310
}
@@ -354,10 +348,12 @@ func (m *initialize) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnosti
354348
return diag.FromErr(err)
355349
}
356350

351+
err = tfe.Init(ctx, tfexec.Upgrade(true))
352+
if err != nil {
353+
return diag.Errorf("terraform init: %v", err)
354+
}
355+
357356
b.Terraform = tfe
358-
return nil
359-
}
360357

361-
func Initialize() bundle.Mutator {
362-
return &initialize{}
358+
return nil
363359
}

bundle/deploy/terraform/plan.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,27 @@ func (p *plan) Name() string {
2525
return "terraform.Plan"
2626
}
2727

28-
func (p *plan) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
29-
tf := b.Terraform
30-
if tf == nil {
31-
return diag.Errorf("terraform not initialized")
28+
func initTF(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
29+
diags := Initialize(ctx, b)
30+
if diags.HasError() {
31+
return diags
3232
}
3333

34+
tf := b.Terraform
3435
err := tf.Init(ctx, tfexec.Upgrade(true))
3536
if err != nil {
3637
return diag.Errorf("terraform init: %v", err)
3738
}
3839

40+
return nil
41+
}
42+
43+
func (p *plan) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
44+
diags := Initialize(ctx, b)
45+
if diags.HasError() {
46+
return diags
47+
}
48+
3949
// Persist computed plan
4050
tfDir, err := Dir(ctx, b)
4151
if err != nil {
@@ -44,7 +54,7 @@ func (p *plan) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
4454
planPath := filepath.Join(tfDir, "plan")
4555
destroy := p.goal == PlanDestroy
4656

47-
notEmpty, err := tf.Plan(ctx, tfexec.Destroy(destroy), tfexec.Out(planPath))
57+
notEmpty, err := b.Terraform.Plan(ctx, tfexec.Destroy(destroy), tfexec.Out(planPath))
4858
if err != nil {
4959
return diag.FromErr(err)
5060
}
@@ -54,7 +64,7 @@ func (p *plan) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
5464
b.TerraformPlanIsEmpty = !notEmpty
5565

5666
log.Debugf(ctx, "Planning complete and persisted at %s\n", planPath)
57-
return nil
67+
return diags
5868
}
5969

6070
// Plan returns a [bundle.Mutator] that runs the equivalent of `terraform plan -out ./plan`

bundle/deploy/terraform/unbind.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/databricks/cli/bundle"
88
"github.com/databricks/cli/libs/diag"
9-
"github.com/hashicorp/terraform-exec/tfexec"
109
)
1110

1211
type unbind struct {
@@ -16,17 +15,13 @@ type unbind struct {
1615
}
1716

1817
func (m *unbind) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
19-
tf := b.Terraform
20-
if tf == nil {
21-
return diag.Errorf("terraform not initialized")
22-
}
23-
24-
err := tf.Init(ctx, tfexec.Upgrade(true))
25-
if err != nil {
26-
return diag.Errorf("terraform init: %v", err)
18+
diags := Initialize(ctx, b)
19+
if diags.HasError() {
20+
return diags
2721
}
22+
tf := b.Terraform
2823

29-
err = tf.StateRm(ctx, fmt.Sprintf("%s.%s", m.tfResourceType, m.resourceKey))
24+
err := tf.StateRm(ctx, fmt.Sprintf("%s.%s", m.tfResourceType, m.resourceKey))
3025
if err != nil {
3126
return diag.Errorf("terraform state rm: %v", err)
3227
}

bundle/statemgmt/state_load.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ func (l *load) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
4646
return diag.FromErr(err)
4747
}
4848
} else {
49-
tf := b.Terraform
50-
if tf == nil {
51-
return diag.Errorf("terraform not initialized")
52-
}
53-
5449
var err error
5550
state, err = terraform.ParseResourcesState(ctx, b)
5651
if err != nil {

bundle/statemgmt/state_pull.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"github.com/databricks/cli/bundle"
1717
"github.com/databricks/cli/bundle/deploy"
18-
"github.com/databricks/cli/bundle/deploy/terraform"
1918
"github.com/databricks/cli/libs/env"
2019
"github.com/databricks/cli/libs/filer"
2120
"github.com/databricks/cli/libs/log"
@@ -131,8 +130,6 @@ func PullResourcesState(ctx context.Context, b *bundle.Bundle) context.Context {
131130

132131
if !*b.DirectDeployment {
133132
engine = "terraform"
134-
// XXX move this close to tf.Init()
135-
bundle.ApplyContext(ctx, b, terraform.Initialize())
136133
}
137134

138135
// Set the engine in the user agent

0 commit comments

Comments
 (0)