Skip to content

Commit

Permalink
reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnester committed Oct 24, 2024
1 parent a51c573 commit 16f02c0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 62 deletions.
34 changes: 4 additions & 30 deletions bundle/config/validate/folder_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"context"
"fmt"
"path"
"strings"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/libraries"
"github.com/databricks/cli/bundle/paths"
"github.com/databricks/cli/bundle/permissions"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/databricks-sdk-go/apierr"
Expand All @@ -24,37 +23,12 @@ func (f *folderPermissions) Apply(ctx context.Context, b bundle.ReadOnlyBundle)
return nil
}

rootPath := b.Config().Workspace.RootPath
paths := []string{}
if !libraries.IsVolumesPath(rootPath) && !libraries.IsWorkspaceSharedPath(rootPath) {
paths = append(paths, rootPath)
}

if !strings.HasSuffix(rootPath, "/") {
rootPath += "/"
}

for _, p := range []string{
b.Config().Workspace.ArtifactPath,
b.Config().Workspace.FilePath,
b.Config().Workspace.StatePath,
b.Config().Workspace.ResourcePath,
} {
if libraries.IsWorkspaceSharedPath(p) || libraries.IsVolumesPath(p) {
continue
}

if strings.HasPrefix(p, rootPath) {
continue
}

paths = append(paths, p)
}
bundlePaths := paths.CollectUniquePaths(b.Config().Workspace)

var diags diag.Diagnostics
g, ctx := errgroup.WithContext(ctx)
results := make([]diag.Diagnostics, len(paths))
for i, p := range paths {
results := make([]diag.Diagnostics, len(bundlePaths))
for i, p := range bundlePaths {
g.Go(func() error {
results[i] = checkFolderPermission(ctx, b, p)
return nil
Expand Down
1 change: 1 addition & 0 deletions bundle/libraries/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func IsWorkspaceLibrary(library *compute.Library) bool {
}

// IsVolumesPath returns true if the specified path indicates that
// it should be interpreted as a Databricks Volumes path.
func IsVolumesPath(path string) bool {
return strings.HasPrefix(path, "/Volumes/")
}
Expand Down
39 changes: 39 additions & 0 deletions bundle/paths/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package paths

import (
"strings"

"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/libraries"
)

func CollectUniquePaths(workspace config.Workspace) []string {
rootPath := workspace.RootPath
paths := []string{}
if !libraries.IsVolumesPath(rootPath) && !libraries.IsWorkspaceSharedPath(rootPath) {
paths = append(paths, rootPath)
}

if !strings.HasSuffix(rootPath, "/") {
rootPath += "/"
}

for _, p := range []string{
workspace.ArtifactPath,
workspace.FilePath,
workspace.StatePath,
workspace.ResourcePath,
} {
if libraries.IsWorkspaceSharedPath(p) || libraries.IsVolumesPath(p) {
continue
}

if strings.HasPrefix(p, rootPath) {
continue
}

paths = append(paths, p)
}

return paths
}
35 changes: 3 additions & 32 deletions bundle/permissions/workspace_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package permissions
import (
"context"
"fmt"
"strings"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/libraries"
"github.com/databricks/cli/bundle/paths"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/databricks-sdk-go/service/workspace"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -55,38 +54,10 @@ func giveAccessForWorkspaceRoot(ctx context.Context, b *bundle.Bundle) error {
}

w := b.WorkspaceClient().Workspace
rootPath := b.Config.Workspace.RootPath
paths := []string{}
if !libraries.IsVolumesPath(rootPath) {
paths = append(paths, rootPath)
}

if !strings.HasSuffix(rootPath, "/") {
rootPath += "/"
}

if !strings.HasPrefix(b.Config.Workspace.ArtifactPath, rootPath) &&
!libraries.IsVolumesPath(b.Config.Workspace.ArtifactPath) {
paths = append(paths, b.Config.Workspace.ArtifactPath)
}

if !strings.HasPrefix(b.Config.Workspace.FilePath, rootPath) &&
!libraries.IsVolumesPath(b.Config.Workspace.FilePath) {
paths = append(paths, b.Config.Workspace.FilePath)
}

if !strings.HasPrefix(b.Config.Workspace.StatePath, rootPath) &&
!libraries.IsVolumesPath(b.Config.Workspace.StatePath) {
paths = append(paths, b.Config.Workspace.StatePath)
}

if !strings.HasPrefix(b.Config.Workspace.ResourcePath, rootPath) &&
!libraries.IsVolumesPath(b.Config.Workspace.ResourcePath) {
paths = append(paths, b.Config.Workspace.ResourcePath)
}
bundlePaths := paths.CollectUniquePaths(b.Config.Workspace)

g, ctx := errgroup.WithContext(ctx)
for _, p := range paths {
for _, p := range bundlePaths {
g.Go(func() error {
return setPermissions(ctx, w, p, permissions)
})
Expand Down

0 comments on commit 16f02c0

Please sign in to comment.