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 is set by the mutator that selects the target.
Target string `json:"target,omitempty" bundle:"readonly"` 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 // DEPRECATED. Left for backward compatibility with Target
Environment string `json:"environment,omitempty" bundle:"readonly"` 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 { func isExplicitRootSet(b *bundle.Bundle) bool {
if b.Config.Targets == nil { if b.Config.Bundle.TargetConfig == nil {
return false return false
} }
targetConfig := b.Config.Targets[b.Config.Bundle.Target] targetConfig := b.Config.Bundle.TargetConfig
if targetConfig.Workspace == nil { if targetConfig.Workspace == nil {
return false return false
} }

View File

@ -354,11 +354,9 @@ func TestProcessTargetModeProductionOkWithRootPath(t *testing.T) {
require.Error(t, diags.Error()) require.Error(t, diags.Error())
// ... but we're okay if we specify a root path // ... 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{
Workspace: &config.Workspace{ RootPath: "some-root-path",
RootPath: "some-root-path",
},
}, },
} }
diags = validateProductionMode(context.Background(), b, false) diags = validateProductionMode(context.Background(), b, false)

View File

@ -15,6 +15,7 @@ type selectTarget struct {
} }
// SelectTarget merges the specified target into the root configuration. // 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 { func SelectTarget(name string) bundle.Mutator {
return &selectTarget{ return &selectTarget{
name: name, name: name,
@ -31,7 +32,7 @@ func (m *selectTarget) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnosti
} }
// Get specified target // Get specified target
_, ok := b.Config.Targets[m.name] targetConfig, ok := b.Config.Targets[m.name]
if !ok { if !ok {
return diag.Errorf("%s: no such target. Available targets: %s", m.name, strings.Join(maps.Keys(b.Config.Targets), ", ")) 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. // Store specified target in configuration for reference.
b.Config.Bundle.TargetConfig = targetConfig
b.Config.Bundle.Target = m.name b.Config.Bundle.Target = m.name
// We do this for backward compatibility. // We do this for backward compatibility.
// TODO: remove when Environments section is not supported anymore. // TODO: remove when Environments section is not supported anymore.
b.Config.Bundle.Environment = b.Config.Bundle.Target 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 return nil
} }

View File

@ -47,8 +47,8 @@ type Root struct {
// Targets can be used to differentiate settings and resources between // Targets can be used to differentiate settings and resources between
// bundle deployment targets (e.g. development, staging, production). // bundle deployment targets (e.g. development, staging, production).
// If not specified, the code below initializes this field with a // Note that this field is set to 'nil' by the SelectTarget mutator;
// single default-initialized target called "default". // use Bundle.TargetConfig to access the selected target configuration.
Targets map[string]*Target `json:"targets,omitempty"` Targets map[string]*Target `json:"targets,omitempty"`
// DEPRECATED. Left for backward compatibility with Targets // DEPRECATED. Left for backward compatibility with Targets

View File

@ -8,7 +8,6 @@ import (
"path/filepath" "path/filepath"
"github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config/mutator"
"github.com/databricks/cli/bundle/deploy/terraform" "github.com/databricks/cli/bundle/deploy/terraform"
"github.com/databricks/cli/bundle/phases" "github.com/databricks/cli/bundle/phases"
"github.com/databricks/cli/cmd/bundle/utils" "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 { if err := diags.Error(); err != nil {
return err return err
} }

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config/mutator"
"github.com/databricks/cli/bundle/config/validate" "github.com/databricks/cli/bundle/config/validate"
"github.com/databricks/cli/bundle/phases" "github.com/databricks/cli/bundle/phases"
"github.com/databricks/cli/bundle/render" "github.com/databricks/cli/bundle/render"
@ -66,7 +65,6 @@ func newValidateCommand() *cobra.Command {
return nil return nil
case flags.OutputJSON: case flags.OutputJSON:
bundle.Apply(ctx, b, mutator.CleanupTargets())
return renderJsonOutput(cmd, b, diags) return renderJsonOutput(cmd, b, diags)
default: default:
return fmt.Errorf("unknown output type %s", root.OutputType(cmd)) return fmt.Errorf("unknown output type %s", root.OutputType(cmd))