@@ -21,118 +21,69 @@ type enterpriseRoleResourceType struct {
2121 client * github.Client
2222 customClient * customclient.Client
2323 enterprises []string
24- userRolesCache map [string ][]string
24+ roleUsersCache map [string ][]string
2525 mu * sync.Mutex
2626}
2727
28- // func enterpriseRoleResource(
29- // ctx context.Context,
30- // role *OrganizationRole,
31- // org *v2.Resource,
32- // ) (*v2.Resource, error) {
33- // profile := map[string]interface{}{
34- // "description": role.Description,
35- // }
36- //
37- // return resource.NewRoleResource(
38- // role.Name,
39- // resourceTypeOrgRole,
40- // role.ID,
41- // []resource.RoleTraitOption{
42- // resource.WithRoleProfile(profile),
43- // },
44- // resource.WithParentResourceID(org.Id),
45- // resource.WithAnnotation(
46- // &v2.V1Identifier{Id: fmt.Sprintf("org_role:%d", role.ID)},
47- // ),
48- // )
49- // }
50-
5128func (o * enterpriseRoleResourceType ) ResourceType (_ context.Context ) * v2.ResourceType {
5229 return o .resourceType
5330}
5431
5532func (o * enterpriseRoleResourceType ) cacheRole (roleId string , userLogin string ) {
5633 o .mu .Lock ()
5734 defer o .mu .Unlock ()
58- if _ , exists := o .userRolesCache [userLogin ]; ! exists {
59- o .userRolesCache [roleId ] = []string {}
35+ if _ , exists := o .roleUsersCache [userLogin ]; ! exists {
36+ o .roleUsersCache [roleId ] = []string {}
6037 }
6138
62- o .userRolesCache [roleId ] = append (o .userRolesCache [ userLogin ], userLogin )
39+ o .roleUsersCache [roleId ] = append (o .roleUsersCache [ roleId ], userLogin )
6340}
6441
65- func (o * enterpriseRoleResourceType ) fillCache () error {
66- for _ , enterprise := range o .enterprises {
67- consumedLicenses , _ , err := o .customClient .ListEnterpriseConsumedLicenses (context .Background (), enterprise )
68- if err != nil {
69- return fmt .Errorf ("baton-github: error listing enterprise consumed licenses for %s: %w" , enterprise , err )
70- }
71-
72- for _ , user := range consumedLicenses .Users {
73- for _ , role := range user .GitHubComEnterpriseRoles {
74- roleId := fmt .Sprintf ("%s:%s" , enterprise , role )
75- o .cacheRole (roleId , user .GitHubComLogin )
76- }
77- }
78- }
79- return nil
80- }
81-
82- func (o * enterpriseRoleResourceType ) getRolesCache () (map [string ][]string , error ) {
83- if len (o .userRolesCache ) == 0 {
84- if err := o .fillCache (); err != nil {
42+ func (o * enterpriseRoleResourceType ) getRoleUsersCache (ctx context.Context ) (map [string ][]string , error ) {
43+ if len (o .roleUsersCache ) == 0 {
44+ if err := o .fillCache (ctx ); err != nil {
8545 return nil , fmt .Errorf ("baton-github: error caching user roles: %w" , err )
8646 }
8747 }
8848
8949 o .mu .Lock ()
9050 defer o .mu .Unlock ()
91- return o .userRolesCache , nil
51+ return o .roleUsersCache , nil
9252}
9353
94- // func (o *enterpriseRoleResourceType) List(
95- // ctx context.Context,
96- // parentID *v2.ResourceId,
97- // pToken *pagination.Token,
98- // ) ([]*v2.Resource, string, annotations.Annotations, error) {
99- //
100- // var ret []*v2.Resource
101- // for _, enterprise := range o.enterprises {
102- // consumedLicenses, _, err := o.customClient.ListEnterpriseConsumedLicenses(ctx, enterprise)
103- // if err != nil {
104- // return nil, "", nil, fmt.Errorf("baton-github: error listing enterprise consumed licenses for %s: %w", enterprise, err)
105- // }
106- //
107- // for _, user := range consumedLicenses.Users {
108- // for _, role := range user.GitHubComEnterpriseRoles {
109- // roleId := fmt.Sprintf("%s:%s", enterprise, role)
110- // o.cacheRole(roleId, user.GitHubComLogin)
111- //
112- // roleResource, err := resourceSdk.NewRoleResource(
113- // role,
114- // resourceTypeEnterpriseRole,
115- // roleId,
116- // []resourceSdk.RoleTraitOption{},
117- // )
118- // if err != nil {
119- // return nil, "", nil, fmt.Errorf("baton-github: error creating role resource for %s in enterprise %s: %w", role, enterprise, err)
120- // }
121- // ret = append(ret, roleResource)
122- // }
123- // }
124- // }
125- //
126- // return ret, "", nil, nil
127- // }
54+ func (o * enterpriseRoleResourceType ) fillCache (ctx context.Context ) error {
55+ for _ , enterprise := range o .enterprises {
56+ page := 0
57+ continuePagination := true
58+ for continuePagination {
59+ consumedLicenses , _ , err := o .customClient .ListEnterpriseConsumedLicenses (ctx , enterprise , page )
60+ if err != nil {
61+ return fmt .Errorf ("baton-github: error listing enterprise consumed licenses for %s: %w" , enterprise , err )
62+ }
63+
64+ if len (consumedLicenses .Users ) == 0 {
65+ continuePagination = false
66+ }
67+ page ++
68+
69+ for _ , user := range consumedLicenses .Users {
70+ for _ , role := range user .GitHubComEnterpriseRoles {
71+ roleId := fmt .Sprintf ("%s:%s" , enterprise , role )
72+ o .cacheRole (roleId , user .GitHubComLogin )
73+ }
74+ }
75+ }
76+ }
77+ return nil
78+ }
12879
12980func (o * enterpriseRoleResourceType ) List (
13081 ctx context.Context ,
13182 parentID * v2.ResourceId ,
13283 pToken * pagination.Token ,
13384) ([]* v2.Resource , string , annotations.Annotations , error ) {
13485 var ret []* v2.Resource
135- cache , err := o .getRolesCache ( )
86+ cache , err := o .getRoleUsersCache ( ctx )
13687 if err != nil {
13788 return nil , "" , nil , fmt .Errorf ("baton-github: error getting user roles cache: %w" , err )
13889 }
@@ -179,18 +130,11 @@ func (o *enterpriseRoleResourceType) Grants(
179130 resource * v2.Resource ,
180131 pToken * pagination.Token ,
181132) ([]* v2.Grant , string , annotations.Annotations , error ) {
182- _ , err := o .getRolesCache ()
183- if err != nil {
184- return nil , "" , nil , fmt .Errorf ("baton-github: error getting enterprise role cache: %w" , err )
185- }
186-
187- cache , err := o .getRolesCache ()
133+ cache , err := o .getRoleUsersCache (ctx )
188134 if err != nil {
189135 return nil , "" , nil , fmt .Errorf ("baton-github: error getting user roles cache: %w" , err )
190136 }
191137
192- // __AUTO_GENERATED_PRINT_VAR_START__
193- fmt .Println (fmt .Sprintf ("Grants resource.Id.Resource: %+v" , resource .Id .Resource )) // __AUTO_GENERATED_PRINT_VAR_END__
194138 ret := []* v2.Grant {}
195139 for _ , userLogin := range cache [resource .Id .Resource ] {
196140 user , _ , err := o .client .Users .Get (ctx , userLogin )
@@ -213,21 +157,13 @@ func (o *enterpriseRoleResourceType) Grants(
213157 return ret , "" , nil , nil
214158}
215159
216- // func (o *enterpriseRoleResourceType) Grant(ctx context.Context, principal *v2.Resource, entitlement *v2.Entitlement) (annotations.Annotations, error) {
217- // return nil, nil
218- // }
219- //
220- // func (o *enterpriseRoleResourceType) Revoke(ctx context.Context, grant *v2.Grant) (annotations.Annotations, error) {
221- // return nil, nil
222- // }
223-
224160func enterpriseRoleBuilder (client * github.Client , customClient * customclient.Client , enterprises []string ) * enterpriseRoleResourceType {
225161 return & enterpriseRoleResourceType {
226162 resourceType : resourceTypeEnterpriseRole ,
227163 client : client ,
228164 customClient : customClient ,
229165 enterprises : enterprises ,
230- userRolesCache : make (map [string ][]string ),
166+ roleUsersCache : make (map [string ][]string ),
231167 mu : & sync.Mutex {},
232168 }
233169}
0 commit comments