From bdd2d8f8e2868017c7d41f0b08bf065f410ef2c6 Mon Sep 17 00:00:00 2001 From: smithclay Date: Fri, 14 Jun 2024 18:35:31 -0700 Subject: [PATCH] flatten json payload --- .../components/servicenowexporter/client.go | 42 ++++++++++--------- .../servicenowexporter/servicenow_event.go | 4 -- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/collector/components/servicenowexporter/client.go b/collector/components/servicenowexporter/client.go index e11e6a7..cc012ed 100644 --- a/collector/components/servicenowexporter/client.go +++ b/collector/components/servicenowexporter/client.go @@ -45,29 +45,31 @@ func (c *midClient) Close() { c.httpClient.CloseIdleConnections() } -func (c *midClient) sendEvents(payload []ServiceNowEvent) error { +func (c *midClient) sendEvents(events []ServiceNowEvent) error { url := c.config.PushEventsURL - request := ServiceNowEventRequestBody{Records: payload} - c.logger.Info("Sending events to ServiceNow", zap.String("url", url), zap.Any("request", request)) - body, err := json.Marshal(request) - if err != nil { - return err - } - r, err := http.NewRequest("POST", url, bytes.NewBuffer(body)) - if err != nil { - return err - } - r.Header.Set("Content-Type", "application/json") - r.SetBasicAuth(c.config.Username, string(c.config.Password)) - res, err := c.httpClient.Do(r) - if err != nil { - return err - } - defer res.Body.Close() + for e := range events { + c.logger.Info("Sending event to ServiceNow", zap.String("url", url), zap.Any("event", e)) + body, err := json.Marshal(e) + if err != nil { + return err + } + r, err := http.NewRequest("POST", url, bytes.NewBuffer(body)) + if err != nil { + return err + } + r.Header.Set("Content-Type", "application/json") + r.SetBasicAuth(c.config.Username, string(c.config.Password)) - if res.StatusCode != 200 { - return handleNon200Response(res) + res, err := c.httpClient.Do(r) + if err != nil { + return err + } + defer res.Body.Close() + + if res.StatusCode != 200 { + handleNon200Response(res) + } } return nil diff --git a/collector/components/servicenowexporter/servicenow_event.go b/collector/components/servicenowexporter/servicenow_event.go index f1ae84c..70f55f5 100644 --- a/collector/components/servicenowexporter/servicenow_event.go +++ b/collector/components/servicenowexporter/servicenow_event.go @@ -1,9 +1,5 @@ package servicenowexporter -type ServiceNowEventRequestBody struct { - Records []ServiceNowEvent `json:"records"` -} - // https://docs.servicenow.com/bundle/vancouver-it-operations-management/page/product/event-management/task/send-events-via-web-service.html type ServiceNowEvent struct { // The resource on the node impacted