Skip to content

Commit

Permalink
flatten json payload
Browse files Browse the repository at this point in the history
  • Loading branch information
smithclay committed Jun 15, 2024
1 parent c8deb93 commit bdd2d8f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
42 changes: 22 additions & 20 deletions collector/components/servicenowexporter/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions collector/components/servicenowexporter/servicenow_event.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit bdd2d8f

Please sign in to comment.