diff --git a/bundle/config/bundle.go b/bundle/config/bundle.go index f533c4d1..4bbbb66f 100644 --- a/bundle/config/bundle.go +++ b/bundle/config/bundle.go @@ -18,6 +18,9 @@ type Bundle struct { // Target is set by the mutator that selects the target. Target string `json:"target,omitempty" bundle:"readonly"` + // TargetConfig stores a snapshot of the target configuration when it was selected by SelectTarget. + TargetConfig *Target `json:"target_config,omitempty" bundle:"internal"` + // DEPRECATED. Left for backward compatibility with Target Environment string `json:"environment,omitempty" bundle:"readonly"` diff --git a/bundle/config/mutator/cleanup_targets.go b/bundle/config/mutator/cleanup_targets.go deleted file mode 100644 index 870b5be9..00000000 --- a/bundle/config/mutator/cleanup_targets.go +++ /dev/null @@ -1,29 +0,0 @@ -package mutator - -import ( - "context" - "fmt" - - "github.com/databricks/cli/bundle" - "github.com/databricks/cli/libs/diag" -) - -type cleanupTargets struct { - name string -} - -// CleanupTargets cleans up configuration properties before the configuration -// is reported by the 'bundle summary' command. -func CleanupTargets() bundle.Mutator { - return &cleanupTargets{} -} - -func (m *cleanupTargets) Name() string { - return fmt.Sprintf("Cleanup(%s)", m.name) -} - -func (m *cleanupTargets) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics { - b.Config.Targets = nil - b.Config.Environments = nil - return nil -} diff --git a/bundle/config/mutator/process_target_mode.go b/bundle/config/mutator/process_target_mode.go index fc56da6e..e2727d5b 100644 --- a/bundle/config/mutator/process_target_mode.go +++ b/bundle/config/mutator/process_target_mode.go @@ -179,10 +179,10 @@ func isRunAsSet(r config.Resources) bool { } func isExplicitRootSet(b *bundle.Bundle) bool { - if b.Config.Targets == nil { + if b.Config.Bundle.TargetConfig == nil { return false } - targetConfig := b.Config.Targets[b.Config.Bundle.Target] + targetConfig := b.Config.Bundle.TargetConfig if targetConfig.Workspace == nil { return false } diff --git a/bundle/config/mutator/process_target_mode_test.go b/bundle/config/mutator/process_target_mode_test.go index 73f52dc2..cb64fb46 100644 --- a/bundle/config/mutator/process_target_mode_test.go +++ b/bundle/config/mutator/process_target_mode_test.go @@ -354,11 +354,9 @@ func TestProcessTargetModeProductionOkWithRootPath(t *testing.T) { require.Error(t, diags.Error()) // ... but we're okay if we specify a root path - b.Config.Targets = map[string]*config.Target{ - "": { - Workspace: &config.Workspace{ - RootPath: "some-root-path", - }, + b.Config.Bundle.TargetConfig = &config.Target{ + Workspace: &config.Workspace{ + RootPath: "some-root-path", }, } diags = validateProductionMode(context.Background(), b, false) diff --git a/bundle/config/mutator/select_target.go b/bundle/config/mutator/select_target.go index 2333c97d..3e0ff8b1 100644 --- a/bundle/config/mutator/select_target.go +++ b/bundle/config/mutator/select_target.go @@ -15,6 +15,7 @@ type selectTarget struct { } // SelectTarget merges the specified target into the root configuration. +// After merging, it removes the 'Targets' section from the configuration. func SelectTarget(name string) bundle.Mutator { return &selectTarget{ name: name, @@ -31,7 +32,7 @@ func (m *selectTarget) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnosti } // Get specified target - _, ok := b.Config.Targets[m.name] + targetConfig, ok := b.Config.Targets[m.name] if !ok { return diag.Errorf("%s: no such target. Available targets: %s", m.name, strings.Join(maps.Keys(b.Config.Targets), ", ")) } @@ -43,11 +44,17 @@ func (m *selectTarget) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnosti } // Store specified target in configuration for reference. + b.Config.Bundle.TargetConfig = targetConfig b.Config.Bundle.Target = m.name // We do this for backward compatibility. // TODO: remove when Environments section is not supported anymore. b.Config.Bundle.Environment = b.Config.Bundle.Target + // Cleanup the original targets and environments sections since they + // show up in the JSON output of the 'summary' and 'validate' commands. + b.Config.Targets = nil + b.Config.Environments = nil + return nil } diff --git a/bundle/config/root.go b/bundle/config/root.go index 4b146745..5a809f32 100644 --- a/bundle/config/root.go +++ b/bundle/config/root.go @@ -47,8 +47,8 @@ type Root struct { // Targets can be used to differentiate settings and resources between // bundle deployment targets (e.g. development, staging, production). - // If not specified, the code below initializes this field with a - // single default-initialized target called "default". + // Note that this field is set to 'nil' by the SelectTarget mutator; + // use Bundle.TargetConfig to access the selected target configuration. Targets map[string]*Target `json:"targets,omitempty"` // DEPRECATED. Left for backward compatibility with Targets diff --git a/cmd/bundle/summary.go b/cmd/bundle/summary.go index b3493a81..5a64b46c 100644 --- a/cmd/bundle/summary.go +++ b/cmd/bundle/summary.go @@ -8,7 +8,6 @@ import ( "path/filepath" "github.com/databricks/cli/bundle" - "github.com/databricks/cli/bundle/config/mutator" "github.com/databricks/cli/bundle/deploy/terraform" "github.com/databricks/cli/bundle/phases" "github.com/databricks/cli/cmd/bundle/utils" @@ -61,7 +60,7 @@ func newSummaryCommand() *cobra.Command { } } - diags = bundle.Apply(ctx, b, bundle.Seq(terraform.Load(), mutator.CleanupTargets())) + diags = bundle.Apply(ctx, b, terraform.Load()) if err := diags.Error(); err != nil { return err } diff --git a/cmd/bundle/validate.go b/cmd/bundle/validate.go index 88b16922..496d5d2b 100644 --- a/cmd/bundle/validate.go +++ b/cmd/bundle/validate.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/databricks/cli/bundle" - "github.com/databricks/cli/bundle/config/mutator" "github.com/databricks/cli/bundle/config/validate" "github.com/databricks/cli/bundle/phases" "github.com/databricks/cli/bundle/render" @@ -66,7 +65,6 @@ func newValidateCommand() *cobra.Command { return nil case flags.OutputJSON: - bundle.Apply(ctx, b, mutator.CleanupTargets()) return renderJsonOutput(cmd, b, diags) default: return fmt.Errorf("unknown output type %s", root.OutputType(cmd))