Skip to content

Commit

Permalink
Merge CleanupTarget back into SelectTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartkats-db committed Oct 12, 2024
1 parent 3f45f71 commit 742210c
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 43 deletions.
3 changes: 3 additions & 0 deletions bundle/config/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type Bundle struct {
// Target is set by the mutator that selects the target.
Target string `json:"target,omitempty" bundle:"readonly"`

// Target 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"`

Expand Down
29 changes: 0 additions & 29 deletions bundle/config/mutator/cleanup_targets.go

This file was deleted.

4 changes: 2 additions & 2 deletions bundle/config/mutator/process_target_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 3 additions & 5 deletions bundle/config/mutator/process_target_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion bundle/config/mutator/select_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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), ", "))
}
Expand All @@ -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

// Ckeanup 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
}
4 changes: 2 additions & 2 deletions bundle/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions cmd/bundle/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/bundle/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 742210c

Please sign in to comment.