-
Notifications
You must be signed in to change notification settings - Fork 475
Fix workspace path normalization to prevent inconsistent plan errors #5130
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
Open
alexott
wants to merge
2
commits into
main
Choose a base branch
from
fix/issue-4672
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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 |
|---|---|---|
|
|
@@ -194,3 +194,30 @@ func PathListHash(v any) int { | |
| } | ||
| return c | ||
| } | ||
|
|
||
| // NormalizeWorkspacePath normalizes the path returned from the API to match the configured path. | ||
| // The Databricks API may add or remove the "/Workspace" prefix depending on the workspace configuration. | ||
| // This function ensures that the returned path matches the format used in the configuration to avoid | ||
| // Terraform detecting false changes. | ||
| // | ||
| // Examples: | ||
| // - If configured path is "/Users/..." and API returns "/Workspace/Users/...", it strips "/Workspace" | ||
| // - If configured path is "/Workspace/Users/..." and API returns "/Users/...", it adds "/Workspace" | ||
| func NormalizeWorkspacePath(configuredPath string, apiPath string) string { | ||
| if configuredPath == "" { | ||
| return apiPath | ||
| } | ||
|
|
||
| // Case 1: API added /Workspace prefix, but config doesn't have it | ||
| if strings.HasPrefix(apiPath, "/Workspace") && !strings.HasPrefix(configuredPath, "/Workspace") { | ||
| return strings.TrimPrefix(apiPath, "/Workspace") | ||
| } | ||
|
|
||
| // Case 2: Config has /Workspace prefix, but API doesn't have it | ||
| if !strings.HasPrefix(apiPath, "/Workspace") && strings.HasPrefix(configuredPath, "/Workspace") { | ||
| return "/Workspace" + apiPath | ||
| } | ||
|
Comment on lines
+211
to
+219
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In both of these cases, shouldn't we ask users to update the config with proper paths? |
||
|
|
||
| // No normalization needed | ||
| return apiPath | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -59,3 +59,68 @@ func TestFileContentSchemaClean(t *testing.T) { | |
| assert.True(t, d.HasError()) | ||
| assert.Equal(t, "Clean path required", d[0].Summary) | ||
| } | ||
|
|
||
| func TestNormalizeWorkspacePath(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| configuredPath string | ||
| apiPath string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "API adds /Workspace prefix - should strip it", | ||
| configuredPath: "/Users/[email protected]/notebook.py", | ||
| apiPath: "/Workspace/Users/[email protected]/notebook.py", | ||
| expected: "/Users/[email protected]/notebook.py", | ||
| }, | ||
| { | ||
| name: "Config has /Workspace prefix but API doesn't - should add it", | ||
| configuredPath: "/Workspace/Users/[email protected]/notebook.py", | ||
| apiPath: "/Users/[email protected]/notebook.py", | ||
| expected: "/Workspace/Users/[email protected]/notebook.py", | ||
| }, | ||
| { | ||
| name: "Both have /Workspace prefix - no change", | ||
| configuredPath: "/Workspace/Users/[email protected]/notebook.py", | ||
| apiPath: "/Workspace/Users/[email protected]/notebook.py", | ||
| expected: "/Workspace/Users/[email protected]/notebook.py", | ||
| }, | ||
| { | ||
| name: "Neither has /Workspace prefix - no change", | ||
| configuredPath: "/Users/[email protected]/notebook.py", | ||
| apiPath: "/Users/[email protected]/notebook.py", | ||
| expected: "/Users/[email protected]/notebook.py", | ||
| }, | ||
| { | ||
| name: "Empty configured path - return API path as-is", | ||
| configuredPath: "", | ||
| apiPath: "/Workspace/Users/[email protected]/notebook.py", | ||
| expected: "/Workspace/Users/[email protected]/notebook.py", | ||
| }, | ||
| { | ||
| name: "Directory path without /Workspace in config, with /Workspace in API", | ||
| configuredPath: "/Shared/test", | ||
| apiPath: "/Workspace/Shared/test", | ||
| expected: "/Shared/test", | ||
| }, | ||
| { | ||
| name: "Directory path with /Workspace in config, without /Workspace in API", | ||
| configuredPath: "/Workspace/Shared/test", | ||
| apiPath: "/Shared/test", | ||
| expected: "/Workspace/Shared/test", | ||
| }, | ||
| { | ||
| name: "Service principal path - API adds /Workspace", | ||
| configuredPath: "/Users/0b66cdac-04f8-408e-9290-13c058a2ebe1/file.py", | ||
| apiPath: "/Workspace/Users/0b66cdac-04f8-408e-9290-13c058a2ebe1/file.py", | ||
| expected: "/Users/0b66cdac-04f8-408e-9290-13c058a2ebe1/file.py", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| result := NormalizeWorkspacePath(tc.configuredPath, tc.apiPath) | ||
| assert.Equal(t, tc.expected, result) | ||
| }) | ||
| } | ||
| } | ||
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
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
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
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 |
|---|---|---|
|
|
@@ -309,3 +309,56 @@ func TestResourceWorkspaceFileUpdate(t *testing.T) { | |
| Update: true, | ||
| }.ApplyNoError(t) | ||
| } | ||
|
|
||
| func TestResourceWorkspaceFileRead_WorkspacePrefixNormalization(t *testing.T) { | ||
| objectID := int64(12345) | ||
| // Test case 1: Config without /Workspace prefix, API returns with prefix | ||
| qa.ResourceFixture{ | ||
| MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) { | ||
| w.GetMockWorkspaceAPI().EXPECT(). | ||
| GetStatusByPath(mock.Anything, "/Users/[email protected]/file.py"). | ||
| Return(&ws_api.ObjectInfo{ | ||
| ObjectId: objectID, | ||
| ObjectType: ws_api.ObjectTypeFile, | ||
| Path: "/Workspace/Users/[email protected]/file.py", | ||
| }, nil) | ||
| }, | ||
| Resource: ResourceWorkspaceFile(), | ||
| Read: true, | ||
| New: true, | ||
| ID: "/Users/[email protected]/file.py", | ||
| State: map[string]any{ | ||
| "path": "/Users/[email protected]/file.py", | ||
| }, | ||
| }.ApplyAndExpectData(t, map[string]any{ | ||
| "id": "/Users/[email protected]/file.py", | ||
| "path": "/Users/[email protected]/file.py", // Should match configured path | ||
| "workspace_path": "/Workspace/Users/[email protected]/file.py", | ||
| "object_id": int(objectID), | ||
| }) | ||
|
|
||
| // Test case 2: Config with /Workspace prefix, API returns without prefix | ||
| qa.ResourceFixture{ | ||
| MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) { | ||
| w.GetMockWorkspaceAPI().EXPECT(). | ||
| GetStatusByPath(mock.Anything, "/Workspace/Users/[email protected]/file.py"). | ||
| Return(&ws_api.ObjectInfo{ | ||
| ObjectId: objectID, | ||
| ObjectType: ws_api.ObjectTypeFile, | ||
| Path: "/Users/[email protected]/file.py", | ||
| }, nil) | ||
| }, | ||
| Resource: ResourceWorkspaceFile(), | ||
| Read: true, | ||
| New: true, | ||
| ID: "/Workspace/Users/[email protected]/file.py", | ||
| State: map[string]any{ | ||
| "path": "/Workspace/Users/[email protected]/file.py", | ||
| }, | ||
| }.ApplyAndExpectData(t, map[string]any{ | ||
| "id": "/Workspace/Users/[email protected]/file.py", | ||
| "path": "/Workspace/Users/[email protected]/file.py", // Should match configured path | ||
| "workspace_path": "/Workspace/Workspace/Users/[email protected]/file.py", | ||
| "object_id": int(objectID), | ||
| }) | ||
| } | ||
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.
Hi @alexott, do you know why this happens?
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.
I saw it in cases when user edit a job (even without changing anything)