-
Notifications
You must be signed in to change notification settings - Fork 129
Config remote sync command #4289
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
Draft
ilyakuz-db
wants to merge
10
commits into
main
Choose a base branch
from
config-remote-sync-command
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.
+1,669
−0
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
70ee529
Command placeholder
ilyakuz-db b9d9afc
First iteration of YAML generation
ilyakuz-db ef55090
File writer
ilyakuz-db 1bf39c9
Target overrides
ilyakuz-db afc2e87
Cleanup
ilyakuz-db beba54d
Fix invalid Dyn panic
ilyakuz-db d8fac50
Fix test to use structs
ilyakuz-db e0bb6b1
Cleanup
ilyakuz-db 02be4c1
Fix missing tags issue
ilyakuz-db ee2564d
Fix sequences
ilyakuz-db 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package configsync | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/deployplan" | ||
| "github.com/databricks/cli/bundle/direct" | ||
| "github.com/databricks/cli/libs/log" | ||
| ) | ||
|
|
||
| // DetectChanges compares current remote state with the last deployed state | ||
| // and returns a map of resource changes. | ||
| func DetectChanges(ctx context.Context, b *bundle.Bundle) (map[string]deployplan.Changes, error) { | ||
| changes := make(map[string]deployplan.Changes) | ||
|
|
||
| deployBundle := &direct.DeploymentBundle{} | ||
| // TODO: for Terraform engine we should read the state file, converted to direct state format, it should be created during deployment | ||
| _, statePath := b.StateFilenameDirect(ctx) | ||
|
|
||
| plan, err := deployBundle.CalculatePlan(ctx, b.WorkspaceClient(), &b.Config, statePath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to calculate plan: %w", err) | ||
| } | ||
|
|
||
| for resourceKey, entry := range plan.Plan { | ||
| resourceChanges := make(deployplan.Changes) | ||
|
|
||
| if entry.Changes != nil { | ||
| for path, changeDesc := range entry.Changes { | ||
| if changeDesc.Remote != nil && changeDesc.Action != deployplan.Skip { | ||
| resourceChanges[path] = changeDesc | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(resourceChanges) != 0 { | ||
| changes[resourceKey] = resourceChanges | ||
| } | ||
|
|
||
| log.Debugf(ctx, "Resource %s has %d changes", resourceKey, len(resourceChanges)) | ||
| } | ||
|
|
||
| return changes, nil | ||
| } |
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,30 @@ | ||
| package configsync | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/cli/bundle/deployplan" | ||
| ) | ||
|
|
||
| // FormatTextOutput formats the config changes as human-readable text. Useful for debugging | ||
| func FormatTextOutput(changes map[string]deployplan.Changes) string { | ||
| var output strings.Builder | ||
|
|
||
| if len(changes) == 0 { | ||
| output.WriteString("No changes detected.\n") | ||
| return output.String() | ||
| } | ||
|
|
||
| output.WriteString(fmt.Sprintf("Detected changes in %d resource(s):\n\n", len(changes))) | ||
|
|
||
| for resourceKey, resourceChanges := range changes { | ||
| output.WriteString(fmt.Sprintf("Resource: %s\n", resourceKey)) | ||
|
|
||
| for path, changeDesc := range resourceChanges { | ||
| output.WriteString(fmt.Sprintf(" %s: %s\n", path, changeDesc.Action)) | ||
| } | ||
| } | ||
|
|
||
| return output.String() | ||
| } |
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,39 @@ | ||
| package configsync | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/deployplan" | ||
| ) | ||
|
|
||
| // FileChange represents a change to a bundle configuration file | ||
| type FileChange struct { | ||
| Path string `json:"path"` | ||
| OriginalContent string `json:"originalContent"` | ||
| ModifiedContent string `json:"modifiedContent"` | ||
| } | ||
|
|
||
| // DiffOutput represents the complete output of the config-remote-sync command | ||
| type DiffOutput struct { | ||
| Files []FileChange `json:"files"` | ||
| Changes map[string]deployplan.Changes `json:"changes"` | ||
| } | ||
|
|
||
| // SaveFiles writes all file changes to disk. | ||
| func SaveFiles(ctx context.Context, b *bundle.Bundle, files []FileChange) error { | ||
| for _, file := range files { | ||
| err := os.MkdirAll(filepath.Dir(file.Path), 0o755) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = os.WriteFile(file.Path, []byte(file.ModifiedContent), 0o644) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
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,89 @@ | ||
| package configsync | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSaveFiles_Success(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| tmpDir := t.TempDir() | ||
|
|
||
| yamlPath := filepath.Join(tmpDir, "subdir", "databricks.yml") | ||
| modifiedContent := `resources: | ||
| jobs: | ||
| test_job: | ||
| name: "Updated Job" | ||
| timeout_seconds: 7200 | ||
| ` | ||
|
|
||
| files := []FileChange{ | ||
| { | ||
| Path: yamlPath, | ||
| OriginalContent: "original content", | ||
| ModifiedContent: modifiedContent, | ||
| }, | ||
| } | ||
|
|
||
| err := SaveFiles(ctx, &bundle.Bundle{}, files) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = os.Stat(yamlPath) | ||
| require.NoError(t, err) | ||
|
|
||
| content, err := os.ReadFile(yamlPath) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, modifiedContent, string(content)) | ||
|
|
||
| _, err = os.Stat(filepath.Dir(yamlPath)) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestSaveFiles_MultipleFiles(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| tmpDir := t.TempDir() | ||
|
|
||
| file1Path := filepath.Join(tmpDir, "file1.yml") | ||
| file2Path := filepath.Join(tmpDir, "subdir", "file2.yml") | ||
| content1 := "content for file 1" | ||
| content2 := "content for file 2" | ||
|
|
||
| files := []FileChange{ | ||
| { | ||
| Path: file1Path, | ||
| OriginalContent: "original 1", | ||
| ModifiedContent: content1, | ||
| }, | ||
| { | ||
| Path: file2Path, | ||
| OriginalContent: "original 2", | ||
| ModifiedContent: content2, | ||
| }, | ||
| } | ||
|
|
||
| err := SaveFiles(ctx, &bundle.Bundle{}, files) | ||
| require.NoError(t, err) | ||
|
|
||
| content, err := os.ReadFile(file1Path) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, content1, string(content)) | ||
|
|
||
| content, err = os.ReadFile(file2Path) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, content2, string(content)) | ||
| } | ||
|
|
||
| func TestSaveFiles_EmptyList(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| err := SaveFiles(ctx, &bundle.Bundle{}, []FileChange{}) | ||
| require.NoError(t, err) | ||
| } |
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,55 @@ | ||
| package configsync | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/databricks/cli/libs/dyn" | ||
| ) | ||
|
|
||
| // ensurePathExists ensures all intermediate nodes exist in the path. | ||
| // It creates empty maps for missing intermediate map keys. | ||
| // For sequences, it creates empty sequences with empty map elements when needed. | ||
| // Returns the modified value with all intermediate nodes guaranteed to exist. | ||
| func ensurePathExists(v dyn.Value, path dyn.Path) (dyn.Value, error) { | ||
| if len(path) == 0 { | ||
| return v, nil | ||
| } | ||
|
|
||
| result := v | ||
| for i := 1; i < len(path); i++ { | ||
| prefixPath := path[:i] | ||
| component := path[i-1] | ||
|
|
||
| item, _ := dyn.GetByPath(result, prefixPath) | ||
| if !item.IsValid() { | ||
| if component.Key() != "" { | ||
| key := path[i].Key() | ||
| isIndex := key == "" | ||
| isKey := key != "" | ||
|
|
||
| if i < len(path) && isIndex { | ||
| index := path[i].Index() | ||
| seq := make([]dyn.Value, index+1) | ||
| for j := range seq { | ||
| seq[j] = dyn.V(dyn.NewMapping()) | ||
| } | ||
| var err error | ||
| result, err = dyn.SetByPath(result, prefixPath, dyn.V(seq)) | ||
| if err != nil { | ||
| return dyn.InvalidValue, fmt.Errorf("failed to create sequence at path %s: %w", prefixPath, err) | ||
| } | ||
| } else if isKey { | ||
| var err error | ||
| result, err = dyn.SetByPath(result, prefixPath, dyn.V(dyn.NewMapping())) | ||
| if err != nil { | ||
| return dyn.InvalidValue, fmt.Errorf("failed to create intermediate path %s: %w", prefixPath, err) | ||
| } | ||
| } | ||
| } else { | ||
| return dyn.InvalidValue, fmt.Errorf("sequence index does not exist at path %s", prefixPath) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result, nil | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Q: Does it make sense to use
yamlsaverhere? Not sure what the benefits would be