Skip to content

Commit a137b89

Browse files
denikIsaac
andauthored
internal: move the user-agent engine tag to the callers of PullResourcesState (databricks#6808)
## Changes The user-agent `engine` dimension used to be set inside `PullResourcesState`. Move it to the callers (`bundle deploy`/`plan` in process.go and the two `bundle generate` resource paths), so the engine tag lives next to where the resolved engine is known. Follow-on cleanups from that move: - `PullResourcesState` no longer mutates the context, so it now returns only `*StateDesc` instead of `(context.Context, *StateDesc)`. - `generate dashboard` and `generate genie-space` had byte-identical state-loading blocks (initialize, resolve engine, pull state, tag engine, load). Extracted into one `loadStateForGenerate` helper so the tag is set in a single place. ## Why Prep for databricks#6749 (move terraform→direct state migration before deploy): that PR needs the engine tag set from the resolved (post-migration) engine, so consolidating the tagging off main keeps its diff focused on the behavior change. ## Tests Behavior-preserving. `acceptance/bundle/user_agent` asserts the `engine/` tag on every recorded request for both engines; it plus the `generate` and `state` suites and the generate unit tests pass with no golden changes. This pull request and its description were written by Isaac. --------- Co-authored-by: Isaac <no-reply@databricks.com>
1 parent ff85929 commit a137b89

5 files changed

Lines changed: 67 additions & 89 deletions

File tree

‎bundle/statemgmt/state_pull.go‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/databricks/cli/libs/filer"
2222
"github.com/databricks/cli/libs/log"
2323
"github.com/databricks/cli/libs/logdiag"
24-
"github.com/databricks/databricks-sdk-go/useragent"
2524
)
2625

2726
type AlwaysPull bool
@@ -127,7 +126,7 @@ func filerRead(ctx context.Context, f filer.Filer, path string, engine engine.En
127126

128127
// PullResourcesState determines correct state to use by reading all 4 states (terraform/direct, local/remote).
129128
// If state is present and the requested engine disagrees, a warning is issued and the state's engine is used.
130-
func PullResourcesState(ctx context.Context, b *bundle.Bundle, alwaysPull AlwaysPull, requiredEngine engine.EngineSetting) (context.Context, *StateDesc) {
129+
func PullResourcesState(ctx context.Context, b *bundle.Bundle, alwaysPull AlwaysPull, requiredEngine engine.EngineSetting) *StateDesc {
131130
var err error
132131

133132
// We read all 4 possible states: terraform/direct X local/remote and then use env var to validate that correct one is used.
@@ -138,7 +137,7 @@ func PullResourcesState(ctx context.Context, b *bundle.Bundle, alwaysPull Always
138137
states := readStates(ctx, b, alwaysPull)
139138

140139
if logdiag.HasError(ctx) {
141-
return ctx, nil
140+
return nil
142141
}
143142

144143
var winner *StateDesc
@@ -160,7 +159,7 @@ func PullResourcesState(ctx context.Context, b *bundle.Bundle, alwaysPull Always
160159
err = validateStates(states)
161160
if err != nil {
162161
logStatesError(ctx, err.Error(), states)
163-
return ctx, winner
162+
return winner
164163
}
165164

166165
if requiredEngine.Type != engine.EngineNotSet && requiredEngine.Type != winner.Engine {
@@ -181,17 +180,13 @@ func PullResourcesState(ctx context.Context, b *bundle.Bundle, alwaysPull Always
181180
}
182181
}
183182

184-
// Set the engine in the user agent
185-
// XXX move this outside this function to bundle/config/engine
186-
ctx = useragent.InContext(ctx, "engine", string(winner.Engine))
187-
188183
if len(states) == 0 {
189-
return ctx, winner
184+
return winner
190185
}
191186

192187
if winner.IsLocal {
193188
// local state is fresh, nothing to do
194-
return ctx, winner
189+
return winner
195190
}
196191

197192
if !winner.IsLocal {
@@ -205,11 +200,11 @@ func PullResourcesState(ctx context.Context, b *bundle.Bundle, alwaysPull Always
205200
err := atomicfile.Write(localStatePath, winner.Content, 0o600, atomicfile.MkDir(0o700))
206201
if err != nil {
207202
logdiag.LogError(ctx, err)
208-
return ctx, winner
203+
return winner
209204
}
210205
}
211206

212-
return ctx, winner
207+
return winner
213208
}
214209

215210
func readStates(ctx context.Context, b *bundle.Bundle, alwaysPull AlwaysPull) []*StateDesc {

‎cmd/bundle/generate/dashboard.go‎

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@ import (
1616
"time"
1717

1818
"github.com/databricks/cli/bundle"
19-
"github.com/databricks/cli/bundle/deploy/terraform"
2019
"github.com/databricks/cli/bundle/generate"
21-
"github.com/databricks/cli/bundle/phases"
2220
"github.com/databricks/cli/bundle/resources"
23-
"github.com/databricks/cli/bundle/statemgmt"
2421
"github.com/databricks/cli/cmd/bundle/deployment"
25-
"github.com/databricks/cli/cmd/bundle/utils"
2622
"github.com/databricks/cli/cmd/root"
2723
"github.com/databricks/cli/libs/cmdio"
2824
"github.com/databricks/cli/libs/diag"
@@ -385,40 +381,7 @@ func (d *dashboard) initialize(ctx context.Context, b *bundle.Bundle) {
385381
}
386382

387383
func (d *dashboard) runForResource(ctx context.Context, b *bundle.Bundle) {
388-
phases.Initialize(ctx, b)
389-
if logdiag.HasError(ctx) {
390-
return
391-
}
392-
393-
requiredEngine, err := utils.ResolveEngineSetting(ctx, b)
394-
if err != nil {
395-
logdiag.LogError(ctx, err)
396-
return
397-
}
398-
ctx, stateDesc := statemgmt.PullResourcesState(ctx, b, statemgmt.AlwaysPull(true), requiredEngine)
399-
if logdiag.HasError(ctx) {
400-
return
401-
}
402-
403-
var state statemgmt.ExportedResourcesMap
404-
if stateDesc.Engine.IsDirect() {
405-
if err := utils.OpenDirectStateForRead(ctx, b, stateDesc); err != nil {
406-
logdiag.LogError(ctx, err)
407-
return
408-
}
409-
state = b.DeploymentBundle.ExportState(ctx)
410-
} else {
411-
var err error
412-
state, err = terraform.ParseResourcesState(ctx, b)
413-
if err != nil {
414-
logdiag.LogError(ctx, err)
415-
return
416-
}
417-
}
418-
419-
bundle.ApplySeqContext(ctx, b,
420-
statemgmt.Load(state),
421-
)
384+
ctx = loadStateForGenerate(ctx, b)
422385
if logdiag.HasError(ctx) {
423386
return
424387
}

‎cmd/bundle/generate/genie_space.go‎

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ import (
1313
"time"
1414

1515
"github.com/databricks/cli/bundle"
16-
"github.com/databricks/cli/bundle/deploy/terraform"
1716
"github.com/databricks/cli/bundle/generate"
18-
"github.com/databricks/cli/bundle/phases"
1917
"github.com/databricks/cli/bundle/resources"
20-
"github.com/databricks/cli/bundle/statemgmt"
2118
"github.com/databricks/cli/cmd/bundle/deployment"
22-
"github.com/databricks/cli/cmd/bundle/utils"
2319
"github.com/databricks/cli/cmd/root"
2420
"github.com/databricks/cli/libs/cmdio"
2521
"github.com/databricks/cli/libs/dyn"
@@ -303,40 +299,7 @@ func (g *genieSpace) initialize(ctx context.Context, b *bundle.Bundle) {
303299
}
304300

305301
func (g *genieSpace) runForResource(ctx context.Context, b *bundle.Bundle) {
306-
phases.Initialize(ctx, b)
307-
if logdiag.HasError(ctx) {
308-
return
309-
}
310-
311-
requiredEngine, err := utils.ResolveEngineSetting(ctx, b)
312-
if err != nil {
313-
logdiag.LogError(ctx, err)
314-
return
315-
}
316-
ctx, stateDesc := statemgmt.PullResourcesState(ctx, b, statemgmt.AlwaysPull(true), requiredEngine)
317-
if logdiag.HasError(ctx) {
318-
return
319-
}
320-
321-
var state statemgmt.ExportedResourcesMap
322-
if stateDesc.Engine.IsDirect() {
323-
if err := utils.OpenDirectStateForRead(ctx, b, stateDesc); err != nil {
324-
logdiag.LogError(ctx, err)
325-
return
326-
}
327-
state = b.DeploymentBundle.ExportState(ctx)
328-
} else {
329-
var err error
330-
state, err = terraform.ParseResourcesState(ctx, b)
331-
if err != nil {
332-
logdiag.LogError(ctx, err)
333-
return
334-
}
335-
}
336-
337-
bundle.ApplySeqContext(ctx, b,
338-
statemgmt.Load(state),
339-
)
302+
ctx = loadStateForGenerate(ctx, b)
340303
if logdiag.HasError(ctx) {
341304
return
342305
}

‎cmd/bundle/generate/state.go‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package generate
2+
3+
import (
4+
"context"
5+
6+
"github.com/databricks/cli/bundle"
7+
"github.com/databricks/cli/bundle/deploy/terraform"
8+
"github.com/databricks/cli/bundle/phases"
9+
"github.com/databricks/cli/bundle/statemgmt"
10+
"github.com/databricks/cli/cmd/bundle/utils"
11+
"github.com/databricks/cli/libs/logdiag"
12+
"github.com/databricks/databricks-sdk-go/useragent"
13+
)
14+
15+
// loadStateForGenerate initializes the bundle, pulls the deployment state, tags the
16+
// resolved engine in the user agent, and loads the state into the bundle. Shared by the
17+
// resource-key paths of "generate dashboard" and "generate genie-space" - the only
18+
// generate commands that resolve a bundle resource against deployment state. Errors are
19+
// reported through logdiag; callers check logdiag.HasError on the returned context.
20+
func loadStateForGenerate(ctx context.Context, b *bundle.Bundle) context.Context {
21+
phases.Initialize(ctx, b)
22+
if logdiag.HasError(ctx) {
23+
return ctx
24+
}
25+
26+
requiredEngine, err := utils.ResolveEngineSetting(ctx, b)
27+
if err != nil {
28+
logdiag.LogError(ctx, err)
29+
return ctx
30+
}
31+
32+
stateDesc := statemgmt.PullResourcesState(ctx, b, statemgmt.AlwaysPull(true), requiredEngine)
33+
if logdiag.HasError(ctx) {
34+
return ctx
35+
}
36+
ctx = useragent.InContext(ctx, "engine", string(stateDesc.Engine))
37+
38+
var state statemgmt.ExportedResourcesMap
39+
if stateDesc.Engine.IsDirect() {
40+
if err := utils.OpenDirectStateForRead(ctx, b, stateDesc); err != nil {
41+
logdiag.LogError(ctx, err)
42+
return ctx
43+
}
44+
state = b.DeploymentBundle.ExportState(ctx)
45+
} else {
46+
state, err = terraform.ParseResourcesState(ctx, b)
47+
if err != nil {
48+
logdiag.LogError(ctx, err)
49+
return ctx
50+
}
51+
}
52+
53+
bundle.ApplySeqContext(ctx, b, statemgmt.Load(state))
54+
return ctx
55+
}

‎cmd/bundle/utils/process.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/databricks/databricks-sdk-go"
3838
"github.com/databricks/databricks-sdk-go/apierr"
3939
"github.com/databricks/databricks-sdk-go/service/bundledeployments"
40+
"github.com/databricks/databricks-sdk-go/useragent"
4041
"github.com/spf13/cobra"
4142
"golang.org/x/mod/semver"
4243
)
@@ -221,10 +222,11 @@ func ProcessBundleRet(cmd *cobra.Command, opts ProcessOptions) (b *bundle.Bundle
221222

222223
if shouldReadState {
223224
// PullResourcesState depends on stateFiler which needs b.Config.Workspace.StatePath which is set in phases.Initialize
224-
ctx, stateDesc = statemgmt.PullResourcesState(ctx, b, statemgmt.AlwaysPull(opts.AlwaysPull), requiredEngine)
225+
stateDesc = statemgmt.PullResourcesState(ctx, b, statemgmt.AlwaysPull(opts.AlwaysPull), requiredEngine)
225226
if logdiag.HasError(ctx) {
226227
return b, stateDesc, root.ErrAlreadyPrinted
227228
}
229+
ctx = useragent.InContext(ctx, "engine", string(stateDesc.Engine))
228230
cmd.SetContext(ctx)
229231
if stateDesc.Engine.IsDirect() {
230232
resolveDeploymentHistory(ctx, b, stateDesc)

0 commit comments

Comments
 (0)