From 8dc69365817cdc9b21b0e938cd73e8d9cd5a3f7f Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 15 Aug 2023 11:58:54 +0200 Subject: [PATCH] Merge artifacts and resources block with overrides enabled (#660) ## Changes Originally, these blocks were merged with overrides. This was (inadvertently) disabled in #94. This change re-enables merging these blocks with overrides, such that any field set in an environment override always takes precedence over the field set in the base definition. ## Tests New unit test passes. --- bundle/config/root.go | 10 +++++----- .../resources/databricks.yml | 20 +++++++++++++++++++ .../{ => workspace}/databricks.yml | 0 bundle/tests/environment_overrides_test.go | 18 +++++++++++++---- 4 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 bundle/tests/environment_overrides/resources/databricks.yml rename bundle/tests/environment_overrides/{ => workspace}/databricks.yml (100%) diff --git a/bundle/config/root.go b/bundle/config/root.go index 4ca9d0a0..b6d1efc9 100644 --- a/bundle/config/root.go +++ b/bundle/config/root.go @@ -166,7 +166,7 @@ func (r *Root) Merge(other *Root) error { } // TODO: define and test semantics for merging. - return mergo.MergeWithOverwrite(r, other) + return mergo.Merge(r, other, mergo.WithOverride) } func (r *Root) MergeEnvironment(env *Environment) error { @@ -178,28 +178,28 @@ func (r *Root) MergeEnvironment(env *Environment) error { } if env.Bundle != nil { - err = mergo.MergeWithOverwrite(&r.Bundle, env.Bundle) + err = mergo.Merge(&r.Bundle, env.Bundle, mergo.WithOverride) if err != nil { return err } } if env.Workspace != nil { - err = mergo.MergeWithOverwrite(&r.Workspace, env.Workspace) + err = mergo.Merge(&r.Workspace, env.Workspace, mergo.WithOverride) if err != nil { return err } } if env.Artifacts != nil { - err = mergo.Merge(&r.Artifacts, env.Artifacts, mergo.WithAppendSlice) + err = mergo.Merge(&r.Artifacts, env.Artifacts, mergo.WithOverride, mergo.WithAppendSlice) if err != nil { return err } } if env.Resources != nil { - err = mergo.Merge(&r.Resources, env.Resources, mergo.WithAppendSlice) + err = mergo.Merge(&r.Resources, env.Resources, mergo.WithOverride, mergo.WithAppendSlice) if err != nil { return err } diff --git a/bundle/tests/environment_overrides/resources/databricks.yml b/bundle/tests/environment_overrides/resources/databricks.yml new file mode 100644 index 00000000..eef5dc01 --- /dev/null +++ b/bundle/tests/environment_overrides/resources/databricks.yml @@ -0,0 +1,20 @@ +bundle: + name: environment_overrides + +workspace: + host: https://acme.cloud.databricks.com/ + +resources: + jobs: + job1: + name: "base job" + +environments: + development: + default: true + + staging: + resources: + jobs: + job1: + name: "staging job" diff --git a/bundle/tests/environment_overrides/databricks.yml b/bundle/tests/environment_overrides/workspace/databricks.yml similarity index 100% rename from bundle/tests/environment_overrides/databricks.yml rename to bundle/tests/environment_overrides/workspace/databricks.yml diff --git a/bundle/tests/environment_overrides_test.go b/bundle/tests/environment_overrides_test.go index 4b8401c8..b8cc224a 100644 --- a/bundle/tests/environment_overrides_test.go +++ b/bundle/tests/environment_overrides_test.go @@ -6,12 +6,22 @@ import ( "github.com/stretchr/testify/assert" ) -func TestEnvironmentOverridesDev(t *testing.T) { - b := loadEnvironment(t, "./environment_overrides", "development") +func TestEnvironmentOverridesWorkspaceDev(t *testing.T) { + b := loadEnvironment(t, "./environment_overrides/workspace", "development") assert.Equal(t, "https://development.acme.cloud.databricks.com/", b.Config.Workspace.Host) } -func TestEnvironmentOverridesStaging(t *testing.T) { - b := loadEnvironment(t, "./environment_overrides", "staging") +func TestEnvironmentOverridesWorkspaceStaging(t *testing.T) { + b := loadEnvironment(t, "./environment_overrides/workspace", "staging") assert.Equal(t, "https://staging.acme.cloud.databricks.com/", b.Config.Workspace.Host) } + +func TestEnvironmentOverridesResourcesDev(t *testing.T) { + b := loadEnvironment(t, "./environment_overrides/resources", "development") + assert.Equal(t, "base job", b.Config.Resources.Jobs["job1"].Name) +} + +func TestEnvironmentOverridesResourcesStaging(t *testing.T) { + b := loadEnvironment(t, "./environment_overrides/resources", "staging") + assert.Equal(t, "staging job", b.Config.Resources.Jobs["job1"].Name) +}