-
Notifications
You must be signed in to change notification settings - Fork 194
Refactor validate_direct_only_resources and approval log loops #5215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0254870
Use ResourceDescription in validate_direct_only_resources
janniklasrose 62d4ae8
Collapse per-type if-chains in approvalForDeploy/Destroy
janniklasrose 6e6940d
Compute direct-only resources from existing engine maps
janniklasrose c3bef30
Pass trailingNewline as a parameter to logApprovalGroups
janniklasrose File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
bundle/config/mutator/validate_direct_only_resources_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package mutator_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/config" | ||
| "github.com/databricks/cli/bundle/config/engine" | ||
| "github.com/databricks/cli/bundle/config/mutator" | ||
| "github.com/databricks/cli/bundle/config/resources" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestValidateDirectOnlyResourcesDirectEngineReturnsNil(t *testing.T) { | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Resources: config.Resources{ | ||
| Catalogs: map[string]*resources.Catalog{ | ||
| "my_catalog": {}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| diags := bundle.Apply(t.Context(), b, mutator.ValidateDirectOnlyResources(engine.EngineDirect)) | ||
| assert.Empty(t, diags) | ||
| } | ||
|
|
||
| func TestValidateDirectOnlyResourcesTerraformEngineNoDirectOnlyReturnsNil(t *testing.T) { | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Resources: config.Resources{ | ||
| Jobs: map[string]*resources.Job{ | ||
| "my_job": {}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| diags := bundle.Apply(t.Context(), b, mutator.ValidateDirectOnlyResources(engine.EngineTerraform)) | ||
| assert.Empty(t, diags) | ||
| } | ||
|
|
||
| func TestValidateDirectOnlyResourcesTerraformEngineDirectOnlyEmitsError(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| bundle *bundle.Bundle | ||
| expectedSummary string | ||
| expectedDetail string | ||
| }{ | ||
| { | ||
| name: "catalogs", | ||
| bundle: &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Resources: config.Resources{ | ||
| Catalogs: map[string]*resources.Catalog{ | ||
| "my_catalog": {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectedSummary: "Catalog resources are only supported with direct deployment mode", | ||
| expectedDetail: "Catalog resources require direct deployment mode. " + | ||
| "Please set the DATABRICKS_BUNDLE_ENGINE environment variable to 'direct' to use catalog resources.\n" + | ||
| "Learn more at https://docs.databricks.com/dev-tools/bundles/direct", | ||
| }, | ||
| { | ||
| name: "external_locations", | ||
| bundle: &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Resources: config.Resources{ | ||
| ExternalLocations: map[string]*resources.ExternalLocation{ | ||
| "my_location": {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectedSummary: "External Location resources are only supported with direct deployment mode", | ||
| expectedDetail: "External Location resources require direct deployment mode. " + | ||
| "Please set the DATABRICKS_BUNDLE_ENGINE environment variable to 'direct' to use external_location resources.\n" + | ||
| "Learn more at https://docs.databricks.com/dev-tools/bundles/direct", | ||
| }, | ||
| { | ||
| name: "vector_search_endpoints", | ||
| bundle: &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Resources: config.Resources{ | ||
| VectorSearchEndpoints: map[string]*resources.VectorSearchEndpoint{ | ||
| "my_endpoint": {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectedSummary: "Vector Search Endpoint resources are only supported with direct deployment mode", | ||
| expectedDetail: "Vector Search Endpoint resources require direct deployment mode. " + | ||
| "Please set the DATABRICKS_BUNDLE_ENGINE environment variable to 'direct' to use vector_search_endpoint resources.\n" + | ||
| "Learn more at https://docs.databricks.com/dev-tools/bundles/direct", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| diags := bundle.Apply(t.Context(), tc.bundle, mutator.ValidateDirectOnlyResources(engine.EngineTerraform)) | ||
| if assert.Len(t, diags, 1) { | ||
| assert.Equal(t, tc.expectedSummary, diags[0].Summary) | ||
| assert.Equal(t, tc.expectedDetail, diags[0].Detail) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package phases | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/databricks/cli/bundle/deployplan" | ||
| "github.com/databricks/cli/libs/cmdio" | ||
| ) | ||
|
|
||
| // approvalGroup describes one resource type that needs explicit user consent | ||
| // before a destructive action is applied. | ||
| type approvalGroup struct { | ||
| group string // matches config.GetResourceTypeFromKey, e.g. "schemas" | ||
| message string // banner shown above the action list | ||
| skipChildren bool // skip actions where IsChildResource() is true | ||
| trailingGap bool // print an empty line after the block (destroy only) | ||
| } | ||
|
|
||
| // logApprovalGroups filters actions per group and prints non-empty groups. | ||
| // Returns the total number of matched actions across all groups. | ||
| func logApprovalGroups(ctx context.Context, actions []deployplan.Action, groups []approvalGroup, types ...deployplan.ActionType) int { | ||
| total := 0 | ||
| for _, g := range groups { | ||
| matched := filterGroup(actions, g.group, types...) | ||
| if len(matched) == 0 { | ||
| continue | ||
| } | ||
| total += len(matched) | ||
| cmdio.LogString(ctx, g.message) | ||
| for _, a := range matched { | ||
| if g.skipChildren && a.IsChildResource() { | ||
| continue | ||
| } | ||
| cmdio.Log(ctx, a) | ||
| } | ||
| if g.trailingGap { | ||
| cmdio.LogString(ctx, "") | ||
| } | ||
| } | ||
| return total | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we make use of these two maps to automatically figure this out?
bundle/deploy/terraform/pkg.go:var GroupToTerraformName = map[string]string{
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perfect!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 6e6940d —
isDirectOnly(name)now checksdresources.SupportedResourcesminusterraform.GroupToTerraformName.