Skip to content

Commit

Permalink
fix: EventAccountFilter only allows up to 10 items
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreZiviani committed Jan 26, 2024
1 parent 92419a8 commit d5b0070
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions exporter/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,38 @@ func (m Metrics) getEventDetailsForOrg(ctx context.Context, event healthTypes.Or
}

func (m Metrics) getAffectedEntitiesForOrg(ctx context.Context, event healthTypes.OrganizationEvent, enrichedEvent *HealthEvent) {
var pagResources *health.DescribeAffectedEntitiesForOrganizationPaginator
pagResources := make([]*health.DescribeAffectedEntitiesForOrganizationPaginator, 0)
if len(enrichedEvent.AffectedAccounts) > 0 {
accountFilter := make([]healthTypes.EventAccountFilter, len(enrichedEvent.AffectedAccounts))
for i, account := range enrichedEvent.AffectedAccounts {
accountFilter[i] = healthTypes.EventAccountFilter{EventArn: event.Arn, AwsAccountId: &account}
affectedAccountsSlices := m.splitSlice(enrichedEvent.AffectedAccounts, 10)
for _, slice := range affectedAccountsSlices {
accountFilter := make([]healthTypes.EventAccountFilter, len(slice))
for i, account := range slice {
accountFilter[i] = healthTypes.EventAccountFilter{EventArn: event.Arn, AwsAccountId: &account}
}

pagResources = append(pagResources, health.NewDescribeAffectedEntitiesForOrganizationPaginator(
m.health,
&health.DescribeAffectedEntitiesForOrganizationInput{OrganizationEntityFilters: accountFilter},
),
)
}

pagResources = health.NewDescribeAffectedEntitiesForOrganizationPaginator(
m.health,
&health.DescribeAffectedEntitiesForOrganizationInput{OrganizationEntityFilters: accountFilter})
} else {
pagResources = health.NewDescribeAffectedEntitiesForOrganizationPaginator(
pagResources = append(pagResources, health.NewDescribeAffectedEntitiesForOrganizationPaginator(
m.health,
&health.DescribeAffectedEntitiesForOrganizationInput{OrganizationEntityFilters: []healthTypes.EventAccountFilter{{EventArn: event.Arn}}})
&health.DescribeAffectedEntitiesForOrganizationInput{OrganizationEntityFilters: []healthTypes.EventAccountFilter{{EventArn: event.Arn}}},
),
)
}

for pagResources.HasMorePages() {
resources, err := pagResources.NextPage(ctx)
if err != nil {
panic(err.Error())
}
for _, slices := range pagResources {
for slices.HasMorePages() {
resources, err := slices.NextPage(ctx)
if err != nil {
panic(err.Error())
}

enrichedEvent.AffectedResources = append(enrichedEvent.AffectedResources, resources.Entities...)
enrichedEvent.AffectedResources = append(enrichedEvent.AffectedResources, resources.Entities...)
}
}
}

Expand Down Expand Up @@ -149,3 +158,13 @@ func (m Metrics) getAccountsNameFromIds(ids []string) []string {

return names
}

func (m Metrics) splitSlice(slice []string, batchSize int) [][]string {
batches := make([][]string, 0, (len(slice)+batchSize-1)/batchSize)
for batchSize < len(slice) {
slice, batches = slice[batchSize:], append(batches, slice[0:batchSize:batchSize])
}
batches = append(batches, slice)

return batches
}

0 comments on commit d5b0070

Please sign in to comment.