Skip to content

Commit 29c7f0b

Browse files
denikIsaac
andcommitted
bundle: drop engine parameters that are always direct
After migration the phases always run on the direct engine, so drop the engine argument from Deploy, deployCore, RunPlan, Destroy, destroyCore, PreDeployChecks, pipelineDeletionCascades, SecretScopeFixups, CheckRunningResource, CheckDashboardsModifiedRemotely and ExtractChanges, together with the branches on it. CheckDashboardsModifiedRemotely moves from bundle/deploy/terraform to bundle/deploy. bind/unbind and apps import now pass the bundle resource group straight to phases.Bind/Unbind instead of round-tripping through Terraform resource names; BindOptions moves next to phases.Bind. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 7d50870 commit 29c7f0b

22 files changed

Lines changed: 118 additions & 268 deletions

‎bundle/config/mutator/resourcemutator/secret_scope_fixups.go‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,17 @@ import (
77
"strings"
88

99
"github.com/databricks/cli/bundle"
10-
"github.com/databricks/cli/bundle/config/engine"
1110
"github.com/databricks/cli/bundle/config/resources"
1211
"github.com/databricks/cli/libs/diag"
1312
"github.com/databricks/cli/libs/dyn"
1413
"github.com/databricks/cli/libs/iamutil"
1514
"github.com/databricks/databricks-sdk-go/service/iam"
1615
)
1716

18-
type secretScopeFixups struct {
19-
engine engine.EngineType
20-
}
17+
type secretScopeFixups struct{}
2118

22-
func SecretScopeFixups(engine engine.EngineType) bundle.Mutator {
23-
return &secretScopeFixups{engine: engine}
19+
func SecretScopeFixups() bundle.Mutator {
20+
return &secretScopeFixups{}
2421
}
2522

2623
func (m *secretScopeFixups) Name() string {
@@ -123,12 +120,6 @@ func collapsePermissions(scope *resources.SecretScope) error {
123120
func (m *secretScopeFixups) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
124121
// Secret scopes by default have the current user as a MANAGE ACL. We need to add it to the client ACL list
125122
// to prevent a phantom persistent diff.
126-
// We do not need to do this in terraform because terraform naively always applies the config during ACL
127-
// creation without checking if the ACL already exists.
128-
// https://github.com/databricks/terraform-provider-databricks/blob/5cb5d3fa46bc4843be1a4c4bce89296eaa2e14fc/secrets/resource_secret_acl.go#L43
129-
if !m.engine.IsDirect() {
130-
return nil
131-
}
132123

133124
// Secret scopes assigns the create MANAGE ACL on it by default. So we always add it to
134125
// the client ACL list as a default.

‎bundle/configsync/diff.go‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/databricks/cli/bundle"
99
"github.com/databricks/cli/bundle/config"
10-
"github.com/databricks/cli/bundle/config/engine"
1110
"github.com/databricks/cli/bundle/deployplan"
1211
"github.com/databricks/cli/libs/dyn"
1312
"github.com/databricks/cli/libs/dyn/convert"
@@ -139,8 +138,8 @@ func isPermissionsOrGrantsSubResource(resourceKey string) bool {
139138
}
140139

141140
// ExtractChanges extracts the map of remote-vs-config changes from a deploy
142-
// plan. engine selects the LocalEdit comparison below.
143-
func ExtractChanges(ctx context.Context, b *bundle.Bundle, plan *deployplan.Plan, engine engine.EngineType) (Changes, error) {
141+
// plan.
142+
func ExtractChanges(ctx context.Context, b *bundle.Bundle, plan *deployplan.Plan) (Changes, error) {
144143
changes := make(Changes)
145144

146145
for resourceKey, entry := range plan.Plan {
@@ -170,12 +169,10 @@ func ExtractChanges(ctx context.Context, b *bundle.Bundle, plan *deployplan.Plan
170169
if change.Operation == OperationSkip {
171170
continue
172171
}
173-
// On the direct engine the state snapshot holds real per-field
174-
// values, so New != Old means the local config diverged from the
175-
// last deploy and this change overwrites that pending edit. The
176-
// terraform sync snapshot stores empty per-resource state, so this
177-
// comparison is meaningless there and is skipped.
178-
if engine.IsDirect() && !structdiff.IsEqual(changeDesc.Old, changeDesc.New) {
172+
// The state snapshot holds real per-field values, so New != Old
173+
// means the local config diverged from the last deploy and this
174+
// change overwrites that pending edit.
175+
if !structdiff.IsEqual(changeDesc.Old, changeDesc.New) {
179176
change.LocalEdit = true
180177
}
181178
change.Value = stripNamePrefix(fullPath, change.Value, b.Config.Presets.NamePrefix)

‎bundle/configsync/diff_test.go‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"testing"
55

66
"github.com/databricks/cli/bundle"
7-
"github.com/databricks/cli/bundle/config/engine"
87
"github.com/databricks/cli/bundle/deployplan"
98
"github.com/stretchr/testify/assert"
109
"github.com/stretchr/testify/require"
@@ -283,23 +282,19 @@ func TestExtractChangesSkipsPermissionsAndGrants(t *testing.T) {
283282
},
284283
}}
285284

286-
for _, eng := range []engine.EngineType{engine.EngineDirect, engine.EngineTerraform} {
287-
t.Run(string(eng), func(t *testing.T) {
288-
changes, err := ExtractChanges(ctx, &bundle.Bundle{}, plan, eng)
289-
require.NoError(t, err)
285+
changes, err := ExtractChanges(ctx, &bundle.Bundle{}, plan)
286+
require.NoError(t, err)
290287

291-
_, hasPerms := changes["resources.jobs.my_job.permissions"]
292-
assert.False(t, hasPerms, "permissions sub-resource must be skipped")
293-
_, hasGrants := changes["resources.schemas.my_schema.grants"]
294-
assert.False(t, hasGrants, "grants sub-resource must be skipped")
288+
_, hasPerms := changes["resources.jobs.my_job.permissions"]
289+
assert.False(t, hasPerms, "permissions sub-resource must be skipped")
290+
_, hasGrants := changes["resources.schemas.my_schema.grants"]
291+
assert.False(t, hasGrants, "grants sub-resource must be skipped")
295292

296-
job, hasJob := changes["resources.jobs.my_job"]
297-
require.True(t, hasJob, "regular resource changes must be kept")
298-
assert.Contains(t, job, "description")
293+
job, hasJob := changes["resources.jobs.my_job"]
294+
require.True(t, hasJob, "regular resource changes must be kept")
295+
assert.Contains(t, job, "description")
299296

300-
namedPerms, hasNamedPerms := changes["resources.jobs.permissions"]
301-
require.True(t, hasNamedPerms, "a resource named permissions must not be skipped")
302-
assert.Contains(t, namedPerms, "description")
303-
})
304-
}
297+
namedPerms, hasNamedPerms := changes["resources.jobs.permissions"]
298+
require.True(t, hasNamedPerms, "a resource named permissions must not be skipped")
299+
assert.Contains(t, namedPerms, "description")
305300
}

bundle/deploy/terraform/check_dashboards_modified_remotely.go renamed to bundle/deploy/check_dashboards_modified_remotely.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package terraform
1+
package deploy
22

33
import (
44
"context"
55
"fmt"
66
"strings"
77

88
"github.com/databricks/cli/bundle"
9-
"github.com/databricks/cli/bundle/config/engine"
109
"github.com/databricks/cli/libs/agent"
1110
"github.com/databricks/cli/libs/diag"
1211
"github.com/databricks/cli/libs/dyn"
@@ -18,17 +17,8 @@ type dashboardState struct {
1817
ETag string
1918
}
2019

21-
func collectDashboardsFromState(ctx context.Context, b *bundle.Bundle, directDeployment bool) ([]dashboardState, error) {
22-
var state ExportedResourcesMap
23-
var err error
24-
if directDeployment {
25-
state = b.DeploymentBundle.ExportState(ctx)
26-
} else {
27-
state, err = ParseResourcesState(ctx, b)
28-
if err != nil {
29-
return nil, err
30-
}
31-
}
20+
func collectDashboardsFromState(ctx context.Context, b *bundle.Bundle) []dashboardState {
21+
state := b.DeploymentBundle.ExportState(ctx)
3222

3323
var dashboards []dashboardState
3424
for resourceKey, instance := range state {
@@ -50,12 +40,11 @@ func collectDashboardsFromState(ctx context.Context, b *bundle.Bundle, directDep
5040
})
5141
}
5242

53-
return dashboards, nil
43+
return dashboards
5444
}
5545

5646
type checkDashboardsModifiedRemotely struct {
5747
isPlan bool
58-
engine engine.EngineType
5948
}
6049

6150
func (l *checkDashboardsModifiedRemotely) Name() string {
@@ -73,10 +62,7 @@ func (l *checkDashboardsModifiedRemotely) Apply(ctx context.Context, b *bundle.B
7362
return nil
7463
}
7564

76-
dashboards, err := collectDashboardsFromState(ctx, b, l.engine.IsDirect())
77-
if err != nil {
78-
return diag.FromErr(err)
79-
}
65+
dashboards := collectDashboardsFromState(ctx, b)
8066

8167
var diags diag.Diagnostics
8268
for _, dashboard := range dashboards {
@@ -131,6 +117,6 @@ func (l *checkDashboardsModifiedRemotely) Apply(ctx context.Context, b *bundle.B
131117
return diags
132118
}
133119

134-
func CheckDashboardsModifiedRemotely(isPlan bool, engine engine.EngineType) *checkDashboardsModifiedRemotely {
135-
return &checkDashboardsModifiedRemotely{isPlan: isPlan, engine: engine}
120+
func CheckDashboardsModifiedRemotely(isPlan bool) *checkDashboardsModifiedRemotely {
121+
return &checkDashboardsModifiedRemotely{isPlan: isPlan}
136122
}

bundle/deploy/terraform/check_dashboards_modified_remotely_test.go renamed to bundle/deploy/check_dashboards_modified_remotely_test.go

Lines changed: 25 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package terraform
1+
package deploy
22

33
import (
4-
"context"
4+
"encoding/json"
55
"errors"
6+
"path/filepath"
67
"testing"
78

89
"github.com/databricks/cli/bundle"
910
"github.com/databricks/cli/bundle/config"
10-
"github.com/databricks/cli/bundle/config/engine"
1111
"github.com/databricks/cli/bundle/config/resources"
12-
"github.com/databricks/cli/internal/testutil"
12+
"github.com/databricks/cli/bundle/direct/dstate"
1313
"github.com/databricks/cli/libs/diag"
1414
"github.com/databricks/databricks-sdk-go/experimental/mocks"
1515
"github.com/databricks/databricks-sdk-go/service/dashboards"
@@ -51,21 +51,22 @@ func TestCheckDashboardsModifiedRemotely_NoDashboards(t *testing.T) {
5151
},
5252
}
5353

54-
diags := bundle.Apply(t.Context(), b, CheckDashboardsModifiedRemotely(false, engine.EngineTerraform))
54+
diags := bundle.Apply(t.Context(), b, CheckDashboardsModifiedRemotely(false))
5555
assert.Empty(t, diags)
5656
}
5757

5858
func TestCheckDashboardsModifiedRemotely_FirstDeployment(t *testing.T) {
5959
b := mockDashboardBundle(t)
60-
diags := bundle.Apply(t.Context(), b, CheckDashboardsModifiedRemotely(false, engine.EngineTerraform))
60+
openDashboardState(t, b, dstate.NewDatabase("lineage", 1))
61+
diags := bundle.Apply(t.Context(), b, CheckDashboardsModifiedRemotely(false))
6162
assert.Empty(t, diags)
6263
}
6364

6465
func TestCheckDashboardsModifiedRemotely_ExistingStateNoChange(t *testing.T) {
6566
ctx := t.Context()
6667

6768
b := mockDashboardBundle(t)
68-
writeFakeDashboardState(t, ctx, b)
69+
writeFakeDashboardState(t, b)
6970

7071
// Mock the call to the API.
7172
m := mocks.NewMockWorkspaceClient(t)
@@ -80,15 +81,15 @@ func TestCheckDashboardsModifiedRemotely_ExistingStateNoChange(t *testing.T) {
8081
b.SetWorkpaceClient(m.WorkspaceClient)
8182

8283
// No changes, so no diags.
83-
diags := bundle.Apply(ctx, b, CheckDashboardsModifiedRemotely(false, engine.EngineTerraform))
84+
diags := bundle.Apply(ctx, b, CheckDashboardsModifiedRemotely(false))
8485
assert.Empty(t, diags)
8586
}
8687

8788
func TestCheckDashboardsModifiedRemotely_ExistingStateChange(t *testing.T) {
8889
ctx := t.Context()
8990

9091
b := mockDashboardBundle(t)
91-
writeFakeDashboardState(t, ctx, b)
92+
writeFakeDashboardState(t, b)
9293

9394
// Mock the call to the API.
9495
m := mocks.NewMockWorkspaceClient(t)
@@ -103,7 +104,7 @@ func TestCheckDashboardsModifiedRemotely_ExistingStateChange(t *testing.T) {
103104
b.SetWorkpaceClient(m.WorkspaceClient)
104105

105106
// The dashboard has changed, so expect an error.
106-
diags := bundle.Apply(ctx, b, CheckDashboardsModifiedRemotely(false, engine.EngineTerraform))
107+
diags := bundle.Apply(ctx, b, CheckDashboardsModifiedRemotely(false))
107108
if assert.Len(t, diags, 1) {
108109
assert.Equal(t, diag.Error, diags[0].Severity)
109110
assert.Equal(t, `dashboard "dash1" has been modified remotely`, diags[0].Summary)
@@ -114,7 +115,7 @@ func TestCheckDashboardsModifiedRemotely_ExistingStateFailureToGet(t *testing.T)
114115
ctx := t.Context()
115116

116117
b := mockDashboardBundle(t)
117-
writeFakeDashboardState(t, ctx, b)
118+
writeFakeDashboardState(t, b)
118119

119120
// Mock the call to the API.
120121
m := mocks.NewMockWorkspaceClient(t)
@@ -126,7 +127,7 @@ func TestCheckDashboardsModifiedRemotely_ExistingStateFailureToGet(t *testing.T)
126127
b.SetWorkpaceClient(m.WorkspaceClient)
127128

128129
// Unable to get the dashboard, so expect an error.
129-
diags := bundle.Apply(ctx, b, CheckDashboardsModifiedRemotely(false, engine.EngineTerraform))
130+
diags := bundle.Apply(ctx, b, CheckDashboardsModifiedRemotely(false))
130131
if assert.Len(t, diags, 1) {
131132
assert.Equal(t, diag.Error, diags[0].Severity)
132133
assert.Equal(t, `failed to get dashboard "dash1"`, diags[0].Summary)
@@ -137,7 +138,7 @@ func TestCheckDashboardsModifiedRemotely_ExistingStateChangePlanMode(t *testing.
137138
ctx := t.Context()
138139

139140
b := mockDashboardBundle(t)
140-
writeFakeDashboardState(t, ctx, b)
141+
writeFakeDashboardState(t, b)
141142

142143
// Mock the call to the API.
143144
m := mocks.NewMockWorkspaceClient(t)
@@ -152,64 +153,21 @@ func TestCheckDashboardsModifiedRemotely_ExistingStateChangePlanMode(t *testing.
152153
b.SetWorkpaceClient(m.WorkspaceClient)
153154

154155
// The dashboard has changed, but in plan mode expect a warning instead of an error.
155-
diags := bundle.Apply(ctx, b, CheckDashboardsModifiedRemotely(true, engine.EngineTerraform))
156+
diags := bundle.Apply(ctx, b, CheckDashboardsModifiedRemotely(true))
156157
if assert.Len(t, diags, 1) {
157158
assert.Equal(t, diag.Warning, diags[0].Severity)
158159
assert.Equal(t, `dashboard "dash1" has been modified remotely`, diags[0].Summary)
159160
}
160161
}
161162

162-
func writeFakeDashboardState(t *testing.T, ctx context.Context, b *bundle.Bundle) {
163-
_, path := b.StateFilenameTerraform(ctx)
164-
165-
// Write fake state file.
166-
testutil.WriteFile(t, path, `
167-
{
168-
"version": 4,
169-
"terraform_version": "1.5.5",
170-
"resources": [
171-
{
172-
"mode": "managed",
173-
"type": "databricks_dashboard",
174-
"name": "dash1",
175-
"instances": [
176-
{
177-
"schema_version": 0,
178-
"attributes": {
179-
"etag": "1000",
180-
"id": "id1"
181-
}
182-
}
183-
]
184-
},
185-
{
186-
"mode": "managed",
187-
"type": "databricks_job",
188-
"name": "job",
189-
"instances": [
190-
{
191-
"schema_version": 0,
192-
"attributes": {
193-
"id": "1234"
194-
}
195-
}
196-
]
197-
},
198-
{
199-
"mode": "managed",
200-
"type": "databricks_dashboard",
201-
"name": "dash2",
202-
"instances": [
203-
{
204-
"schema_version": 0,
205-
"attributes": {
206-
"etag": "1001",
207-
"id": "id2"
208-
}
209-
}
210-
]
211-
}
212-
]
213-
}
214-
`)
163+
func openDashboardState(t *testing.T, b *bundle.Bundle, data dstate.Database) {
164+
b.DeploymentBundle.StateDB.OpenWithData(filepath.Join(t.TempDir(), "resources.json"), data)
165+
}
166+
167+
func writeFakeDashboardState(t *testing.T, b *bundle.Bundle) {
168+
data := dstate.NewDatabase("lineage", 1)
169+
data.State["resources.dashboards.dash1"] = dstate.ResourceEntry{ID: "id1", State: json.RawMessage(`{"etag": "1000"}`)}
170+
data.State["resources.jobs.job"] = dstate.ResourceEntry{ID: "1234", State: json.RawMessage(`{}`)}
171+
data.State["resources.dashboards.dash2"] = dstate.ResourceEntry{ID: "id2", State: json.RawMessage(`{"etag": "1001"}`)}
172+
openDashboardState(t, b, data)
215173
}

‎bundle/deploy/terraform/import.go‎

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)