// HTTPCreateEventBatchHandler creates a batch of events
// Design assumption - all events in a given batch are dispatched for the same token and role
HTTPCreateEventBatchHandler creates a batch of events
Design assumption - all events in a given batch are dispatched for the same token and role
// HTTPCreateEventBatchHandler creates a batch of events // Design assumption - all events in a given batch are dispatched for the same token and role
func (c *Collector) HTTPCreateEventBatchHandler(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	var rec []recommendations.AWSEvent
	token, ok := middleware.TokenFromContext(ctx)
	if !ok {
		io.RespondError(ctx, c.log, w, errors.New("could not load token"))
		return
	}
	if token == nil {
		http.Error(w, "invalid token", http.StatusBadRequest)
		return
	}
	if err := io.DecodeJSONBody(w, r, &rec); err != nil {
		io.RespondError(ctx, c.log, w, err)
		return
	}
	c.log.With("events", rec).Info("received events")
	advisor := recommendations.NewAdvisor()
	var res CreateEventBatchResponse
	for _, e := range rec {
		action, err := c.handleRecommendation(handleRecommendationArgs{
			Event:   e,
			Token:   token,
			Advisor: advisor,
		})
		if err != nil {
			io.RespondError(ctx, c.log, w, err)
			return
		}
		res.AlertIDs = append(res.AlertIDs, action.ID)
	}
	io.RespondJSON(ctx, c.log, w, res, http.StatusAccepted)
}
func ( c * Collector ) HTTPCreateEventBatchHandler ( w http . ResponseWriter , r * http . Request ) { ctx := r . Context ( ) var rec [ ] recommendations . AWSEvent token , ok := middleware . TokenFromContext ( ctx ) if ! ok { io . RespondError ( ctx , c . log , w , errors . New ( " " ) ) return } if token == nil { http . Error ( w , " " , http . StatusBadRequest ) return } if err := io . DecodeJSONBody ( w , r , & rec ) ; err != nil { io . RespondError ( ctx , c . log , w , err ) return } c . log . With ( " " , rec ) . Info ( " " ) advisor := recommendations . NewAdvisor ( ) var res CreateEventBatchResponse for _ , e := range rec { action , err := c . handleRecommendation ( handleRecommendationArgs { Event : e , Token : token , Advisor : advisor , } ) if err != nil { io . RespondError ( ctx , c . log , w , err ) return } res . AlertIDs = append ( res . AlertIDs , action . ID ) } io . RespondJSON ( ctx , c . log , w , res , http . StatusAccepted ) }
HTTPCreateEventBatchHandler creates a batch of events
Design assumption - all events in a given batch are dispatched for the same token and role
HTTPCreateEventBatchHandler creates a batch of events Design assumption - all events in a given batch are dispatched for the same token and role