Skip to content

Commit 45a3fff

Browse files
pieternIsaac
andcommitted
Skip whole-block references above the key when setting defaults
SetDefault only skipped a pure ${...} reference where the pattern ends. A reference higher up, such as new_cluster: ${resources.jobs.base.job_clusters[0].new_cluster}, still failed with "expected a map to index ...workload_type, found string", because fixed path components do not treat a non-map as a no-match. Walk the pattern one component at a time and skip a pure reference at any level; the referenced block gets the default itself. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 2534bb0 commit 45a3fff

7 files changed

Lines changed: 189 additions & 14 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Fix `bundle validate` failing with "expected a map to index" when a job cluster `new_cluster` or a cluster `workload_type` is a reference to a whole block, such as `${resources.jobs.base.job_clusters[0].new_cluster}`. ([#7338](https://github.com/databricks/cli/pull/7338))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
bundle:
2+
name: test-bundle
3+
4+
resources:
5+
jobs:
6+
base:
7+
job_clusters:
8+
- job_cluster_key: main
9+
new_cluster:
10+
spark_version: 15.4.x-scala2.12
11+
node_type_id: i3.xlarge
12+
num_workers: 1
13+
workload_type:
14+
clients:
15+
jobs: false
16+
viaref:
17+
job_clusters:
18+
- job_cluster_key: main
19+
new_cluster: ${resources.jobs.base.job_clusters[0].new_cluster}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Cloud = false
2+
EnvMatrix.DMS = ["", "true"]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
>>> [CLI] bundle validate
3+
Name: test-bundle
4+
Target: default
5+
Workspace:
6+
User: [USERNAME]
7+
Path: /Workspace/Users/[USERNAME]/.bundle/test-bundle/default
8+
9+
Validation OK!
10+
11+
>>> [CLI] bundle plan -o json
12+
{
13+
"clients": {
14+
"jobs": false,
15+
"notebooks": true
16+
}
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# The new_cluster of viaref is a reference to the whole new_cluster of another job.
2+
# The defaults (e.g. workload_type.clients.notebooks) are not written into the reference; the referenced new_cluster gets them.
3+
trace $CLI bundle validate
4+
trace $CLI bundle plan -o json | jq '.plan."resources.jobs.viaref".new_state.value.job_clusters[0].new_cluster.workload_type'

‎bundle/set_default.go‎

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bundle
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
"github.com/databricks/cli/libs/diag"
@@ -30,20 +31,7 @@ func (m *setDefault) Name() string {
3031

3132
func (m *setDefault) Apply(ctx context.Context, b *Bundle) diag.Diagnostics {
3233
err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) {
33-
return dyn.MapByPattern(v, m.pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
34-
// A default cannot be written into a pure reference to a whole block
35-
// (e.g. "${resources.jobs.base.schedule}"), so leave it to the referenced value.
36-
if s, ok := v.AsString(); ok && dynvar.IsPureVariableReference(s) {
37-
return v, nil
38-
}
39-
_, err := dyn.GetByPath(v, m.key)
40-
switch {
41-
case dyn.IsNoSuchKeyError(err):
42-
return dyn.SetByPath(v, m.key, dyn.V(m.value))
43-
default:
44-
return v, err
45-
}
46-
})
34+
return m.apply(v, dyn.EmptyPath, m.pattern)
4735
})
4836
if err != nil {
4937
return diag.FromErr(err)
@@ -52,6 +40,55 @@ func (m *setDefault) Apply(ctx context.Context, b *Bundle) diag.Diagnostics {
5240
return nil
5341
}
5442

43+
// setDefaultPathError prefixes an error with the full path of the value it was
44+
// raised on, because apply visits sub-values and dyn errors only carry the path
45+
// relative to the value they were raised on.
46+
type setDefaultPathError struct {
47+
path dyn.Path
48+
err error
49+
}
50+
51+
func (e setDefaultPathError) Error() string {
52+
return fmt.Sprintf("%s: %s", e.path, e.err)
53+
}
54+
55+
// Unwrap lets wildcard visits and callers still classify the underlying dyn error.
56+
func (e setDefaultPathError) Unwrap() error {
57+
return e.err
58+
}
59+
60+
// apply walks the pattern one component at a time so that a pure reference
61+
// anywhere along the way is skipped, not only where the pattern ends.
62+
func (m *setDefault) apply(v dyn.Value, prefix dyn.Path, pattern dyn.Pattern) (dyn.Value, error) {
63+
// A default cannot be written into a pure reference to a whole block
64+
// (e.g. "${resources.jobs.base.schedule}"), so leave it to the referenced value.
65+
if s, ok := v.AsString(); ok && dynvar.IsPureVariableReference(s) {
66+
return v, nil
67+
}
68+
69+
if len(pattern) == 0 {
70+
_, err := dyn.GetByPath(v, m.key)
71+
switch {
72+
case dyn.IsNoSuchKeyError(err):
73+
return dyn.SetByPath(v, m.key, dyn.V(m.value))
74+
default:
75+
return v, err
76+
}
77+
}
78+
79+
nv, err := dyn.MapByPattern(v, pattern[:1], func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
80+
return m.apply(v, prefix.Append(p...), pattern[1:])
81+
})
82+
if err == nil || len(prefix) == 0 {
83+
return nv, err
84+
}
85+
// Errors from deeper levels already carry their full path.
86+
if _, ok := errors.AsType[setDefaultPathError](err); ok {
87+
return nv, err
88+
}
89+
return nv, setDefaultPathError{path: prefix, err: err}
90+
}
91+
5592
func SetDefault(ctx context.Context, b *Bundle, pattern string, value any) {
5693
pat, err := dyn.NewPatternFromString(pattern)
5794
if err != nil {

‎bundle/set_default_test.go‎

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

66
"github.com/databricks/cli/bundle/config"
77
"github.com/databricks/cli/libs/dyn"
8+
"github.com/databricks/cli/libs/dyn/dynvar"
89
"github.com/databricks/cli/libs/logdiag"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
@@ -37,3 +38,97 @@ resources:
3738
require.NoError(t, err)
3839
assert.Equal(t, "${resources.jobs.base.schedule}", viaref.MustString())
3940
}
41+
42+
func TestSetDefaultSkipsPureReferenceAboveKey(t *testing.T) {
43+
tests := []struct {
44+
name string
45+
config string
46+
pattern string
47+
set string
48+
ref string
49+
}{
50+
{
51+
name: "job_cluster_new_cluster",
52+
config: `
53+
resources:
54+
jobs:
55+
base:
56+
job_clusters:
57+
- job_cluster_key: main
58+
new_cluster:
59+
workload_type:
60+
clients:
61+
jobs: false
62+
viaref:
63+
job_clusters:
64+
- job_cluster_key: main
65+
new_cluster: ${resources.jobs.base.job_clusters[0].new_cluster}
66+
`,
67+
pattern: "resources.jobs.*.job_clusters[*].new_cluster.workload_type.clients.notebooks",
68+
set: "resources.jobs.base.job_clusters[0].new_cluster.workload_type.clients.notebooks",
69+
ref: "resources.jobs.viaref.job_clusters[0].new_cluster",
70+
},
71+
{
72+
name: "cluster_workload_type",
73+
config: `
74+
resources:
75+
clusters:
76+
base:
77+
workload_type:
78+
clients:
79+
jobs: false
80+
viaref:
81+
workload_type: ${resources.clusters.base.workload_type}
82+
`,
83+
pattern: "resources.clusters.*.workload_type.clients.notebooks",
84+
set: "resources.clusters.base.workload_type.clients.notebooks",
85+
ref: "resources.clusters.viaref.workload_type",
86+
},
87+
}
88+
89+
for _, tc := range tests {
90+
t.Run(tc.name, func(t *testing.T) {
91+
root, diags := config.LoadFromBytes("databricks.yml", []byte(tc.config))
92+
require.Empty(t, diags)
93+
b := &Bundle{Config: *root}
94+
95+
ctx := logdiag.InitContext(t.Context())
96+
logdiag.SetCollect(ctx, true)
97+
SetDefault(ctx, b, tc.pattern, true)
98+
require.Empty(t, logdiag.FlushCollected(ctx))
99+
100+
v := b.Config.Value()
101+
set, err := dyn.GetByPath(v, dyn.MustPathFromString(tc.set))
102+
require.NoError(t, err)
103+
assert.True(t, set.MustBool())
104+
105+
ref, err := dyn.GetByPath(v, dyn.MustPathFromString(tc.ref))
106+
require.NoError(t, err)
107+
assert.True(t, dynvar.IsPureVariableReference(ref.MustString()))
108+
})
109+
}
110+
}
111+
112+
func TestSetDefaultErrorHasFullPath(t *testing.T) {
113+
// Built directly because loading the config would drop a wrong-kind workload_type.
114+
v := dyn.V(map[string]dyn.Value{
115+
"resources": dyn.V(map[string]dyn.Value{
116+
"jobs": dyn.V(map[string]dyn.Value{
117+
"job": dyn.V(map[string]dyn.Value{
118+
"job_clusters": dyn.V([]dyn.Value{
119+
dyn.V(map[string]dyn.Value{
120+
"new_cluster": dyn.V(map[string]dyn.Value{
121+
"workload_type": dyn.V("not a map"),
122+
}),
123+
}),
124+
}),
125+
}),
126+
}),
127+
}),
128+
})
129+
130+
pattern := dyn.MustPatternFromString("resources.jobs.*.job_clusters[*].new_cluster.workload_type.clients")
131+
m := SetDefaultMutator(pattern, "notebooks", true).(*setDefault)
132+
_, err := m.apply(v, dyn.EmptyPath, m.pattern)
133+
assert.EqualError(t, err, `resources.jobs.job.job_clusters[0].new_cluster.workload_type: expected a map to index "clients", found string`)
134+
}

0 commit comments

Comments
 (0)