Merge CleanupTarget back into SelectTarget

This commit is contained in:
Lennart Kats 2024-10-12 11:54:05 +02:00
parent 3f45f7179f
commit 6ea53065cf
No known key found for this signature in database
GPG Key ID: 1EB8B57673197023
8 changed files with 19 additions and 43 deletions

View File

@ -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"`

View File

@ -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
}

View File

@ -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
}

View File

@ -354,12 +354,10 @@ 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{
"": {
b.Config.Bundle.TargetConfig = &config.Target{
Workspace: &config.Workspace{
RootPath: "some-root-path",
},
},
}
diags = validateProductionMode(context.Background(), b, false)
require.NoError(t, diags.Error())

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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))