@@ -23,6 +23,10 @@ import (
2323 "google.golang.org/protobuf/proto"
2424)
2525
26+ const projectsCountProfileKey = "projects_count"
27+ const groupMembersCountProfileKey = "group_members_count"
28+ const descendantGroupsCountProfileKey = "descendant_groups_count"
29+
2630type groupBuilder struct {
2731 client * client.GitlabClient
2832}
@@ -61,12 +65,31 @@ func (o *groupBuilder) List(ctx context.Context, parentResourceID *v2.ResourceId
6165 if err != nil {
6266 return nil , "" , outputAnnotations , err
6367 }
68+ groupIds := make ([]string , 0 )
69+ for _ , g := range groups {
70+ gId := gitlabFullGroupID (g .ID )
71+ groupIds = append (groupIds , gId )
72+ }
73+
74+ groupsWithCounts , rateLimitDesc , err := o .client .ListGroupsWithCounts (ctx , groupIds )
75+ if rateLimitDesc != nil {
76+ outputAnnotations .WithRateLimiting (rateLimitDesc )
77+ }
78+ if err != nil {
79+ return nil , "" , outputAnnotations , err
80+ }
81+
82+ groupCountMap := make (map [string ]* client.GroupWithCount )
83+ for _ , gc := range groupsWithCounts {
84+ groupCountMap [gc .Id ] = & gc
85+ }
6486
6587 outResources := make ([]* v2.Resource , 0 , len (groups ))
6688 for _ , group := range groups {
6789 parentResourceID = getParentGroup (group .ParentID )
68-
69- resource , err := groupResource (group , parentResourceID , o .client .IsOnPremise )
90+ fullGroupID := gitlabFullGroupID (group .ID )
91+ groupCounts := groupCountMap [fullGroupID ]
92+ resource , err := groupResource (ctx , group , parentResourceID , o .client .IsOnPremise , groupCounts )
7093 if err != nil {
7194 return nil , "" , outputAnnotations , err
7295 }
@@ -116,6 +139,16 @@ func (o *groupBuilder) Entitlements(_ context.Context, resource *v2.Resource, _
116139}
117140
118141func (o * groupBuilder ) Grants (ctx context.Context , resource * v2.Resource , pToken * pagination.Token ) ([]* v2.Grant , string , annotations.Annotations , error ) {
142+ groupTrait , err := resourceSdk .GetGroupTrait (resource )
143+ if err != nil {
144+ return nil , "" , nil , fmt .Errorf ("okta-connectorv2: failed to get group trait: %w" , err )
145+ }
146+ groupMembersCount , ok := resourceSdk .GetProfileInt64Value (groupTrait .Profile , groupMembersCountProfileKey )
147+
148+ if ok && groupMembersCount == 0 {
149+ return nil , "" , nil , nil
150+ }
151+
119152 var outGrants []* v2.Grant
120153 var outputAnnotations = annotations .New ()
121154 var users []* client.GroupMember
@@ -270,7 +303,7 @@ func (o *groupBuilder) Revoke(ctx context.Context, grant *v2.Grant) (annotations
270303 return outputAnnotations , nil
271304}
272305
273- func groupResource (group * client.Group , parentResourceID * v2.ResourceId , isOnPremise bool ) (* v2.Resource , error ) {
306+ func groupResource (ctx context. Context , group * client.Group , parentResourceID * v2.ResourceId , isOnPremise bool , groupCounts * client. GroupWithCount ) (* v2.Resource , error ) {
274307 profile := map [string ]interface {}{
275308 "id" : group .ID ,
276309 "name" : group .Name ,
@@ -288,8 +321,16 @@ func groupResource(group *client.Group, parentResourceID *v2.ResourceId, isOnPre
288321 profile ["parent_group_id" ] = group .ParentID
289322 }
290323
324+ hasProjects := true
325+ if groupCounts != nil {
326+ hasProjects = groupCounts .ProjectsCount > 0
327+ profile ["projects_count" ] = groupCounts .ProjectsCount
328+ profile ["group_members_count" ] = groupCounts .GroupMembersCount
329+ profile ["descendant_groups_count" ] = groupCounts .DescendantGroupsCount
330+ }
331+
291332 annos := make ([]proto.Message , 0 )
292- if parentResourceID == nil {
333+ if parentResourceID == nil && hasProjects {
293334 annos = append (annos , & v2.ChildResourceType {ResourceTypeId : projectResourceType .Id })
294335 }
295336
0 commit comments