-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevent.go
More file actions
126 lines (105 loc) · 4.07 KB
/
event.go
File metadata and controls
126 lines (105 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package ilert
import (
"encoding/json"
"errors"
)
// EventComment represents a comment in an event
type EventComment struct {
// Required. The creator of the comment
Creator string `json:"creator"`
// Required. The content of the comment
Content string `json:"content"`
}
// EventLog represents a log entry in an event
type EventLog struct {
// Required. The timestamp of the log entry
Timestamp string `json:"timestamp"`
// Required. The log level (e.g., INFO, WARN, ERROR)
Level string `json:"level"`
// Required. The log body/message
Body string `json:"body"`
// Optional. Labels for the log entry
Labels map[string]string `json:"labels,omitempty"`
}
// Event represents the alert event https://api.ilert.com/api-docs/#tag/Events
type Event struct {
// Required. The API key of the alert source.
APIKey string `json:"apiKey"`
// Required. Must be either ALERT, ACCEPT, or RESOLVE
EventType string `json:"eventType"`
// Required. The event summary. Will be used as the alert summary if a new alert is created.
Summary string `json:"summary"`
// Optional. The event details. Will be used as the alert details if a new alert is created.
Details string `json:"details,omitempty"`
// Optional. For ALERT events, the alert key can be used to deduplicate or group events. If an open alert with the key already exists, the event will be appended to the alert's event log. Otherwise a new alert will be created. For ACCEPT and RESOLVE events, the alert key is used to reference the open alert which is to be accepted or resolved by this event.
AlertKey string `json:"alertKey,omitempty"`
// Optional. For ALERT events, the alert key can be used to deduplicate or group events. If an open alert with the key already exists, the event will be appended to the alert's event log. Otherwise a new alert will be created. For ACCEPT and RESOLVE events, the alert key is used to reference the open alert which is to be accepted or resolved by this event.
Priority string `json:"priority,omitempty"`
// Optional. A list of images containing src, href and alt.
Images []AlertImage `json:"images,omitempty"`
// Optional. A list of links, containing href and text.
Links []AlertLink `json:"links,omitempty"`
// Optional. A list of labels.
Labels map[string]string `json:"labels,omitempty"`
// Optional. Additional custom details for the event.
CustomDetails map[string]interface{} `json:"customDetails,omitempty"`
// Optional. A list of comments for the event.
Comments []EventComment `json:"comments,omitempty"`
// Optional. A list of log entries for the event.
Logs []EventLog `json:"logs,omitempty"`
}
// EventTypes defines event types
var EventTypes = struct {
Alert string
Accept string
Resolve string
}{
Alert: "ALERT",
Accept: "ACCEPT",
Resolve: "RESOLVE",
}
// EventResponse describes event API response body
type EventResponse struct {
AlertKey string `json:"alertKey"`
AlertURL string `json:"alertUrl"`
ResponseCode string `json:"responseCode"`
}
// CreateEventInput represents the input of a CreateEvent operation.
type CreateEventInput struct {
_ struct{}
// alert event
Event *Event
// (optional) request url
URL *string
}
// CreateEventOutput represents the output of a CreateEvent operation.
type CreateEventOutput struct {
_ struct{}
EventResponse *EventResponse
}
// CreateEvent creates an alert event. https://api.ilert.com/api-docs/#tag/Events/paths/~1events/post
func (c *Client) CreateEvent(input *CreateEventInput) (*CreateEventOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.Event == nil {
return nil, errors.New("input event is required")
}
url := apiRoutes.events
if input.URL != nil && *input.URL != "" {
url = *input.URL
}
resp, err := c.httpClient.R().SetBody(input.Event).Post(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200, 202); apiErr != nil {
return nil, apiErr
}
eventResponse := &EventResponse{}
err = json.Unmarshal(resp.Body(), eventResponse)
if err != nil {
return nil, err
}
return &CreateEventOutput{EventResponse: eventResponse}, nil
}