@@ -3,17 +3,17 @@ package resourcemutator
33import (
44 "context"
55 "fmt"
6+ "maps"
7+ "reflect"
68 "slices"
79 "strings"
810
911 "github.com/databricks/cli/bundle/permissions"
1012
1113 "github.com/databricks/cli/bundle"
12- "github.com/databricks/cli/bundle/config"
1314 "github.com/databricks/cli/bundle/config/resources"
1415 "github.com/databricks/cli/libs/diag"
1516 "github.com/databricks/cli/libs/logdiag"
16- "github.com/databricks/cli/libs/structs/structpath"
1717 "github.com/databricks/databricks-sdk-go/service/iam"
1818)
1919
@@ -108,44 +108,63 @@ func (m *bundlePermissions) Apply(ctx context.Context, b *bundle.Bundle) diag.Di
108108 return diag .FromErr (err )
109109 }
110110
111- b .Config .Resources .IterateAllPermissions (func (typ , name string , perms config.PermissionList ) bool {
112- lm , ok := levelsMap [typ ]
111+ for _ , group := range b .Config .Resources .AllResources () {
112+ key := group .Description .PluralName
113+ lm , ok := levelsMap [key ]
113114 if ! ok {
114- return true
115+ continue
115116 }
116- err = applyToResource (ctx , b , typ , name , lm , perms )
117- return err == nil
118- })
119- return diag .FromErr (err )
120- }
121117
122- // applyToResource applies the bundle permissions to the permissions perms of the
123- // resource name of type typ, using the permission levels lm of that type.
124- func applyToResource (ctx context.Context , b * bundle.Bundle , typ , name string , lm map [string ]string , perms config.PermissionList ) error {
125- path := structpath .NewPath (nil , "resources" , typ , name , "permissions" )
126- // Present as [structvar.View] decides: a non-nil list or a pure reference.
127- had := ! perms .IsNil () || b .Config .IsReferenceAt (path )
128-
129- existing := make ([]resources.Permission , 0 , perms .Len ())
130- for i := range perms .Len () {
131- existing = append (existing , perms .At (i ))
118+ for _ , name := range slices .Sorted (maps .Keys (group .Resources )) {
119+ field := reflect .ValueOf (group .Resources [name ]).Elem ().FieldByName ("Permissions" )
120+ permissions := readPermissions (field )
121+ permissions = append (permissions , convertPermissions (
122+ ctx ,
123+ b .Config .Permissions ,
124+ permissions ,
125+ key ,
126+ lm ,
127+ )... )
128+ writePermissions (field , permissions )
129+ }
132130 }
133131
134- added := convertPermissions (ctx , b .Config .Permissions , existing , typ , lm )
135- for _ , p := range added {
136- perms .Append (p )
132+ return nil
133+ }
134+
135+ // readPermissions converts the permissions field of a resource to []resources.Permission.
136+ // The element type of the field differs between resources, but all have the same fields.
137+ func readPermissions (field reflect.Value ) []resources.Permission {
138+ var permissions []resources.Permission
139+ for i := range field .Len () {
140+ p := field .Index (i )
141+ permissions = append (permissions , resources.Permission {
142+ Level : iam .PermissionLevel (p .FieldByName ("Level" ).String ()),
143+ UserName : p .FieldByName ("UserName" ).String (),
144+ ServicePrincipalName : p .FieldByName ("ServicePrincipalName" ).String (),
145+ GroupName : p .FieldByName ("GroupName" ).String (),
146+ })
137147 }
148+ return permissions
149+ }
138150
139- if ! had {
140- return nil
151+ // writePermissions sets the permissions field of a resource to permissions.
152+ // An empty list is stored as nil, so that the field is absent from the configuration.
153+ func writePermissions (field reflect.Value , permissions []resources.Permission ) {
154+ if len (permissions ) == 0 {
155+ field .SetZero ()
156+ return
141157 }
142158
143- // Empty permissions are dropped. Otherwise they are rebuilt without locations.
144- if len (existing ) == 0 && len (added ) == 0 {
145- return b .Config .Delete (path )
159+ out := reflect .MakeSlice (field .Type (), len (permissions ), len (permissions ))
160+ for i , p := range permissions {
161+ e := out .Index (i )
162+ e .FieldByName ("Level" ).SetString (string (p .Level ))
163+ e .FieldByName ("UserName" ).SetString (p .UserName )
164+ e .FieldByName ("ServicePrincipalName" ).SetString (p .ServicePrincipalName )
165+ e .FieldByName ("GroupName" ).SetString (p .GroupName )
146166 }
147- b .Config .SetLocations (path , nil )
148- return nil
167+ field .Set (out )
149168}
150169
151170func validatePermissions (b * bundle.Bundle ) error {
0 commit comments