Skip to content

Commit a370eed

Browse files
committed
add 'debug states' command
1 parent 0f7073b commit a370eed

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

acceptance/bundle/state/state_present/output.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,13 @@ Deployment complete!
102102

103103
>>> print_requests.py --get //api/2.1/unity-catalog/schemas
104104
"cli/[DEV_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID] engine/direct auth/pat"
105+
106+
>>> DATABRICKS_BUNDLE_ENGINE= [CLI] bundle debug states
107+
[TEST_TMP_DIR]/.databricks/bundle/default/terraform/terraform.tfstate: local terraform state serial=3 lineage=""
108+
[TEST_TMP_DIR]/.databricks/bundle/default/resources.json: local direct state serial=10 lineage=""
109+
110+
>>> DATABRICKS_BUNDLE_ENGINE= [CLI] bundle debug states --force-pull
111+
terraform.tfstate: remote terraform state serial=3 lineage=""
112+
[TEST_TMP_DIR]/.databricks/bundle/default/terraform/terraform.tfstate: local terraform state serial=3 lineage=""
113+
resources.json: remote direct state serial=10 lineage=""
114+
[TEST_TMP_DIR]/.databricks/bundle/default/resources.json: local direct state serial=10 lineage=""

acceptance/bundle/state/state_present/script

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ trace DATABRICKS_BUNDLE_ENGINE=terraform musterr $CLI bundle deploy
3636

3737
trace DATABRICKS_BUNDLE_ENGINE= $CLI bundle deploy
3838
trace print_requests.py --get //api/2.1/unity-catalog/schemas | jq '.headers["User-Agent"][0]' | contains.py 'engine/direct' '!terraform' '!tf-provider'
39+
40+
trace DATABRICKS_BUNDLE_ENGINE= $CLI bundle debug states
41+
trace DATABRICKS_BUNDLE_ENGINE= $CLI bundle debug states --force-pull
42+
43+
rm out.requests.txt

bundle/statemgmt/state_pull.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type StateDesc struct {
3535

3636
IsDirect bool `json:"-"`
3737
IsLocal bool `json:"-"`
38+
39+
AllStates []*StateDesc
3840
}
3941

4042
func (s *StateDesc) String() string {
@@ -146,6 +148,7 @@ func PullResourcesState(ctx context.Context, b *bundle.Bundle, alwaysPull Always
146148
}
147149
} else {
148150
winner = states[len(states)-1]
151+
winner.AllStates = states
149152
}
150153

151154
log.Infof(ctx, "Available resource state files (from least to most preferred): %v", states)

cmd/bundle/debug.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ func newDebugCommand() *cobra.Command {
1616
cmd.AddCommand(debug.NewTerraformCommand())
1717
cmd.AddCommand(debug.NewRefSchemaCommand())
1818
cmd.AddCommand(debug.NewPlanCommand())
19+
cmd.AddCommand(debug.NewStatesCommand())
1920
return cmd
2021
}

cmd/bundle/debug/states.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package debug
2+
3+
import (
4+
"strings"
5+
6+
"github.com/databricks/cli/cmd/bundle/utils"
7+
"github.com/databricks/cli/cmd/root"
8+
"github.com/databricks/cli/libs/cmdio"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func NewStatesCommand() *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "states",
15+
Short: "Show available state files",
16+
Args: root.NoArgs,
17+
}
18+
19+
var forcePull bool
20+
cmd.Flags().BoolVar(&forcePull, "force-pull", false, "Skip local cache and load the state from the remote workspace")
21+
22+
cmd.RunE = func(cmd *cobra.Command, args []string) error {
23+
opts := utils.ProcessOptions{
24+
// when using filers must run Initialize() phase otherwise some paths are not present and this happens:
25+
// Error: reading terraform.tfstate: opening: relative path escapes root: terraform.tfstate
26+
SkipInitialize: !forcePull,
27+
ReadState: true,
28+
AlwaysPull: forcePull,
29+
}
30+
31+
_, stateDesc, err := utils.ProcessBundleRet(cmd, opts)
32+
ctx := cmd.Context()
33+
34+
if err != nil {
35+
return err
36+
}
37+
38+
var stateStrs []string
39+
for _, state := range stateDesc.AllStates {
40+
stateStrs = append(stateStrs, state.String())
41+
}
42+
cmdio.LogString(ctx, strings.Join(stateStrs, "\n"))
43+
return nil
44+
}
45+
46+
return cmd
47+
}

0 commit comments

Comments
 (0)