@@ -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
1713type configureDashboardSerializedDashboard struct {}
@@ -25,42 +21,32 @@ func (c configureDashboardSerializedDashboard) Name() string {
2521}
2622
2723func (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}
0 commit comments