Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions acceptance/bundle/debug/list-targets-root-engine/databricks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
bundle:
name: test-bundle
engine: terraform

targets:
inherited:
default: true
uses_direct:
bundle:
engine: direct
null_target:
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
EnvMatrix.DMS = ["", "true"]
23 changes: 23 additions & 0 deletions acceptance/bundle/debug/list-targets-root-engine/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

>>> [CLI] bundle debug list-targets
inherited (default) engine:terraform
null_target engine:terraform
uses_direct

>>> [CLI] bundle debug list-targets --output json
{
"targets": [
{
"name": "inherited",
"default": true,
"engine": "terraform"
},
{
"name": "null_target",
"engine": "terraform"
},
{
"name": "uses_direct"
}
]
}
2 changes: 2 additions & 0 deletions acceptance/bundle/debug/list-targets-root-engine/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
trace $CLI bundle debug list-targets
trace $CLI bundle debug list-targets --output json
6 changes: 6 additions & 0 deletions acceptance/bundle/debug/list-targets/databricks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ targets:
mode: production
workspace:
host: https://prod.example.com
uses_terraform:
bundle:
engine: terraform
uses_direct:
bundle:
engine: direct
with_vars:
default: ${var.is_default}
mode: ${var.target_mode}
Expand Down
9 changes: 9 additions & 0 deletions acceptance/bundle/debug/list-targets/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
dev (default) development https://dev.example.com
prod production https://prod.example.com
staging
uses_direct
uses_terraform engine:terraform
with_vars ${var.target_mode} ${var.target_host}

>>> [CLI] bundle debug list-targets --output json
Expand All @@ -22,6 +24,13 @@ with_vars ${var.target_mode} ${var.target_host}
{
"name": "staging"
},
{
"name": "uses_direct"
},
{
"name": "uses_terraform",
"engine": "terraform"
},
{
"name": "with_vars",
"mode": "${var.target_mode}",
Expand Down
29 changes: 25 additions & 4 deletions cmd/bundle/debug/list_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/config/engine"
"github.com/databricks/cli/bundle/phases"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/cmdio"
Expand All @@ -23,13 +24,18 @@ type targetInfo struct {
Default bool `json:"default,omitempty"`
Mode config.Mode `json:"mode,omitempty"`
Host string `json:"host,omitempty"`

// Engine is only reported for targets that use the removed Terraform engine.
Engine engine.EngineType `json:"engine,omitempty"`
}

type listTargetsOutput struct {
Targets []targetInfo `json:"targets"`
}

func collectTargets(targets map[string]*config.Target) []targetInfo {
// collectTargets returns the targets sorted by name. The engine of a target is
// its own bundle.engine if set, otherwise the top-level bundle.engine.
func collectTargets(targets map[string]*config.Target, rootEngine engine.EngineType) []targetInfo {
names := slices.Sorted(maps.Keys(targets))

result := make([]targetInfo, 0, len(names))
Expand All @@ -38,7 +44,7 @@ func collectTargets(targets map[string]*config.Target) []targetInfo {
// YAML decoding can leave a nil entry in the map when a target is
// declared with a null value. Skip rather than dereference and panic.
if t == nil {
result = append(result, targetInfo{Name: name})
result = append(result, targetInfo{Name: name, Engine: terraformOnly(rootEngine)})
continue
}
info := targetInfo{
Expand All @@ -49,13 +55,25 @@ func collectTargets(targets map[string]*config.Target) []targetInfo {
if t.Workspace != nil {
info.Host = t.Workspace.Host
}
targetEngine := rootEngine
if t.Bundle != nil && t.Bundle.Engine != engine.EngineNotSet {
targetEngine = t.Bundle.Engine
}
info.Engine = terraformOnly(targetEngine)
result = append(result, info)
}
return result
}

func terraformOnly(e engine.EngineType) engine.EngineType {
if e == engine.EngineTerraform {
return e
}
return engine.EngineNotSet
}

// NewListTargetsCommand returns a command that lists all bundle targets
// with their name, default, mode, and workspace host fields.
// with their name, default, mode, workspace host, and (if terraform) engine fields.
func NewListTargetsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list-targets",
Expand All @@ -79,7 +97,7 @@ func NewListTargetsCommand() *cobra.Command {
return root.ErrAlreadyPrinted
}

targets := collectTargets(b.Config.Targets)
targets := collectTargets(b.Config.Targets, b.Config.Bundle.Engine)

switch root.OutputType(cmd) {
case flags.OutputText:
Expand All @@ -94,6 +112,9 @@ func NewListTargetsCommand() *cobra.Command {
if t.Host != "" {
parts = append(parts, t.Host)
}
if t.Engine != "" {
parts = append(parts, "engine:"+string(t.Engine))
}
cmdio.LogString(ctx, strings.Join(parts, " "))
}
case flags.OutputJSON:
Expand Down
Loading