Skip to content

Commit 1f936e6

Browse files
Move LoadStateFromDMS from lock package to statemgmt package
LoadStateFromDMS is a state-loading function, not a lock function. Moving it to statemgmt where it belongs alongside other state management code. Co-authored-by: Isaac
1 parent e7d2877 commit 1f936e6

3 files changed

Lines changed: 65 additions & 55 deletions

File tree

bundle/deploy/lock/deployment_metadata_service.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/databricks/cli/bundle/deploy"
1818
"github.com/databricks/cli/bundle/deployplan"
1919
"github.com/databricks/cli/bundle/direct"
20-
"github.com/databricks/cli/bundle/direct/dstate"
2120
"github.com/databricks/cli/bundle/statemgmt"
2221
"github.com/databricks/cli/internal/build"
2322
"github.com/databricks/cli/libs/filer"
@@ -257,58 +256,6 @@ func makeOperationReporter(svc *tmpdms.DeploymentMetadataAPI, deploymentID, vers
257256
}
258257
}
259258

260-
// LoadStateFromDMS loads resource state from the deployment metadata service
261-
// into the state DB. It first opens the local state file (which contains the
262-
// deployment ID pointer), then populates the resource state from the server.
263-
func LoadStateFromDMS(ctx context.Context, b *bundle.Bundle) error {
264-
if b.DeploymentID == "" {
265-
return nil
266-
}
267-
268-
// Open the local state file first so the state DB path is set.
269-
// The local file contains {"deployment_id":"..."} with no resource state.
270-
db := &b.DeploymentBundle.StateDB
271-
_, localPath := b.StateFilenameDirect(ctx)
272-
if err := db.Open(localPath); err != nil {
273-
return fmt.Errorf("opening local state: %w", err)
274-
}
275-
276-
svc, err := tmpdms.NewDeploymentMetadataAPI(b.WorkspaceClient())
277-
if err != nil {
278-
return fmt.Errorf("failed to create metadata service client: %w", err)
279-
}
280-
281-
resources, err := svc.ListResources(ctx, tmpdms.ListResourcesRequest{
282-
DeploymentID: b.DeploymentID,
283-
})
284-
if err != nil {
285-
return fmt.Errorf("failed to list resources from deployment metadata service: %w", err)
286-
}
287-
288-
// Populate resource state from the server.
289-
db.Data.State = make(map[string]dstate.ResourceEntry)
290-
291-
for _, r := range resources {
292-
// The DMS stores keys without the "resources." prefix (e.g., "jobs.foo").
293-
// The state DB expects the full key (e.g., "resources.jobs.foo").
294-
resourceKey := "resources." + r.ResourceKey
295-
296-
var stateBytes json.RawMessage
297-
if r.State != nil {
298-
stateBytes, err = json.Marshal(r.State)
299-
if err != nil {
300-
return fmt.Errorf("marshaling state for %s: %w", resourceKey, err)
301-
}
302-
}
303-
304-
db.Data.State[resourceKey] = dstate.ResourceEntry{
305-
ID: r.ResourceID,
306-
State: stateBytes,
307-
}
308-
}
309-
310-
return nil
311-
}
312259

313260
// planActionToOperationAction maps a deploy plan action to a metadata service
314261
// operation action type. No-op actions like Skip return ("", nil) and should

bundle/statemgmt/state_dms.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package statemgmt
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/databricks/cli/bundle"
9+
"github.com/databricks/cli/bundle/direct/dstate"
10+
"github.com/databricks/cli/libs/tmpdms"
11+
)
12+
13+
// LoadStateFromDMS loads resource state from the deployment metadata service
14+
// into the state DB. It first opens the local state file (which contains the
15+
// deployment ID pointer), then populates the resource state from the server.
16+
func LoadStateFromDMS(ctx context.Context, b *bundle.Bundle) error {
17+
if b.DeploymentID == "" {
18+
return nil
19+
}
20+
21+
// Open the local state file first so the state DB path is set.
22+
// The local file contains {"deployment_id":"..."} with no resource state.
23+
db := &b.DeploymentBundle.StateDB
24+
_, localPath := b.StateFilenameDirect(ctx)
25+
if err := db.Open(localPath); err != nil {
26+
return fmt.Errorf("opening local state: %w", err)
27+
}
28+
29+
svc, err := tmpdms.NewDeploymentMetadataAPI(b.WorkspaceClient())
30+
if err != nil {
31+
return fmt.Errorf("failed to create metadata service client: %w", err)
32+
}
33+
34+
resources, err := svc.ListResources(ctx, tmpdms.ListResourcesRequest{
35+
DeploymentID: b.DeploymentID,
36+
})
37+
if err != nil {
38+
return fmt.Errorf("failed to list resources from deployment metadata service: %w", err)
39+
}
40+
41+
// Populate resource state from the server.
42+
db.Data.State = make(map[string]dstate.ResourceEntry)
43+
44+
for _, r := range resources {
45+
// The DMS stores keys without the "resources." prefix (e.g., "jobs.foo").
46+
// The state DB expects the full key (e.g., "resources.jobs.foo").
47+
resourceKey := "resources." + r.ResourceKey
48+
49+
var stateBytes json.RawMessage
50+
if r.State != nil {
51+
stateBytes, err = json.Marshal(r.State)
52+
if err != nil {
53+
return fmt.Errorf("marshaling state for %s: %w", resourceKey, err)
54+
}
55+
}
56+
57+
db.Data.State[resourceKey] = dstate.ResourceEntry{
58+
ID: r.ResourceID,
59+
State: stateBytes,
60+
}
61+
}
62+
63+
return nil
64+
}

cmd/bundle/utils/process.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/databricks/cli/bundle/config/engine"
1212
"github.com/databricks/cli/bundle/config/mutator"
1313
"github.com/databricks/cli/bundle/config/validate"
14-
"github.com/databricks/cli/bundle/deploy/lock"
1514
"github.com/databricks/cli/bundle/deployplan"
1615
"github.com/databricks/cli/bundle/direct"
1716
"github.com/databricks/cli/bundle/phases"
@@ -189,7 +188,7 @@ func ProcessBundleRet(cmd *cobra.Command, opts ProcessOptions) (b *bundle.Bundle
189188
needDirectState := stateDesc.Engine.IsDirect() && (opts.InitIDs || opts.ErrorOnEmptyState || opts.Deploy || opts.ReadPlanPath != "" || opts.PreDeployChecks || opts.PostStateFunc != nil)
190189
if needDirectState {
191190
if b.DeploymentID != "" {
192-
if err := lock.LoadStateFromDMS(ctx, b); err != nil {
191+
if err := statemgmt.LoadStateFromDMS(ctx, b); err != nil {
193192
logdiag.LogError(ctx, err)
194193
return b, stateDesc, root.ErrAlreadyPrinted
195194
}

0 commit comments

Comments
 (0)