2023-11-13 11:29:40 +00:00
|
|
|
package permissions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-11-13 11:29:40 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/workspace"
|
|
|
|
)
|
|
|
|
|
|
|
|
type workspaceRootPermissions struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func ApplyWorkspaceRootPermissions() bundle.Mutator {
|
|
|
|
return &workspaceRootPermissions{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply implements bundle.Mutator.
|
2024-03-25 14:18:47 +00:00
|
|
|
func (*workspaceRootPermissions) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2023-11-13 11:29:40 +00:00
|
|
|
err := giveAccessForWorkspaceRoot(ctx, b)
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-11-13 11:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*workspaceRootPermissions) Name() string {
|
|
|
|
return "ApplyWorkspaceRootPermissions"
|
|
|
|
}
|
|
|
|
|
|
|
|
func giveAccessForWorkspaceRoot(ctx context.Context, b *bundle.Bundle) error {
|
|
|
|
permissions := make([]workspace.WorkspaceObjectAccessControlRequest, 0)
|
|
|
|
|
|
|
|
for _, p := range b.Config.Permissions {
|
2024-10-10 13:16:06 +00:00
|
|
|
level, err := GetWorkspaceObjectPermissionLevel(p.Level)
|
2023-11-13 11:29:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
permissions = append(permissions, workspace.WorkspaceObjectAccessControlRequest{
|
|
|
|
GroupName: p.GroupName,
|
|
|
|
UserName: p.UserName,
|
|
|
|
ServicePrincipalName: p.ServicePrincipalName,
|
|
|
|
PermissionLevel: level,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(permissions) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
w := b.WorkspaceClient().Workspace
|
|
|
|
obj, err := w.GetStatusByPath(ctx, b.Config.Workspace.RootPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.UpdatePermissions(ctx, workspace.WorkspaceObjectPermissionsRequest{
|
|
|
|
WorkspaceObjectId: fmt.Sprint(obj.ObjectId),
|
|
|
|
WorkspaceObjectType: "directories",
|
|
|
|
AccessControlList: permissions,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-10 13:16:06 +00:00
|
|
|
func GetWorkspaceObjectPermissionLevel(bundlePermission string) (workspace.WorkspaceObjectPermissionLevel, error) {
|
2023-11-13 11:29:40 +00:00
|
|
|
switch bundlePermission {
|
|
|
|
case CAN_MANAGE:
|
|
|
|
return workspace.WorkspaceObjectPermissionLevelCanManage, nil
|
|
|
|
case CAN_RUN:
|
|
|
|
return workspace.WorkspaceObjectPermissionLevelCanRun, nil
|
|
|
|
case CAN_VIEW:
|
|
|
|
return workspace.WorkspaceObjectPermissionLevelCanRead, nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("unsupported bundle permission level %s", bundlePermission)
|
|
|
|
}
|
|
|
|
}
|