Skip to content

Commit 28533c6

Browse files
committed
refactor: move ConfigureDashboardSerializedDashboard call down the transformation pipeline, closer to terraform write
1 parent bcba28d commit 28533c6

File tree

7 files changed

+29
-37
lines changed

7 files changed

+29
-37
lines changed

bundle/config/mutator/paths/dashboard_paths_visitor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ func VisitDashboardPaths(value dyn.Value, fn VisitFunc) (dyn.Value, error) {
1313
)
1414

1515
return dyn.MapByPattern(value, pattern, func(path dyn.Path, value dyn.Value) (dyn.Value, error) {
16-
return fn(path, TranslateModeLocalAbsoluteFile, value)
16+
return fn(path, TranslateModeLocalRelative, value)
1717
})
1818
}

bundle/config/mutator/resourcemutator/configure_dashboards_serialized_dashboard.go

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/databricks/cli/bundle"
8-
"github.com/databricks/cli/libs/diag"
97
"github.com/databricks/cli/libs/dyn"
10-
)
118

12-
const (
13-
filePathFieldName = "file_path"
14-
serializedDashboardFieldName = "serialized_dashboard"
9+
"github.com/databricks/cli/bundle"
10+
"github.com/databricks/cli/libs/diag"
1511
)
1612

1713
type configureDashboardSerializedDashboard struct{}
@@ -25,42 +21,32 @@ func (c configureDashboardSerializedDashboard) Name() string {
2521
}
2622

2723
func (c configureDashboardSerializedDashboard) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnostics {
28-
var diags diag.Diagnostics
29-
24+
for _, dashboard := range b.Config.Resources.Dashboards {
25+
path := dashboard.FilePath
26+
if path == "" {
27+
continue
28+
}
29+
contents, err := b.SyncRoot.ReadFile(path)
30+
if err != nil {
31+
return diag.FromErr(fmt.Errorf("failed to read serialized dashboard from file_path %s: %w", path, err))
32+
}
33+
dashboard.SerializedDashboard = string(contents)
34+
}
35+
36+
// Drop the "file_path" field. It is mutually exclusive with "serialized_dashboard".
3037
pattern := dyn.NewPattern(
3138
dyn.Key("resources"),
3239
dyn.Key("dashboards"),
3340
dyn.AnyKey(),
3441
)
35-
36-
// Configure serialized_dashboard field for all dashboards.
3742
err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) {
3843
return dyn.MapByPattern(v, pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
39-
// Include "serialized_dashboard" field if "file_path" is set.
40-
// Note: the Terraform resource supports "file_path" natively, but we read the contents of the dashboard here
41-
// to cover the use case of deployments from the workspace
42-
path, ok := v.Get(filePathFieldName).AsString()
43-
if !ok {
44-
return v, nil
45-
}
46-
47-
contents, err := b.SyncRoot.ReadFile(path)
48-
if err != nil {
49-
return dyn.InvalidValue, fmt.Errorf("failed to read serialized dashboard from file_path %s: %w", path, err)
50-
}
51-
52-
v, err = dyn.Set(v, serializedDashboardFieldName, dyn.V(string(contents)))
53-
if err != nil {
54-
return dyn.InvalidValue, fmt.Errorf("failed to set serialized_dashboard: %w", err)
55-
}
56-
57-
// Drop the "file_path" field. It is mutually exclusive with "serialized_dashboard".
5844
return dyn.Walk(v, func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
5945
switch len(p) {
6046
case 0:
6147
return v, nil
6248
case 1:
63-
if p[0] == dyn.Key(filePathFieldName) {
49+
if p[0] == dyn.Key("file_path") {
6450
return v, dyn.ErrDrop
6551
}
6652
}
@@ -71,6 +57,7 @@ func (c configureDashboardSerializedDashboard) Apply(_ context.Context, b *bundl
7157
})
7258
})
7359

60+
var diags diag.Diagnostics
7461
diags = diags.Extend(diag.FromErr(err))
7562
return diags
7663
}

bundle/config/mutator/resourcemutator/resource_mutator.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ func applyInitializeMutators(ctx context.Context, b *bundle.Bundle) diag.Diagnos
4141
// Updates (dynamic): resources.dashboards.*.embed_credentials (sets to false if not set)
4242
ConfigureDashboardDefaults(),
4343

44-
// Reads (typed): resources.dashboards.*.file_path (checks for existing file_path)
45-
// Updates (typed): resources.dashboards.*.serialized_dashboard (sets to the contents of the file in file_path)
46-
// Removes resources.dashboards.*.file_path
47-
ConfigureDashboardSerializedDashboard(),
48-
4944
// Reads (dynamic): resources.volumes.* (checks for existing volume_type)
5045
// Updates (dynamic): resources.volumes.*.volume_type (sets to "MANAGED" if not set)
5146
ConfigureVolumeDefaults(),

bundle/config/mutator/translate_paths_dashboards_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestTranslatePathsDashboards_FilePathRelativeSubDirectory(t *testing.T) {
4949
// Assert that the file path for the dashboard has been converted to its local absolute path.
5050
assert.Equal(
5151
t,
52-
filepath.ToSlash(filepath.Join(dir, "src", "my_dashboard.lvdash.json")),
52+
filepath.ToSlash(filepath.Join("src", "my_dashboard.lvdash.json")),
5353
b.Config.Resources.Dashboards["dashboard"].FilePath,
5454
)
5555
}

bundle/phases/bind.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package phases
33
import (
44
"context"
55

6+
"github.com/databricks/cli/bundle/config/mutator/resourcemutator"
7+
68
"github.com/databricks/cli/bundle"
79
"github.com/databricks/cli/bundle/deploy/lock"
810
"github.com/databricks/cli/bundle/deploy/terraform"
@@ -25,6 +27,7 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *terraform.BindOptions) (d
2527
diags = diags.Extend(bundle.ApplySeq(ctx, b,
2628
terraform.StatePull(),
2729
terraform.Interpolate(),
30+
resourcemutator.ConfigureDashboardSerializedDashboard(),
2831
terraform.Write(),
2932
terraform.Import(opts),
3033
terraform.StatePush(),
@@ -48,6 +51,7 @@ func Unbind(ctx context.Context, b *bundle.Bundle, resourceType, resourceKey str
4851
diags = diags.Extend(bundle.ApplySeq(ctx, b,
4952
terraform.StatePull(),
5053
terraform.Interpolate(),
54+
resourcemutator.ConfigureDashboardSerializedDashboard(),
5155
terraform.Write(),
5256
terraform.Unbind(resourceType, resourceKey),
5357
terraform.StatePush(),

bundle/phases/deploy.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"errors"
66
"slices"
77

8+
"github.com/databricks/cli/bundle/config/mutator/resourcemutator"
9+
810
"github.com/databricks/cli/bundle"
911
"github.com/databricks/cli/bundle/apps"
1012
"github.com/databricks/cli/bundle/artifacts"
@@ -209,6 +211,7 @@ func Deploy(ctx context.Context, b *bundle.Bundle, outputHandler sync.OutputHand
209211
deploy.StatePush(),
210212
permissions.ApplyWorkspaceRootPermissions(),
211213
terraform.Interpolate(),
214+
resourcemutator.ConfigureDashboardSerializedDashboard(),
212215
terraform.Write(),
213216
terraform.Plan(terraform.PlanGoal("deploy")),
214217
)

bundle/phases/destroy.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"errors"
66
"net/http"
77

8+
"github.com/databricks/cli/bundle/config/mutator/resourcemutator"
9+
810
"github.com/databricks/cli/bundle"
911
"github.com/databricks/cli/bundle/deploy/files"
1012
"github.com/databricks/cli/bundle/deploy/lock"
@@ -118,6 +120,7 @@ func Destroy(ctx context.Context, b *bundle.Bundle) (diags diag.Diagnostics) {
118120
diags = diags.Extend(bundle.ApplySeq(ctx, b,
119121
terraform.StatePull(),
120122
terraform.Interpolate(),
123+
resourcemutator.ConfigureDashboardSerializedDashboard(),
121124
terraform.Write(),
122125
terraform.Plan(terraform.PlanGoal("destroy")),
123126
))

0 commit comments

Comments
 (0)