Skip to content
Draft
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
2 changes: 0 additions & 2 deletions acceptance/bundle/deploy/snapshot-comparison/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ Deployment complete!

=== Run migrate on bundle 1
>>> [CLI] bundle deployment migrate
Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged
Success! Migrated 2 resources to direct engine state file: [TEST_TMP_DIR]/bundle1/.databricks/bundle/default/resources.json

Validate the migration by running "databricks bundle plan", there should be no actions planned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Usage:

Flags:
-h, --help help for migrate
--noplancheck Skip running bundle plan before migration.
--noplancheck No-op (kept for compatibility).

Global Flags:
--debug enable debug logging
Expand Down
2 changes: 0 additions & 2 deletions acceptance/bundle/migrate/basic/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Updating deployment state...
Deployment complete!

>>> [CLI] bundle deployment migrate
Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
Plan: 0 to add, 0 to change, 0 to delete, 3 unchanged
Success! Migrated 3 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/dev/resources.json

Validate the migration by running "databricks bundle plan", there should be no actions planned.
Expand Down
2 changes: 0 additions & 2 deletions acceptance/bundle/migrate/dashboards/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ Updating deployment state...
Deployment complete!

>>> [CLI] bundle deployment migrate
Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged
Success! Migrated 1 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/default/resources.json

Validate the migration by running "databricks bundle plan", there should be no actions planned.
Expand Down
11 changes: 1 addition & 10 deletions acceptance/bundle/migrate/default-python/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,7 @@ Deployment complete!

>>> print_state.py

>>> musterr [CLI] bundle deployment migrate
Building python_artifact...
Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
Building python_artifact...
update jobs.sample_job

Plan: 0 to add, 1 to change, 0 to delete, 1 unchanged
Error: 'databricks bundle plan' shows actions planned, aborting migration. Please run 'databricks bundle deploy' first to ensure your bundle is up to date, If actions persist after deploy, skip plan check with --noplancheck option

>>> [CLI] bundle deployment migrate --noplancheck
>>> [CLI] bundle deployment migrate
Building python_artifact...
Success! Migrated 2 resources to direct engine state file: [TEST_TMP_DIR]/my_default_python/.databricks/bundle/dev/resources.json

Expand Down
3 changes: 1 addition & 2 deletions acceptance/bundle/migrate/default-python/script
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ cd my_default_python
trace DATABRICKS_BUNDLE_ENGINE=terraform $CLI bundle deploy
trace print_state.py > ../out.state_original.json

trace musterr $CLI bundle deployment migrate
trace $CLI bundle deployment migrate --noplancheck
trace $CLI bundle deployment migrate

trace print_state.py > ../out.state_after_migration.json
trace jq '.. | .libraries? | select(.)' ../out.state_after_migration.json
Expand Down
2 changes: 0 additions & 2 deletions acceptance/bundle/migrate/grants/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ Updating deployment state...
Deployment complete!

>>> [CLI] bundle deployment migrate
Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
Plan: 0 to add, 0 to change, 0 to delete, 6 unchanged
Success! Migrated 6 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/default/resources.json

Validate the migration by running "databricks bundle plan", there should be no actions planned.
Expand Down
2 changes: 0 additions & 2 deletions acceptance/bundle/migrate/permissions/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ Updating deployment state...
Deployment complete!

>>> [CLI] bundle deployment migrate
Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
Plan: 0 to add, 0 to change, 0 to delete, 4 unchanged
Success! Migrated 4 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/default/resources.json

Validate the migration by running "databricks bundle plan", there should be no actions planned.
Expand Down
4 changes: 0 additions & 4 deletions acceptance/bundle/migrate/profile_arg/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ Updating deployment state...
Deployment complete!

>>> [CLI] bundle deployment migrate -p non_existent321
Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged
Success! Migrated 1 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/dev/resources.json

Validate the migration by running "databricks bundle plan -p non_existent321", there should be no actions planned.
Expand All @@ -24,8 +22,6 @@ Updating deployment state...
Deployment complete!

>>> [CLI] bundle deployment migrate -p non_existent321 -t prod
Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged
Success! Migrated 2 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/prod/resources.json

Validate the migration by running "databricks bundle plan -t prod -p non_existent321", there should be no actions planned.
Expand Down
14 changes: 0 additions & 14 deletions acceptance/bundle/migrate/runas/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,6 @@ Consider using a adding a top-level permissions section such as the following:
See https://docs.databricks.com/dev-tools/bundles/permissions.html to learn more about permission configuration.
in databricks.yml:5:3

Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
Recommendation: permissions section should explicitly include the current deployment identity '[USERNAME]' or one of its groups
If it is not included, CAN_MANAGE permissions are only applied if the present identity is used to deploy.

Consider using a adding a top-level permissions section such as the following:

permissions:
- user_name: [USERNAME]
level: CAN_MANAGE

See https://docs.databricks.com/dev-tools/bundles/permissions.html to learn more about permission configuration.
in databricks.yml:5:3

Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged
Success! Migrated 2 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/production/resources.json

Validate the migration by running "databricks bundle plan", there should be no actions planned.
Expand Down
4 changes: 0 additions & 4 deletions acceptance/bundle/migrate/var_arg/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ Updating deployment state...
Deployment complete!

>>> [CLI] bundle deployment migrate --var=job_name=Custom Job Name
Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged
Success! Migrated 1 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/dev/resources.json

Validate the migration by running "databricks bundle plan --var 'job_name=Custom Job Name'", there should be no actions planned.
Expand Down Expand Up @@ -39,8 +37,6 @@ Updating deployment state...
Deployment complete!

>>> [CLI] bundle deployment migrate --var job_name=Custom Job Name
Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged
Success! Migrated 1 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/dev/resources.json

Validate the migration by running "databricks bundle plan --var 'job_name=Custom Job Name'", there should be no actions planned.
Expand Down
24 changes: 18 additions & 6 deletions bundle/deploy/terraform/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ type stateInstanceAttributes struct {
}

// Returns a mapping resourceKey -> stateInstanceAttributes
// ParseResourcesStateFromBytes parses a terraform state file from already-read bytes.
func ParseResourcesStateFromBytes(ctx context.Context, raw []byte) (ExportedResourcesMap, error) {
var state resourcesState
if err := json.Unmarshal(raw, &state); err != nil {
return nil, err
}
return resourcesStateToMap(ctx, &state)
}

func parseResourcesState(ctx context.Context, path string) (ExportedResourcesMap, error) {
rawState, err := os.ReadFile(path)
if err != nil {
Expand All @@ -59,12 +68,10 @@ func parseResourcesState(ctx context.Context, path string) (ExportedResourcesMap
}
return nil, err
}
var state resourcesState
err = json.Unmarshal(rawState, &state)
if err != nil {
return nil, err
}
return ParseResourcesStateFromBytes(ctx, rawState)
}

func resourcesStateToMap(ctx context.Context, state *resourcesState) (ExportedResourcesMap, error) {
if state.Version != SupportedStateVersion {
return nil, fmt.Errorf("unsupported deployment state version: %d. Try re-deploying the bundle", state.Version)
}
Expand Down Expand Up @@ -131,5 +138,10 @@ func ParseResourcesState(ctx context.Context, b *bundle.Bundle) (ExportedResourc
return nil, err
}
filename, _ := b.StateFilenameTerraform(ctx)
return parseResourcesState(ctx, filepath.Join(cacheDir, filename))
return ParseResourcesStateFromPath(ctx, filepath.Join(cacheDir, filename))
}

// ParseResourcesStateFromPath parses a terraform state file at a known path.
func ParseResourcesStateFromPath(ctx context.Context, path string) (ExportedResourcesMap, error) {
return parseResourcesState(ctx, path)
}
26 changes: 3 additions & 23 deletions bundle/direct/bundle_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import (
"github.com/databricks/databricks-sdk-go"
)

type MigrateMode bool

func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.WorkspaceClient, plan *deployplan.Plan, migrateMode MigrateMode) {
func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.WorkspaceClient, plan *deployplan.Plan) {
if plan == nil {
panic("Planning is not done")
}
Expand Down Expand Up @@ -52,9 +50,6 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa

action := entry.Action
errorPrefix := fmt.Sprintf("cannot %s %s", action, resourceKey)
if migrateMode {
errorPrefix = "cannot migrate " + resourceKey
}

if action == deployplan.Undefined {
logdiag.LogError(ctx, fmt.Errorf("cannot deploy %s: unknown action %q", resourceKey, action))
Expand Down Expand Up @@ -82,10 +77,6 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa
}

if action == deployplan.Delete {
if migrateMode {
logdiag.LogError(ctx, fmt.Errorf("%s: Unexpected delete action during migration", errorPrefix))
return false
}
err = d.Destroy(ctx, &b.StateDB)
if err != nil {
logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err))
Expand Down Expand Up @@ -113,19 +104,8 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa
return false
}

if migrateMode {
// In migration mode we're reading resources in DAG order so that we have fully resolved config snapshots stored
id := b.StateDB.GetResourceID(resourceKey)
if id == "" {
logdiag.LogError(ctx, fmt.Errorf("state entry not found for %q", resourceKey))
return false
}
err = b.StateDB.SaveState(resourceKey, id, sv.Value, entry.DependsOn)
} else {
// TODO: redo calcDiff to downgrade planned action if possible (?)
err = d.Deploy(ctx, &b.StateDB, sv.Value, action, entry)
}

// TODO: redo calcDiff to downgrade planned action if possible (?)
err = d.Deploy(ctx, &b.StateDB, sv.Value, action, entry)
if err != nil {
logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err))
return false
Expand Down
6 changes: 6 additions & 0 deletions bundle/direct/bundle_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,12 @@ func (b *DeploymentBundle) makePlan(ctx context.Context, configRoot *config.Root
return p, nil
}

// ExtractReferences extracts all variable references from the config subtree rooted at node.
// Returns a map from structpath string (field path within the resource) to template string.
func ExtractReferences(root dyn.Value, node string) (map[string]string, error) {
return extractReferences(root, node)
}

func extractReferences(root dyn.Value, node string) (map[string]string, error) {
nodeType := config.GetResourceTypeFromKey(node)
refs := make(map[string]string)
Expand Down
Loading
Loading