func()

in x-pack/metricbeat/module/aws/awshealth/awshealth.go [146:321]


func (m *MetricSet) getEventDetails(
	ctx context.Context,
	awsHealth *health.Client,
) []mb.Event {
	// Define event filter to fetch only upcoming and open events
	eventFilter := types.EventFilter{
		EventStatusCodes: []types.EventStatusCode{
			types.EventStatusCodeUpcoming,
			types.EventStatusCodeOpen,
		},
	}
	var (
		deEvents          []types.Event
		affPage           health.DescribeAffectedEntitiesPaginator
		healthDetails     []AWSHealthMetric
		healthDetailsTemp []AWSHealthMetric
		affEntityTemp     AffectedEntityDetails
		affInputParams    health.DescribeAffectedEntitiesInput
	)

	// Create an instance of DescribeEventsInput with desired parameters
	deInputParams := health.DescribeEventsInput{
		Filter: &eventFilter,
	}

	// Define options for DescribeEventsPaginator
	deOptions := &health.DescribeEventAggregatesPaginatorOptions{
		Limit:                10,
		StopOnDuplicateToken: true,
	}

	// Function option to apply options to the paginator
	deOptFn := func(options *health.DescribeEventsPaginatorOptions) {
		// Apply the provided options
		options.Limit = deOptions.Limit
		options.StopOnDuplicateToken = deOptions.StopOnDuplicateToken
	}
	// Define options for DescribeAffectedEntitiesPaginator
	affOptions := &health.DescribeAffectedEntitiesPaginatorOptions{
		Limit:                10,
		StopOnDuplicateToken: true,
	}
	// Function option to apply options to the paginator
	affOptFn := func(options *health.DescribeAffectedEntitiesPaginatorOptions) {
		// Apply the provided options
		options.Limit = affOptions.Limit
		options.StopOnDuplicateToken = affOptions.StopOnDuplicateToken
	}
	// Create DescribeEventsPaginator with AWS Health client and options
	dePage := health.NewDescribeEventsPaginator(awsHealth, &deInputParams, deOptFn)

	for dePage.HasMorePages() {
		healthDetailsTemp = []AWSHealthMetric{}

		// Perform actions for the current page
		currentPage, err := dePage.NextPage(ctx)
		if err != nil {
			m.Logger().Errorf("[AWS Health] DescribeEvents failed with : %w", err)
			break
		}
		deEvents = currentPage.Events
		eventArns := make([]string, len(deEvents))
		// Iterate through events to extract relevant information
		for i, de := range deEvents {
			healthDetailsTemp = append(healthDetailsTemp, AWSHealthMetric{
				EventArn:          awssdk.ToString(de.Arn),
				EndTime:           awssdk.ToTime(de.EndTime),
				EventScopeCode:    string(de.EventScopeCode),
				EventTypeCategory: string(de.EventTypeCategory),
				EventTypeCode:     awssdk.ToString(de.EventTypeCode),
				LastUpdatedTime:   awssdk.ToTime(de.LastUpdatedTime),
				Region:            awssdk.ToString(de.Region),
				Service:           awssdk.ToString(de.Service),
				StartTime:         awssdk.ToTime(de.StartTime),
				StatusCode:        string(de.StatusCode),
			})
			eventArns[i] = awssdk.ToString(de.Arn)
		}
		// Fetch event details for the current page of events
		eventDetails, err := awsHealth.DescribeEventDetails(ctx, &health.DescribeEventDetailsInput{
			EventArns: eventArns,
			Locale:    &locale,
		})
		if err != nil {
			m.Logger().Errorf("[AWS Health] DescribeEventDetails failed with : %w", err)
			break
		}
		// Fetch event description for the current page of events
		successSet := eventDetails.SuccessfulSet
		for x := range successSet {
			for y := range healthDetailsTemp {
				if awssdk.ToString(successSet[x].Event.Arn) == healthDetailsTemp[y].EventArn {
					healthDetailsTemp[y].EventDescription = awssdk.ToString(successSet[x].EventDescription.LatestDescription)
				}
			}
		}
		// Fetch affected entities related to event ARNs in the current page
		affInputParams = health.DescribeAffectedEntitiesInput{
			Filter: &types.EntityFilter{
				EventArns: eventArns,
			},
		}
		affPage = *health.NewDescribeAffectedEntitiesPaginator(
			awsHealth,
			&affInputParams,
			affOptFn,
		)

		for affPage.HasMorePages() {
			// Fetch current page of affected entities
			affCurrentPage, err := affPage.NextPage(ctx)
			if err != nil {
				m.Logger().Errorf("[AWS Health] DescribeAffectedEntitie failed with : %w", err)
				break
			}
			// Extract relevant details of affected entities and match them with event details
			for _, ace := range affCurrentPage.Entities {
				affEntityTemp = AffectedEntityDetails{
					AwsAccountId:    awssdk.ToString(ace.AwsAccountId),
					EntityUrl:       awssdk.ToString(ace.EntityUrl),
					EntityValue:     awssdk.ToString(ace.EntityValue),
					LastUpdatedTime: awssdk.ToTime(ace.LastUpdatedTime),
					StatusCode:      string(ace.StatusCode),
					EntityArn:       awssdk.ToString(ace.EntityArn),
				}
				for l, hd := range healthDetailsTemp {
					if awssdk.ToString(ace.EventArn) == hd.EventArn {
						healthDetailsTemp[l].AffectedEntities = append(healthDetailsTemp[l].AffectedEntities, affEntityTemp)
						switch string(ace.StatusCode) {
						case "PENDING":
							healthDetailsTemp[l].AffectedEntitiesPending++
						case "RESOLVED":
							healthDetailsTemp[l].AffectedEntitiesResolved++
						case "":
							// Do Nothing
						default:
							healthDetailsTemp[l].AffectedEntitiesOthers++

						}
					}
				}
			}
		}
		// Append current page's health details to the overall list
		healthDetails = append(healthDetails, healthDetailsTemp...)
	}
	// Convert health details to Metricbeat events
	var events = make([]mb.Event, 0, len(healthDetails))
	for _, detail := range healthDetails {
		event := mb.Event{
			MetricSetFields: mapstr.M{
				"event_arn":                  detail.EventArn,
				"end_time":                   detail.EndTime,
				"event_scope_code":           detail.EventScopeCode,
				"event_type_category":        detail.EventTypeCategory,
				"event_type_code":            detail.EventTypeCode,
				"last_updated_time":          detail.LastUpdatedTime,
				"region":                     detail.Region,
				"service":                    detail.Service,
				"start_time":                 detail.StartTime,
				"status_code":                detail.StatusCode,
				"affected_entities":          detail.AffectedEntities,
				"event_description":          detail.EventDescription,
				"affected_entities_pending":  detail.AffectedEntitiesPending,
				"affected_entities_resolved": detail.AffectedEntitiesResolved,
				"affected_entities_others":   detail.AffectedEntitiesOthers,
			},
			RootFields: mapstr.M{
				"cloud.provider": "aws",
			},
			Service: "aws-health",
		}
		events = append(events, event)
	}
	return events
}