@@ -4,10 +4,14 @@ import (
4
4
"context"
5
5
"fmt"
6
6
7
- "github.com/databricks/cli/libs/dyn"
8
-
9
7
"github.com/databricks/cli/bundle"
10
8
"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"
11
15
)
12
16
13
17
type configureDashboardSerializedDashboard struct {}
@@ -21,32 +25,42 @@ func (c configureDashboardSerializedDashboard) Name() string {
21
25
}
22
26
23
27
func (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
+
37
30
pattern := dyn .NewPattern (
38
31
dyn .Key ("resources" ),
39
32
dyn .Key ("dashboards" ),
40
33
dyn .AnyKey (),
41
34
)
35
+
36
+ // Configure serialized_dashboard field for all dashboards.
42
37
err := b .Config .Mutate (func (v dyn.Value ) (dyn.Value , error ) {
43
38
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".
44
58
return dyn .Walk (v , func (p dyn.Path , v dyn.Value ) (dyn.Value , error ) {
45
59
switch len (p ) {
46
60
case 0 :
47
61
return v , nil
48
62
case 1 :
49
- if p [0 ] == dyn .Key ("file_path" ) {
63
+ if p [0 ] == dyn .Key (filePathFieldName ) {
50
64
return v , dyn .ErrDrop
51
65
}
52
66
}
@@ -57,7 +71,6 @@ func (c configureDashboardSerializedDashboard) Apply(_ context.Context, b *bundl
57
71
})
58
72
})
59
73
60
- var diags diag.Diagnostics
61
74
diags = diags .Extend (diag .FromErr (err ))
62
75
return diags
63
76
}
0 commit comments