@@ -4,10 +4,14 @@ import (
44 "context"
55 "fmt"
66
7- "github.com/databricks/cli/libs/dyn"
8-
97 "github.com/databricks/cli/bundle"
108 "github.com/databricks/cli/libs/diag"
9+ "github.com/databricks/cli/libs/dyn"
10+ )
11+
12+ const (
13+ filePathFieldName = "file_path"
14+ serializedDashboardFieldName = "serialized_dashboard"
1115)
1216
1317type configureDashboardSerializedDashboard struct {}
@@ -21,32 +25,42 @@ func (c configureDashboardSerializedDashboard) Name() string {
2125}
2226
2327func (c configureDashboardSerializedDashboard ) Apply (_ context.Context , b * bundle.Bundle ) diag.Diagnostics {
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".
28+ var diags diag.Diagnostics
29+
3730 pattern := dyn .NewPattern (
3831 dyn .Key ("resources" ),
3932 dyn .Key ("dashboards" ),
4033 dyn .AnyKey (),
4134 )
35+
36+ // Configure serialized_dashboard field for all dashboards.
4237 err := b .Config .Mutate (func (v dyn.Value ) (dyn.Value , error ) {
4338 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".
4458 return dyn .Walk (v , func (p dyn.Path , v dyn.Value ) (dyn.Value , error ) {
4559 switch len (p ) {
4660 case 0 :
4761 return v , nil
4862 case 1 :
49- if p [0 ] == dyn .Key ("file_path" ) {
63+ if p [0 ] == dyn .Key (filePathFieldName ) {
5064 return v , dyn .ErrDrop
5165 }
5266 }
@@ -57,7 +71,6 @@ func (c configureDashboardSerializedDashboard) Apply(_ context.Context, b *bundl
5771 })
5872 })
5973
60- var diags diag.Diagnostics
6174 diags = diags .Extend (diag .FromErr (err ))
6275 return diags
6376}
0 commit comments