diff --git a/bundle/config/root.go b/bundle/config/root.go index 17f2747e..fda3759d 100644 --- a/bundle/config/root.go +++ b/bundle/config/root.go @@ -408,15 +408,19 @@ func rewriteShorthands(v dyn.Value) (dyn.Value, error) { // For each variable, normalize its contents if it is a single string. return dyn.Map(target, "variables", dyn.Foreach(func(_ dyn.Path, variable dyn.Value) (dyn.Value, error) { - if variable.Kind() != dyn.KindString { + switch variable.Kind() { + + case dyn.KindString, dyn.KindBool, dyn.KindFloat, dyn.KindInt: + // Rewrite the variable to a map with a single key called "default". + // This conforms to the variable type. Normalization back to the typed + // configuration will convert this to a string if necessary. + return dyn.NewValue(map[string]dyn.Value{ + "default": variable, + }, variable.Location()), nil + + default: return variable, nil } - - // Rewrite the variable to a map with a single key called "default". - // This conforms to the variable type. - return dyn.NewValue(map[string]dyn.Value{ - "default": variable, - }, variable.Location()), nil })) })) } diff --git a/bundle/tests/variables/variable_overrides_in_target/databricks.yml b/bundle/tests/variables/variable_overrides_in_target/databricks.yml new file mode 100644 index 00000000..4e52b507 --- /dev/null +++ b/bundle/tests/variables/variable_overrides_in_target/databricks.yml @@ -0,0 +1,41 @@ +bundle: + name: foobar + +resources: + pipelines: + my_pipeline: + name: ${var.foo} + continuous: ${var.baz} + clusters: + - num_workers: ${var.bar} + + + +variables: + foo: + default: "a_string" + description: "A string variable" + + bar: + default: 42 + description: "An integer variable" + + baz: + default: true + description: "A boolean variable" + +targets: + use-default-variable-values: + + override-string-variable: + variables: + foo: "overridden_string" + + override-int-variable: + variables: + bar: 43 + + override-both-bool-and-string-variables: + variables: + foo: "overridden_string" + baz: false diff --git a/bundle/tests/variables_test.go b/bundle/tests/variables_test.go index fde36344..f5180268 100644 --- a/bundle/tests/variables_test.go +++ b/bundle/tests/variables_test.go @@ -120,3 +120,52 @@ func TestVariablesWithTargetLookupOverrides(t *testing.T) { assert.Equal(t, "cluster: some-test-cluster", b.Config.Variables["d"].Lookup.String()) assert.Equal(t, "instance-pool: some-test-instance-pool", b.Config.Variables["e"].Lookup.String()) } + +func TestVariableTargetOverrides(t *testing.T) { + var tcases = []struct { + targetName string + pipelineName string + pipelineContinuous bool + pipelineNumWorkers int + }{ + { + "use-default-variable-values", + "a_string", + true, + 42, + }, + { + "override-string-variable", + "overridden_string", + true, + 42, + }, + { + "override-int-variable", + "a_string", + true, + 43, + }, + { + "override-both-bool-and-string-variables", + "overridden_string", + false, + 42, + }, + } + + for _, tcase := range tcases { + t.Run(tcase.targetName, func(t *testing.T) { + b := loadTarget(t, "./variables/variable_overrides_in_target", tcase.targetName) + diags := bundle.Apply(context.Background(), b, bundle.Seq( + mutator.SetVariables(), + mutator.ResolveVariableReferences("variables")), + ) + require.NoError(t, diags.Error()) + + assert.Equal(t, tcase.pipelineName, b.Config.Resources.Pipelines["my_pipeline"].Name) + assert.Equal(t, tcase.pipelineContinuous, b.Config.Resources.Pipelines["my_pipeline"].Continuous) + assert.Equal(t, tcase.pipelineNumWorkers, b.Config.Resources.Pipelines["my_pipeline"].Clusters[0].NumWorkers) + }) + } +}